You can provide users with a temporary access URL by generating a signed URL. During URL generation, you can specify the URL expiration time to limit the duration of the user’s access.
The code is as follows:
var req = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Get)
Expiration = DateTime.Now.AddHours(1)
var uri = client.GeneratePresignedUri(req);
By default, the generated URL supports GET access, which grants users direct access to related content through a browser.
If you want to allow users to perform other temporary operations (such as object upload and deletion), you may require to sign the URL by using another method. The following code signs the URL for the PUT method:
// Generate the URL of the PUT method
var req = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Put)
Expiration = DateTime.Now.AddHours(1),
ContentType = "text/html"
var uri = client.GeneratePresignedUri(req);
Currently, the .NET SDK supports signing URLs by using the PutObject and GetObject methods.
var generatePresignedUriRequest = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Put);
var signedUrl = client.GeneratePresignedUri(generatePresignedUriRequest);
var result = client.PutObject(signedUrl, fileToUpload);
OSS can temporarily grant authorization to access the Alibaba Cloud STS service. Alibaba Cloud STS (Security Token Service) is a web service that provides temporary access token to a cloud computing user.
Through STS, you can assign a third-party application or federated user (you can manage the user ID) an access credential with a custom validity period and permissions.
Third-party applications or federated users can use these access credentials to directly call the Alibaba Cloud product APIs or use the SDKs provided by Alibaba Cloud products to access the cloud product APIs.
You do not require to expose your long-term key (AccessKey) 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 the access permission and validity of this token.
The access token automatically becomes invalid when it expires.
The interaction process is as follows:
Log on as the app user. App user IDs are managed by the customer. The customer can customize the ID management system or use an external web account or OpenID. For each valid app user, the AppServer can precisely define the minimum access permission.
The AppServer requests a security token (SecurityToken) from the STS. Before calling STS, the AppServer needs to determine the minimum access permission (described in policy syntax) of the app user and the expiration time of the authorization. Then you can obtain the security token by calling the STS’ AssumeRole interface. For more information about roles and usage, see Role in the RAM User Guide.
The STS returns a valid access credential to the AppServer. The access credential contains a security token, a temporary pair of accesskeys (an AccessKeyId and an AccessKeySecret), and the expiration time.
The AppServer returns the access credential to the ClientApp. The ClientApp can cache this credential. When the credential becomes invalid, the ClientApp needs to request a new valid access credential from the AppServer. For example, if the access credential is valid for one hour, the ClientApp can request the AppServer to update the access credential every 30 minutes.
The ClientApp uses the access credential cached locally to request Alibaba Cloud Service APIs. The cloud service perceives the STS access credential, relies on STS to verify the credential, and responds to the user request appropriately.
For more information about the STS security token, see Roles in the RAM User Guide. The key is to obtain a valid access credential by calling the STS interface AssumeRole. The method can also be called using the STS DSK. For more information, see Preface.
After obtaining the STS temporary credential, the user’s client generates an OSSClient using the contained security token (SecurityToken) and temporary accessKey (AccessKeyId, AccessKeySecret). Take object upload as an example:
string accessKeyId = "<accessKeyId>";
string accessKeySecret = "<accessKeySecret>";
string securityToken = "<securityToken>"
// Take Hangzhou as an example
string endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
var ossClient = new OssClient(endpoint, accessKeyId, accessKeySecret, securityToken);