This topic describes how to authorize temporary access to OSS by using STS or a signed URL.

Use STS to authorize temporary access

You can use Alibaba Cloud Security Token Service (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 a third-party application or your RAM user an access credential with a customized validity period and permissions. 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 long-term AccessKey pair to the third-party application. You can customize the access permissions and validity period of this token.
  • The access token automatically expires when the validity period ends.

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 create a request that contains signatures by using STS:

using Aliyun.OSS;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var securityToken = "<yourSecurityToken>";
// After you obtain a temporary STS credential for your OSSClient, an OSSClient instance is created based on the security token and temporary AccessKey pair (AccessKey ID and AccessKey secret) that are contained in the credential.
// Create an OSSClient instance.
var ossStsClient = new OssClient(endpoint, accessKeyId, accessKeySecret, securityToken);
// Perform corresponding operations.
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.

Use a signed URL to authorize temporary access

You can generate a signed URL and provide it to a visitor to grant 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 the complete code of using a signed URL to authorize temporary access, visit GitHub.

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

  • 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:

    using Aliyun.OSS;
    using Aliyun.OSS.Common;
    var endpoint = "<yourEndpoint>";
    var accessKeyId = "<yourAccessKeyId>";
    var accessKeySecret = "<yourAccessKeySecret>";
    var bucketName = "<yourBucketName>";
    var objectName = "<yourObjectName>";
    var objectContent = "More than just cloud." ;
    // Create an OSSClient instance.
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    try
    {
        // Generate a signed URL.
        var generatePresignedUriRequest = new GeneratePresignedUriRequest(bucketName, objectName, SignHttpMethod.Put)
        {
            Expiration = DateTime.Now.AddHours(1),
        };
        var signedUrl = client.GeneratePresignedUri(generatePresignedUriRequest);
        // Use the signed URL to upload an object.
        var buffer = Encoding.UTF8.GetBytes(objectContent);
        using (var ms = new MemoryStream(buffer))
        {
            client.PutObject(signedUrl, ms);
        }
        Console.WriteLine("Put object by signatrue succeeded. {0} ", signedUrl.ToString());
    }
    catch (OssException ex)
    {
        Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
            ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed with error info: {0}", ex.Message);
    }
  • 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:

    using Aliyun.OSS;
    using Aliyun.OSS.Common;
    var endpoint = "<yourEndpoint>";
    var accessKeyId = "<yourAccessKeyId>";
    var accessKeySecret = "<yourAccessKeySecret>";
    var bucketName = "<yourBucketName>";
    var objectName = "<yourObjectName>";
    var downloadFilename = "<yourDownloadFilename>";
    // Create an OSSClient instance.
    var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
    try
    {
        var metadata = client.GetObjectMetadata(bucketName, objectName);
        var etag = metadata.ETag;
        // Generate a signed URL.
        var req = new GeneratePresignedUriRequest(bucketName, objectName, SignHttpMethod.Get);
        var uri = client.GeneratePresignedUri(req);
        // Use the signed URL to download an object.
        OssObject ossObject = client.GetObject(uri);
        using (var file = File.Open(downloadFilename, FileMode.OpenOrCreate))
        {
            using (Stream stream = ossObject.Content)
            {
                int length = 4 * 1024;
                var buf = new byte[length];
                do
                {
                    length = stream.Read(buf, 0, length);
                    file.Write(buf, 0, length);
                } while (length ! = 0);
            }
        }
        Console.WriteLine("Get object by signatrue succeeded. {0} ", uri.ToString());
    }
    catch (OssException ex)
    {
        Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}", 
            ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed with error info: {0}", ex.Message);
    }