This topic describes how to use temporary access credentials from Alibaba Cloud Security Token Service (STS) or a presigned URL to temporarily access Object Storage Service (OSS) resources.
You must specify a validity period for STS temporary access credentials and a presigned URL. If you use temporary access credentials to generate a presigned URL for operations such as uploading or downloading files, the shorter validity period takes effect. For example, if the validity period of your STS temporary access credentials is 1200 seconds and the validity period of the presigned URL is 3600 seconds, you cannot use the presigned URL to upload files after 1200 seconds because the STS temporary access credentials have expired.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
Use STS to authorize temporary access
OSS supports temporary access authorization through Alibaba Cloud Security Token Service (STS). STS is a web service that provides temporary access tokens for cloud computing users. With STS, you can issue an access credential with a custom validity period and permissions to third-party applications or sub-users (users whose identities you manage). For more information about STS, see What is STS.
STS provides the following benefits:
You do not have to expose your long-term AccessKey pair to third-party applications. Instead, you can generate an access token and provide it to the application. You can customize the access permissions and validity period of this token.
You do not need to manage permission revocation. The access token automatically becomes invalid when it expires.
To access OSS using temporary access credentials from STS, perform the following steps:
Obtain temporary access credentials
Temporary access credentials include a temporary AccessKey pair (an AccessKey ID and an AccessKey secret) and a security token (SecurityToken). The validity period of temporary access credentials is specified in seconds. The minimum value is 900. The maximum value is the maximum session duration specified for the current RAM role. For more information, see Set the maximum session duration for a RAM role.
You can obtain temporary access credentials in one of the following ways.
Method 1
Call the AssumeRole operation to obtain temporary access credentials.
Method 2
Use STS SDKs to obtain temporary access credentials. For more information, see STS SDKs.
Use the STS credentials to create a signed request.
Use a presigned URL to authorize temporary access
Usage notes
When you use an OSS SDK to generate a presigned URL, the OSS SDK uses a specific algorithm based on the key information stored in the local computer to calculate a signature and adds the signature to a URL to ensure the validity and security of the URL. The operations performed to calculate and construct the URL are completed on the client. You do not need to send requests to the server over the network. This way, you do not need to grant specific permissions to the caller when you generate the presigned URL. However, to allow third-party users to perform relevant operations on the resources authorized by the presigned URL, you must make sure that the principal that calls the API operations to generate the presigned URL has the corresponding permissions.
For example, if a principal wants to upload an object by using a presigned URL, you must grant the oss:PutObject permission to the principal. If a principal wants to download or preview an object by using a presigned URL, you must grant the oss:GetObject permission to the principal.
You can generate a presigned URL and provide the URL to a visitor for temporary access. When you generate a presigned URL, you can specify the validity period of the URL to limit the period of time during which the visitor can access specific data.
To generate a presigned URL that is used to access resources over HTTPS, set the protocol in the endpoint to HTTPS.
The presigned URL generated by using the following sample code may contain a plus sign (
+). In this case, replace the plus sign (+) in the URL with%2B. Otherwise, the presigned URL may not be used to access the object as expected.