Feature Overview
Node.js SDK provides APIs for getting object URLs and pre-signed request URLs. For details, see the descriptions and samples below.
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
We recommend users to generate pre-signed URLs using temporary keys to further enhance the security of pre-signed upload and download requests. When applying for temporary keys, please follow the Principle of Least Privilege to prevent leakage of resources beyond the target bucket or object. The action for applying temporary keys should include the
"name/cos:GetObject" permission.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.
Signature Calculation
In all COS XML API requests, the authentication credential
Authorization is required for all operations on private resources for COS to determine whether a request is valid.There are two ways to use the authentication credential:
1. Put it in the header parameter; field name: authorization.
2. Put it in the URL parameter; field name: sign.
The
COS.getAuthorization method is used to calculate the authentication credential (Authorization), which is the signing information used to verify the validity of the request.Note
It is recommended that this method is only used for frontend debugging but not in actual projects, as it may disclose keys.
Use Cases
Obtaining the authentication credential for file download:
// Log in to https://console.cloud.tencent.com/cam/capi to check and manage the SecretId and SecretKey of your project.var COS = require('cos-nodejs-sdk-v5');var Authorization = COS.getAuthorization({SecretId: process.env.SecretId, // Recommended to obtain from environment variables; User's SecretId, it is advised to use sub-account keys and follow the principle of least privilege to reduce usage risks. For obtaining sub-account keys, please refer to https://cloud.tencent.com/document/product/598/37140SecretKey: process.env.SecretKey, // Recommended to obtain from environment variables; User's SecretKey, it is advised to use sub-account keys and follow the principle of least privilege to reduce usage risks. For obtaining sub-account keys, please refer to https://cloud.tencent.com/document/product/598/37140Method: 'get',Key: 'a.jpg',Expires: 60,Query: {},Headers: {},});
Description
Parameter | ParameterDescription | Local Disk Types | Required |
SecretId | Your SecretId. | String | Required |
SecretKey | Your SecretKey. | String | Required |
Method | HTTP request method such as GET, POST, DELETE, or HEAD | String | Required |
Key | Object key (object name) is the unique ID of an object in a bucket. If the request operation is to be performed on a file, this parameter is required and should be a filename. If the operation is on a bucket, this parameter should be left empty. | String | Not required |
Query | Request parameters to include in the signature. Format: {key: 'val'} | Object | Not required |
Headers | Request headers to include in the signature. Format: {key: 'val'} | Object | Not required |
Expires | Signature expiration time in seconds. Default value: 900. | Number | Not required |
Returned value description
The return value is the calculated authentication credential string
authorization.Getting a Pre-signed Request URL
Download request samples
Sample 1. Get an unsigned object URL.
var url = cos.getObjectUrl({Bucket: 'examplebucket-1250000000', /* Enter your own bucket, required field */Region: 'COS_REGION', /* The region where the bucket is located, such as ap-beijing, required field */Key: '1.jpg', /* The object key stored in the bucket (e.g., 1.jpg, a/b/test.txt), supports Chinese characters, required field */Sign: false,});
Sample 2. Get a signed object URL.
var url = cos.getObjectUrl({Bucket: 'examplebucket-1250000000', /* Enter your own bucket, required field */Region: 'COS_REGION', /* The region where the bucket is located, such as ap-beijing, required field */Key: 'Avatar.jpg', /* The object key stored in the bucket (e.g., 1.jpg, a/b/test.txt), supports Chinese characters, required field */});
Sample 3. If the signing process is asynchronous, you need to get the signed URL through a callback.
cos.getObjectUrl({Bucket: 'examplebucket-1250000000', /* Enter your own bucket, required field */Region: 'COS_REGION', /* The region where the bucket is located, such as ap-beijing, required field */Key: 'Avatar.jpg', /* The object key stored in the bucket (e.g., 1.jpg, a/b/test.txt), supports Chinese characters, required field */Sign: false,},function (err, data) {console.log(err || data.Url);});
Sample 4. Specify the validity period of a link.
cos.getObjectUrl({Bucket: 'examplebucket-1250000000', /* Enter your own bucket, required field */Region: 'COS_REGION', /* The region where the bucket is located, such as ap-beijing, required field */Key: 'Avatar.jpg', /* The object key stored in the bucket (e.g., 1.jpg, a/b/test.txt), supports Chinese characters, required field */Sign: true,Expires: 3600, // Unit: seconds},function (err, data) {console.log(err || data.Url);});
Sample 5. Get the file URL and download the file.
var request = require('request');var fs = require('fs');cos.getObjectUrl({Bucket: 'examplebucket-1250000000', /* Enter your own bucket, required field */Region: 'COS_REGION', /* The region where the bucket is located, such as ap-beijing, required field */Key: 'Avatar.jpg', /* The object key stored in the bucket (e.g., 1.jpg, a/b/test.txt), supports Chinese characters, required field */Sign: true,},function (err, data) {if (err) return console.log(err);console.log(data.Url);var req = request(data.Url, function (err, response, body) {console.log(err || body);});var writeStream = fs.createWriteStream(__dirname + '/1.jpg');req.pipe(writeStream);});
Sample 6. Generate the pre-signed URL with the signature containing
Query and Header.cos.getObjectUrl({Bucket: 'examplebucket-1250000000', /* Enter your own bucket, required field */Region: 'COS_REGION', /* The region where the bucket is located, such as ap-beijing, required field */Key: 'Avatar.jpg', /* The object key stored in the bucket (e.g., 1.jpg, a/b/test.txt), supports Chinese characters, required field */Sign: true,/* The input request parameters must be the same as the actual request to prevent users from tampering with the HTTP request parameters */Query: {'imageMogr2/thumbnail/200x/': '',},/* The input request header must be included in the actual request to prevent users from tampering with the HTTP request header entered here. */Headers: {host: 'xxx', /* Specify the host for access; non-specified hosts will result in a 403 error */},},function (err, data) {console.log(err || data.Url);});
Upload request samples
Sample 1. Get a pre-signed URL for
Put Object upload.var request = require('request');var fs = require('fs');cos.getObjectUrl({Bucket: 'examplebucket-1250000000', /* Enter your own bucket, required field */Region: 'COS_REGION', /* The region where the bucket is located, such as ap-beijing, required field */Key: 'Avatar.jpg', /* The object key stored in the bucket (e.g., 1.jpg, a/b/test.txt), supports Chinese characters, required field */Method: 'PUT',Sign: true,},function (err, data) {if (err) return console.log(err);console.log(data.Url);var readStream = fs.createReadStream(__dirname + '/1.jpg');var req = request({method: 'PUT',url: data.Url,},function (err, response, body) {console.log(err || body);});readStream.pipe(req);});
Description
Parameter | ParameterDescription | Local Disk Types | Required |
Bucket | Bucket name in the format of BucketName-APPID | String | Required |
Region | String | Required | |
Key | Object key (object name) is the unique ID of an object in a bucket. If the request operation is to be performed on a file, this parameter is required and should be a filename. If the operation is on a bucket, this parameter should be left empty. | String | Required |
Sign | Whether to return a signed URL. Default value: true. | Boolean | Not required |
Protocol | The optional values are http: or https:, with the default being http: (including the colon). | String | Not required |
Domain | Bucket access domain name. Default value: {BucketName-APPID}.cos.{Region}.myqcloud.com. | String | Not required |
Method | HTTP request method such as GET, POST, DELETE, or HEAD. Default value: GET. | String | Not required |
Query | Request parameters to include in the signature. Format: {key: 'val'} | Object | Not required |
Headers | Request headers to include in the signature. Format: {key: 'val'} | Object | Not required |
Expires | Signature expiration time in seconds. Default value: 900. | Number | Not required |
Returned value description
A string is returned in one of the two ways:
1. If the signature can be calculated synchronously (for example, the SecretId and SecretKey have been passed in during instantiation), the signed URL will be returned by default.
2. Otherwise, an unsigned URL will be returned.
Callback function description
function(err, data) { ... }
Parameter | ParameterDescription | Local Disk Types |
err | Error code, which is returned when an error (network error or service error) occurs. If the request is successful, this parameter is empty. For more information, see Error Codes. | Object |
data | Object returned when the request is successful. If the request fails, this parameter is empty. | Object |
- Url | Calculated URL | String |