参考 : https://help.aliyun.com/document_detail/32016.html
aliyun oss基本信息定义
http://
private final static String endpoint = "oss-cn-beijing.aliyuncs.com";
private final static String accessKeyId = "accessKeyId";
private final static String accessKeySecret = "accessKeySecret";
private final static String roleArn = "acs:ram::xxxx:role/test";
private final static String roleSessionName = "roleSessionName";
private final static String bucketName = "test-bucket";
private final static String objectName = "test_sts";
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值
DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", accessKeyId, accessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
//构造请求,设置参数。关于参数含义和设置方法,请参见API参考。
AssumeRoleRequest request = new AssumeRoleRequest();
request.setRegionId("cn-beijing");
request.setRoleArn(roleArn);
request.setRoleSessionName(roleSessionName);
request.setPolicy(policy); // 如果policy为空,则用户将获得该角色下所有权限。
request.setDurationSeconds(10 * 60);
//发起请求,并得到响应。
AssumeRoleResponse response = client.getAcsResponse(request);
临时访问地址获取
HttpMethod method = HttpMethod.GET;
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// path为bucket下请求资源的全路径
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, path, method);
Date expiration = new Date(System.currentTimeMillis() + (10 * 60 * 1000));
request.setExpiration(expiration);
URL signedUrl = ossClient.generatePresignedUrl(request);