Skip to content

Commit

Permalink
Merge pull request #69 from AlibabaCloudLandingZone/solution-fc-sts-t…
Browse files Browse the repository at this point in the history
…oken/0.0.4

solution-fc-sts-token/0.0.4
  • Loading branch information
wibud authored Jul 12, 2024
2 parents b9ff85d + 3cd337c commit f55849b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion solution/solution-fc-sts-token/code-example/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.17.2</version>
<version>3.17.4</version>
</dependency>
<!--sls-sdk-->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Bucket> buckets = ossClient.listBuckets();
Expand Down

0 comments on commit f55849b

Please sign in to comment.