edit-icon download-icon

Authorized access

Last Updated: Aug 08, 2018

Use STS for temporary access authorization

OSS supports Alibaba Cloud Security Token Service (STS) for temporary access authorization. STS is a web service that provides a temporary access token to a cloud computing user. Through the STS, you can assign a third-party application or a RAM user (you can manage the user ID) an access credential with a custom validity period and permissions. For more information about STS, see STS introduction.

STS advantages:

  • Your long-term key (AccessKey) is not exposed to a third-party application. You only need to generate an access token and send the access token to the third-party application. You can customize access permissions and the validity of this token.

  • You do not need to keep track of permission revocation issues. The access token automatically becomes invalid when it expires.

For more information about the process of access to OSS with STS, see RAM and STS scenario practices in OSS Developer Guide.

Create a signature request with STS

Use the following code to create a signature request with STS:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String securityToken = "<yourSecurityToken>";
  7. // After a user obtains a temporary STS credential, the OSSClient is generated with the security token and temporary access key (AccessKeyID and AccessKeySecret).
  8. // Create an OSSClient instance.
  9. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret, securityToken);
  10. // Perform operations on OSS.
  11. // Close your OSSClient.
  12. ossClient.shutdown();

Sign a URL to authorize temporary access

Sign a URL

You can provide a signed URL to a visitor for temporary access. When you sign a URL, you can specify the expiration time for a URL to restrict the period of access from visitors.

Sign a URL for access with HTTP GET

Use the following code to sign a URL that allows access with HTTP GET:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. String objectName = "<yourObjectName>";
  8. // Create an OSSClient instance.
  9. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  10. // Set the expiration time of a URL to one hour.
  11. Date expiration = new Date(new Date().getTime() + 3600 * 1000);
  12. // Generate the URL that allows access with HTTP GET. Visitors can use a browser to access relevant content.
  13. URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
  14. // Close your OSSClient.
  15. ossClient.shutdown();

Sign a URL for access with other HTTP methods

A URL needs to be signed for temporary access from a visitor to perform other operations such as file upload and deletion. Example:

  1. // Sign a URL that allows access with HTTP PUT.
  2. URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration, HttpMethod.PUT);

Visitors can specify the HttpMethod.PUT parameter and use the signed URL to upload files.

Add specified parameters to a URL

Use the following code to add specified parameters to a URL:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. String objectName = "<yourObjectName>";
  8. // Create an OSSClient instance.
  9. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  10. // Create a request.
  11. GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectName);
  12. // Set HttpMethod to PUT.
  13. generatePresignedUrlRequest.setMethod(HttpMethod.PUT);
  14. // Add user-defined Object Meta.
  15. generatePresignedUrlRequest.addUserMetadata("author", "baymax");
  16. // Add Content-Type.
  17. generatePresignedUrlRequest.setContentType("application/octet-stream");
  18. // Set the expiration time of a URL to one hour.
  19. Date expiration = new Date(new Date().getTime() + 3600 * 1000);
  20. generatePresignedUrlRequest.setExpiration(expiration);
  21. // Generate the signed URL.
  22. URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
  23. // Close your OSSClient.
  24. ossClient.shutdown();

Use a signed URL to obtain or upload an object

Use a signed URL to obtain an object

Use the following code to obtain a specified object through a signed URL:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. String objectName = "<yourObjectName>";
  8. // Create an OSSClient instance.
  9. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  10. Date expiration = DateUtil.parseRfc822Date("Wed, 18 Mar 2022 14:20:00 GMT");
  11. GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
  12. // Configure the expiration time.
  13. request.setExpiration(expiration);
  14. // Generate a signed URL that allows HTTP GET access.
  15. URL signedUrl = ossClient .generatePresignedUrl(request);
  16. System.out.println("signed url for getObject: " + signedUrl);
  17. // Use the signed URL to send a request.
  18. Map<String, String> customHeaders = new HashMap<String, String>();
  19. // Add a request header to GetObject.
  20. customHeaders.put("Range", "bytes=100-1000");
  21. OSSObject object = ossClient.getObject(signedUrl,customHeaders);
  22. // Close your OSSClient.
  23. ossClient.shutdown();

Use a signed URL to upload a file

Use the following code to upload a file with a signed URL:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. String objectName = "<yourObjectName>";
  8. // Create an OSSClient instance.
  9. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  10. // Generate the signed URL.
  11. Date expiration = DateUtil.parseRfc822Date("Thu, 19 Mar 2019 18:00:00 GMT");
  12. GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.PUT);
  13. // Configure the expiration time.
  14. request.setExpiration(expiration);
  15. // Configure Content-Type.
  16. request.setContentType("application/octet-stream");
  17. // Configure user-defined Object Meta.
  18. request.addUserMetadata("author", "aliy");
  19. // Generate a signed URL that allows access with HTTP PUT.
  20. URL signedUrl = ossClient.generatePresignedUrl(request);
  21. System.out.println("signed url for putObject: " + signedUrl);
  22. // Use the signed URL to send a request.
  23. File f = new File("<yourLocalFile>");
  24. FileInputStream fin = new FileInputStream(f);
  25. // Add a request header to PutObject.
  26. Map<String, String> customHeaders = new HashMap<String, String>();
  27. customHeaders.put("Content-Type", "application/octet-stream");
  28. customHeaders.put("x-oss-meta-author", "aliy");
  29. PutObjectResult result = ossClient.putObject(signedUrl, fin, f.length(), customHeaders);
  30. // Close your OSSClient.
  31. ossClient.shutdown();
Thank you! We've received your feedback.