Commit 86bc7e2720782af132dc51cc93d0052f259b342f
1 parent
03da0354
fix(infra): use provider endpoint for OSS presign
Showing
2 changed files
with
42 additions
and
0 deletions
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/framework/file/core/client/s3/S3FileClient.java
| ... | ... | @@ -173,6 +173,12 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> { |
| 173 | 173 | * @return 节点地址 |
| 174 | 174 | */ |
| 175 | 175 | private String buildPresignerEndpoint() { |
| 176 | + // 阿里云 OSS、七牛云的自定义域名仅用于文件访问,不能作为 S3 签名节点。 | |
| 177 | + // Presigner 必须使用服务商提供的 endpoint,否则生成的签名无法用于直传。 | |
| 178 | + if (StrUtil.contains(config.getEndpoint(), S3FileClientConfig.ENDPOINT_ALIYUN) | |
| 179 | + || StrUtil.contains(config.getEndpoint(), S3FileClientConfig.ENDPOINT_QINIU)) { | |
| 180 | + return buildEndpoint(); | |
| 181 | + } | |
| 176 | 182 | // 补全 domain |
| 177 | 183 | if (StrUtil.isEmpty(config.getDomain())) { |
| 178 | 184 | config.setDomain(buildDomain()); | ... | ... |
yudao-module-infra/src/test/java/cn/iocoder/yudao/module/infra/framework/file/core/s3/S3FileClientTest.java
| ... | ... | @@ -70,6 +70,42 @@ public class S3FileClientTest { |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | @Test |
| 73 | + public void testPresignPutUrl_aliyunUsesEndpointInsteadOfCustomDomain() { | |
| 74 | + S3FileClientConfig config = new S3FileClientConfig(); | |
| 75 | + config.setAccessKey("access-key"); | |
| 76 | + config.setAccessSecret("access-secret"); | |
| 77 | + config.setBucket("test-bucket"); | |
| 78 | + config.setDomain("https://custom.example.com"); | |
| 79 | + config.setEndpoint("oss-cn-beijing.aliyuncs.com"); | |
| 80 | + config.setEnablePathStyleAccess(false); | |
| 81 | + config.setEnablePublicAccess(false); | |
| 82 | + S3FileClient client = new S3FileClient(0L, config); | |
| 83 | + client.init(); | |
| 84 | + | |
| 85 | + String result = client.presignPutUrl("avatar.jpg"); | |
| 86 | + | |
| 87 | + assertTrue(result.startsWith("https://test-bucket.oss-cn-beijing.aliyuncs.com/")); | |
| 88 | + } | |
| 89 | + | |
| 90 | + @Test | |
| 91 | + public void testPresignPutUrl_qiniuUsesEndpointInsteadOfCustomDomain() { | |
| 92 | + S3FileClientConfig config = new S3FileClientConfig(); | |
| 93 | + config.setAccessKey("access-key"); | |
| 94 | + config.setAccessSecret("access-secret"); | |
| 95 | + config.setBucket("test-bucket"); | |
| 96 | + config.setDomain("https://custom.example.com"); | |
| 97 | + config.setEndpoint("s3-cn-south-1.qiniucs.com"); | |
| 98 | + config.setEnablePathStyleAccess(false); | |
| 99 | + config.setEnablePublicAccess(false); | |
| 100 | + S3FileClient client = new S3FileClient(0L, config); | |
| 101 | + client.init(); | |
| 102 | + | |
| 103 | + String result = client.presignPutUrl("avatar.jpg"); | |
| 104 | + | |
| 105 | + assertTrue(result.startsWith("https://test-bucket.s3-cn-south-1.qiniucs.com/")); | |
| 106 | + } | |
| 107 | + | |
| 108 | + @Test | |
| 73 | 109 | @Disabled // MinIO,如果要集成测试,可以注释本行 |
| 74 | 110 | public void testMinIO() throws Exception { |
| 75 | 111 | S3FileClientConfig config = new S3FileClientConfig(); | ... | ... |