参考 : https://help.aliyun.com/document_detail/32016.html

aliyun oss基本信息定义

  1. http://
  2. private final static String endpoint = "oss-cn-beijing.aliyuncs.com";
  3. private final static String accessKeyId = "accessKeyId";
  4. private final static String accessKeySecret = "accessKeySecret";
  5. private final static String roleArn = "acs:ram::xxxx:role/test";
  6. private final static String roleSessionName = "roleSessionName";
  7. private final static String bucketName = "test-bucket";
  8. private final static String objectName = "test_sts";
  9. private final static String policy = "{\"Version\":1,\"Statement\":[{\"Action\":[\"oss:GetObject\"],\"Resource\":[\"acs:oss:*:*:test-bucket/*\"],\"Effect\":\"Allow\"}]}";

STS Token获取

[

](https://help.aliyun.com/document_detail/100624.htm?spm=a2c4g.11186623.0.0.4d4f4f77Lw4Fqj#concept-xzh-nzk-2gb)
需按照教程先生成roleArn值

  1. DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", accessKeyId, accessKeySecret);
  2. IAcsClient client = new DefaultAcsClient(profile);
  3. //构造请求,设置参数。关于参数含义和设置方法,请参见API参考。
  4. AssumeRoleRequest request = new AssumeRoleRequest();
  5. request.setRegionId("cn-beijing");
  6. request.setRoleArn(roleArn);
  7. request.setRoleSessionName(roleSessionName);
  8. request.setPolicy(policy); // 如果policy为空,则用户将获得该角色下所有权限。
  9. request.setDurationSeconds(10 * 60);
  10. //发起请求,并得到响应。
  11. AssumeRoleResponse response = client.getAcsResponse(request);

临时访问地址获取

  1. HttpMethod method = HttpMethod.GET;
  2. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
  3. // path为bucket下请求资源的全路径
  4. GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, path, method);
  5. Date expiration = new Date(System.currentTimeMillis() + (10 * 60 * 1000));
  6. request.setExpiration(expiration);
  7. URL signedUrl = ossClient.generatePresignedUrl(request);