diff --git a/solution/solution-fc-sts-token/code-example/java/pom.xml b/solution/solution-fc-sts-token/code-example/java/pom.xml index fac4bcb..6daf112 100644 --- a/solution/solution-fc-sts-token/code-example/java/pom.xml +++ b/solution/solution-fc-sts-token/code-example/java/pom.xml @@ -44,7 +44,7 @@ com.aliyun.oss aliyun-sdk-oss - 3.17.2 + 3.17.4 diff --git a/solution/solution-fc-sts-token/code-example/java/src/main/java/org/example/oss_sdk/App.java b/solution/solution-fc-sts-token/code-example/java/src/main/java/org/example/oss_sdk/App.java index 816d2cf..d55ac88 100644 --- a/solution/solution-fc-sts-token/code-example/java/src/main/java/org/example/oss_sdk/App.java +++ b/solution/solution-fc-sts-token/code-example/java/src/main/java/org/example/oss_sdk/App.java @@ -8,12 +8,14 @@ import com.alibaba.fastjson2.JSON; import com.aliyun.fc.runtime.Context; import com.aliyun.fc.runtime.StreamRequestHandler; +import com.aliyun.oss.ClientBuilderConfiguration; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.common.auth.CredentialsProvider; import com.aliyun.oss.common.auth.DefaultCredentialProvider; import com.aliyun.oss.common.auth.Credentials; import com.aliyun.oss.common.auth.DefaultCredentials; +import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.Bucket; public class App implements StreamRequestHandler { @@ -28,10 +30,21 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co CredentialsProvider credentialsProvider = new DefaultCredentialProvider(ossCreds); - // OSS endpoint,以杭州为例 + // Bucket所在地域对应的Endpoint。以华东1(杭州)为例。 String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; - - OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider); + // Endpoint对应的Region信息,例如cn-hangzhou。 + String region = "cn-hangzhou"; + // 建议使用更安全的V4签名算法,则初始化时需要加入endpoint对应的region信息,同时声明SignVersion.V4 + // OSS Java SDK 3.17.4及以上版本支持V4签名。 + ClientBuilderConfiguration configuration = new ClientBuilderConfiguration(); + configuration.setSignatureVersion(SignVersion.V4); + + OSS ossClient = OSSClientBuilder.create() + .endpoint(endpoint) + .credentialsProvider(credentialsProvider) + .clientConfiguration(configuration) + .region(region) + .build(); // 调用OpenAPI List buckets = ossClient.listBuckets();