Feature Overview
This document provides an overview of SDK code samples related to generating pre-signed object URLs.
For instructions on using pre-signed URLs for uploads, please refer to Pre-signed Authorization Upload. For instructions on using pre-signed URLs for downloads, please refer to Pre-signed Authorization Download.
Note
To learn about signature rules, see Request Signature.
We recommend users to generate pre-signed URLs using temporary keys to further enhance the security of pre-signed upload, download, and other requests through temporary authorization. When applying for temporary keys, please follow the Principle of Least Privilege to prevent the leakage of resources beyond the target storage bucket or object.
If you need to use a permanent key to generate a pre-signed URL, you are advised to limit the permission of the permanent key to uploads and downloads only to avoid risks.
Generating a Pre-Signed URL for Downloading Objects
Note
This API is used to generate a pre-signed URL for downloading COS objects.
Sample code
// Bucket name in the format of <1>BucketName-APPID</1> (<3>APPID</3> is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket.let bucket = "examplebucket-1250000000";// The location of the object in the bucket, i.e., the object keylet cosPath = "exampleobject";// URL request parameterslet requestParameters = {"test1k":"test1v", "test2k":"test2v"};try {// Obtain pre-signed download URL for an objectString objectUrl = await Cos.getDefaultService().getPresignedUrl(bucket,cosPath,{parameters: requestParameters});} catch (e) {// An exception will be thrown upon failure, handle the error accordinglyconsole.log(e);}
Description
Parameter name | Description | Local Disk Types | Required |
bucket | Bucket name follows the naming convention BucketName-APPID. For more information, please refer to Bucket Overview. | String | Required |
cosPath | The Object Key is the unique identifier of an object within a storage bucket. For example, in the object access domain examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/picture.jpg, the Object Key is doc/picture.jpg. | String | Required |
signValidTime | Signature validity period, in seconds. Note that this is the signature validity period. You need to ensure the key validity period by yourself. | Int | Not required |
signHost | Whether to include the Host header in the signature. You can choose not to include it, but the request may fail or vulnerabilities may occur. | Bool | Not required |
parameters | HTTP request parameters, which should be the same as those passed to the actual request. This can prevent users from tampering with the HTTP request parameters. | Map | Not required |
Response description
Success: A pre-signed URL for downloading objects is returned.
Failure: An error occurs (e.g., authentication failure), and an exception CosXmlClientError or CosXmlServiceError is thrown. For more information, please refer to Error Handling.