This topic describes how to authorize temporary access to Object Storage Service (OSS) by using Security Token Service (STS) or a signed URL.

Use STS to authorize temporary access

You can use STS to authorize temporary access to OSS. STS is a web service that provides temporary access tokens for cloud computing users. You can use STS to grant an access credential with a custom validity period and custom permissions for a third-party application or a RAM user managed by you. For more information about STS, see What is STS?

STS has the following benefits:

  • You need only to generate an access token and send the access token to a third-party application, instead of exposing your AccessKey pair to the third-party application. You can customize the access permissions and validity period of this token.
  • The access token automatically expires after the validity period. Therefore, you do not need to manually revoke the permissions of an access token.

For more information about how to access OSS by using STS, see Access OSS with a temporary access credential provided by STS in OSS Developer Guide.

The following code provides an example on how to generate a signed request by using an STS credential:

// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
String endpoint = "yourEndpoint";
// Specify the temporary access credential obtained from STS. The temporary access credential contains a security token and a temporary AccessKey pair that consists of an AccessKey ID and an AccessKey secret. 
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
String securityToken = "yourSecurityToken";
// Specify the bucket name. 
//String bucketName = "examplebucket";
// Specify the full path of the object. The full path of the object does not contain bucket names. 
//String objectName = "exampleobject.txt";

// You can use the AccessKey pair and security token contained in the temporary access credential obtained from STS to create an OSSClient. 
// Create an OSSClient instance. 
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, securityToken);

// Perform operations on OSS resources, such as upload or download objects. 
// Upload an object. In this example, a local file is uploaded to OSS as an object. 
// Specify the full path of the local file to upload. If the path of the local file is not specified, the file is uploaded to the path of the project to which the sample program belongs. 
//PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new File("D:\\localpath\\examplefile.txt"));
//ossClient.putObject(putObjectRequest);

// Download an object as a local file. If the specified local file already exists, the file is replaced by the downloaded object. If the specified local file does not exist, the local file is created. 
// If the path for the object is not specified, the downloaded object is saved to the path of the project to which the sample program belongs. 
//ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File("D:\\localpath\\examplefile.txt"));

// Shut down the OSSClient instance. 
ossClient.shutdown();

Use a signed URL to authorize temporary access

This section provides examples on how to use a signed URL to authorize temporary access.

