content: package org.example.oss_sdk;
  
  import com.alibaba.fastjson2.JSON;
  import com.aliyun.credentials.Client;
  import com.aliyun.credentials.models.Config;
  import com.aliyun.credentials.models.CredentialModel;
  import com.aliyun.oss.*;
  import com.aliyun.oss.common.auth.Credentials;
  import com.aliyun.oss.common.auth.CredentialsProvider;
  import com.aliyun.oss.common.auth.DefaultCredentials;
  import com.aliyun.oss.common.comm.SignVersion;
  import com.aliyun.oss.model.Bucket;
  
  import java.util.List;
  
  /**
   * 通过Credentials工具初始化
   */
  public class CredentialsDefaultSample {
      public static void main(String[] args) {
          // 初始化凭据客户端
          Config credentialConfig = new Config();
          credentialConfig.setType("ecs_ram_role");
          // 选填，该ECS角色的角色名称，不填会自动获取，建议加上以减少请求次数
          credentialConfig.setRoleName("<your-ecs-instance-role-name>");
          // 在加固模式下获取STS Token，强烈建议开启
          credentialConfig.setEnableIMDSv2(true);
          Client credentialClient = new Client(credentialConfig);
  
          // Bucket所在地域对应的Endpoint。以华东1（杭州）为例。
          String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
          // 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客户端
          OSS ossClient = OSSClientBuilder.create()
              .endpoint(endpoint)
              .credentialsProvider(new CredentialsProvider() {
                  @Override
                  public void setCredentials(Credentials credentials) {
                  }
  
                  @Override
                  public Credentials getCredentials() {
                      // 保证线程安全，从 CredentialModel 中获取 ak/sk/security token
                      CredentialModel credentialModel = credentialClient.getCredential();
                      String ak = credentialModel.getAccessKeyId();
                      String sk = credentialModel.getAccessKeySecret();
                      String token = credentialModel.getSecurityToken();
                      return new DefaultCredentials(ak, sk, token);
                  }
              })
              .clientConfiguration(configuration)
              .region(region)
              .build();
  
          // 调用OSS API
          List<Bucket> buckets = ossClient.listBuckets();
          System.out.println(JSON.toJSONString(buckets));
  
          // 关闭OSSClient。
          ossClient.shutdown();
      }
  }
  