Note The validity period must be set for both an STS temporary account and a signed URL. When you use an STS temporary account to generate a signed URL to perform operations such as object upload and download, the minimum validity period takes precedence. For example, you can set the validity period of your STS temporary account to 1200 seconds, and that of the signed URL to 3600 seconds. After 1200 seconds, you cannot use the signed URL generated by the STS temporary account to upload objects.
  • Generate a signed URL

    You can generate a signed URL and provide the URL to a visitor for temporary access. When you generate a signed URL, you can specify the validity period of the URL to limit the period of access from visitors.

    For more information about how to add signature information to a URL and forward the URL to a third party for authorized access, see Generate a signed URL.

  • Generate a signed URL to allow HTTP GET requests

    The following code provides an example on how to generate a signed URL to allow HTTP GET requests:

    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    String endpoint = "yourEndpoint";
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    String accessKeyId = "yourAccessKeyId";
    String accessKeySecret = "yourAccessKeySecret";
    // Specify the bucket name. 
    String bucketName = "examplebucket";
    // Specify the full path of the object. The full path of the object does not contain bucket names. 
    String objectName = "exampleobject.txt";
    
    // Create an OSSClient instance. 
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
    // Set the validity period of the URL to one hour. 
    Date expiration = new Date(new Date().getTime() + 3600 * 1000);
    // Generate the signed URL that allows HTTP GET requests. Visitors can enter the URL in a browser to access specified OSS resources. 
    URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
    System.out.println(url);
    // Shut down the OSSClient instance. 
    ossClient.shutdown();
                        
  • Generate a signed URL that allows other HTTP requests

    To authorize other users to temporarily perform operations such as object upload and deletion, you must generate a signed URL that allows corresponding HTTP requests. For example, you can generate a signed URL that allows HTTP PUT requests to authorize users to upload objects.

    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    String endpoint = "yourEndpoint";
    // Specify the temporary access credential obtained from STS. The temporary access credential contains a security token and a temporary AccessKey pair that consists of an AccessKey ID and an AccessKey secret. 
    String accessKeyId = "yourAccessKeyId";
    String accessKeySecret = "yourAccessKeySecret";
    String securityToken = "yourSecurityToken";
    // Specify the bucket name. 
    String bucketName = "examplebucket";
    // Specify the full path of the object. The full path of the object does not contain bucket names. 
    String objectName = "exampleobject.txt";
    
    // You can use the AccessKey pair and security token contained in the temporary access credential obtained from STS to create an OSSClient. 
    // Create an OSSClient instance. 
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, securityToken);
    
    GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.PUT);
    // Set the validity period of the URL to one hour. 
    Date expiration = new Date(new Date().getTime() + 3600 * 1000);
    request.setExpiration(expiration);
    // Set ContentType. 
    request.setContentType(DEFAULT_OBJECT_CONTENT_TYPE);
    // Set user metadata. 
    request.addUserMetadata("author", "aliy");
    
    // Generate a signed URL. 
    URL signedUrl = ossClient.generatePresignedUrl(request);
    
    Map<String, String> requestHeaders = new HashMap<String, String>();
    requestHeaders.put(HttpHeaders.CONTENT_TYPE, DEFAULT_OBJECT_CONTENT_TYPE);
    requestHeaders.put(OSS_USER_METADATA_PREFIX + "author", "aliy");
    
    // Use the signed URL to upload an object. 
    ossClient.putObject(signedUrl, new ByteArrayInputStream("Hello OSS".getBytes()), -1, requestHeaders, true);
    
    // Shut down the OSSClient instance. 
    ossClient.shutdown();
                        

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

  • Generate a signed URL with specified parameters

    The following code provides an example on how to generate a signed URL that contains specified parameters:

    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    String endpoint = "yourEndpoint";
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    String accessKeyId = "yourAccessKeyId";
    String accessKeySecret = "yourAccessKeySecret";
    // Specify the bucket name. 
    String bucketName = "examplebucket";
    // Specify the full path of the object excluding the bucket name. 
    String objectName = "exampleobject.txt";
    
    // Create an OSSClient instance. 
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
    // Create a request. 
    GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, objectName);
    // Set HttpMethod to PUT. 
    generatePresignedUrlRequest.setMethod(HttpMethod.PUT);
    // Add user metadata. 
    generatePresignedUrlRequest.addUserMetadata("author", "baymax");
    // Set ContentType. 
    generatePresignedUrlRequest.setContentType("application/txt");
    // Set the validity period of the URL to one hour. 
    Date expiration = new Date(new Date().getTime() + 3600 * 1000);
    generatePresignedUrlRequest.setExpiration(expiration);
    // Generate the signed URL. 
    URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
    System.out.println(url);
    // Shut down the OSSClient instance. 
    ossClient.shutdown();                    
  • Use a signed URL to upload or obtain an object
    • Use a signed URL to upload an object

      The following code provides an example on how to upload an object by using a signed URL:

      // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
      String endpoint = "yourEndpoint";
      // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
      String accessKeyId = "yourAccessKeyId";
      String accessKeySecret = "yourAccessKeySecret";
      // Specify the bucket name. 
      String bucketName = "examplebucket";
      // Specify the full path of the object excluding the bucket name. 
      String objectName = "exampleobject.txt";
      
      // Create an OSSClient instance. 
      OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
      
      // Specify the expiration date of the signed URL. 
      Date expiration = DateUtil.parseRfc822Date("Wed, 18 Mar 2022 14:20:00 GMT");
      // Generate a signed URL. 
      GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.PUT);
      // Set the validity period of the signed URL. 
      request.setExpiration(expiration);
      // Set ContentType. 
      request.setContentType("application/txt");
      // Add user metadata. 
      request.addUserMetadata("author", "aliy");
      // Generate a signed URL to allow HTTP PUT requests. 
      URL signedUrl = ossClient.generatePresignedUrl(request);
      System.out.println("signed url for putObject: " + signedUrl);
      
      // Use the signed URL to send a request. 
      // Specify the full path of the local file to upload. If the path of the local file is not specified, the file is uploaded to the path of the project to which the sample program belongs. 
      File f = new File("D:\\localpath\\examplefile.txt");
      FileInputStream fin = new FileInputStream(f);
      // Add headers to the PutObject request. 
      Map<String, String> customHeaders = new HashMap<String, String>();
      customHeaders.put("Content-Type", "application/txt");
      customHeaders.put("x-oss-meta-author", "aliy");
      
      PutObjectResult result = ossClient.putObject(signedUrl, fin, f.length(), customHeaders);
      
      // Shut down the OSSClient instance. 
      ossClient.shutdown();                           
    • Use a signed URL to download an object

      The following code provides an example on how to download a specified object by using a signed URL:

      // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
      String endpoint = "yourEndpoint";
      // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
      String accessKeyId = "yourAccessKeyId";
      String accessKeySecret = "yourAccessKeySecret";
      // Specify the bucket name. 
      String bucketName = "examplebucket";
      // Specify the full path of the object. The full path of the object does not contain bucket names. 
      String objectName = "exampleobject.txt";
      
      // Create an OSSClient instance. 
      OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
      
      // Specify the expiration date of the signed URL. 
      Date expiration = DateUtil.parseRfc822Date("Wed, 18 Mar 2022 14:20:00 GMT");
      // Generate a signed URL. 
      GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
      // Set the validity period of the signed URL. 
      request.setExpiration(expiration);
      // Generate a signed URL to allow HTTP GET requests. 
      URL signedUrl = ossClient .generatePresignedUrl(request);
      System.out.println("signed url for getObject: " + signedUrl);
      
      // Use the signed URL to send a request. 
      Map<String, String> customHeaders = new HashMap<String, String>();
      // Add headers to the GetObject request. 
      customHeaders.put("Range", "bytes=100-1000");
      OSSObject object = ossClient.getObject(signedUrl,customHeaders);
      
      // Shut down the OSSClient instance. 
      ossClient.shutdown();