The SDK for WeChat Mini Program provides samples for calculating signatures and getting object URLs and pre-signed request URLs.
In all COS XML API requests, the authentication credential "Authorization" is required for all the operations on private resources for COS to determine whether a request is valid.
There are two ways to use the authentication credential:
The COS.getAuthorization method is used to calculate the authentication credential (Authorization), which is the signing information used to verify the request's validity.
It is recommended to only use this method for frontend debugging and not in real projects as it involves key disclosure risks.
Obtain the authentication credential for object download:
var Authorization = COS.getAuthorization({
SecretId: 'COS_SECRETID',
SecretKey: 'COS_SECRETKEY',
Method: 'get',
Key: 'picture.jpg',
Expires: 60,
Query: {},
Headers: {}
});
Parameter Name | Description | Type | Required |
---|---|---|---|
SecretId | User's SecretId | String | Yes |
SecretKey | User's SecretKey | String | Yes |
Method | HTTP operation method such as GET , POST , DELETE , and HEAD |
String | Yes |
Key | Object key (object name), a unique ID of an object in a bucket. |
String | No |
Query | Query parameter object of the request | Object | No |
Headers | Header parameter object of the request | Object | No |
Expires | Validity period of the signature; unit: second. Default value: 900 | Number | No |
The returned value is the calculated authentication credential string ""authorization"".
In a WeChat Mini Program, you can only upload files via the POST Object
API. Getting a pre-signed URL is not applicable to this API. If you need to upload files by yourself, please refer to Uploading Directly Through a WeChat Mini Program.
Sample 1. Get an object URL without signature
var url = cos.getObjectUrl({
Bucket: 'examplebucket-1250000000',
Region: 'ap-beijing',
Key: 'picture.jpg',
Sign: false
});
Sample 2. Get a signed object URL
var url = cos.getObjectUrl({
Bucket: 'examplebucket-1250000000',
Region: 'ap-beijing',
Key: 'picture.jpg'
});
Sample 3. Get a signed URL through callback
If the signing process is asynchronous, you need to get the signed URL through callback.
cos.getObjectUrl({
Bucket: 'examplebucket-1250000000',
Region: 'ap-beijing',
Key: 'picture.jpg',
Sign: false
}, function (err, data) {
console.log(err || data.Url);
});
Sample 4. Specify the validity period of the link
cos.getObjectUrl({
Bucket: 'examplebucket-1250000000',
Region: 'ap-beijing',
Key: 'picture.jpg',
Sign: true,
Expires: 3600, // Unit: second
}, function (err, data) {
console.log(err || data.Url);
});
Sample 5. Get the object URL and download the object
cos.getObjectUrl({
Bucket: 'examplebucket-1250000000',
Region: 'ap-beijing',
Key: 'picture.jpg',
Sign: true
}, function (err, data) {
if (!err) return console.log(err);
wx.downloadFile({
url: data.Url, // The “url” domain name needs to be added to the download allowlist
success (res) {
console.log(res.statusCode, res.tempFilePath);
},
fail: function (err) {
console.log(err);
},
});
});
Parameter Name | Description | Type | Required |
---|---|---|---|
Bucket | Bucket name in the format of BucketName-APPID |
String | Yes |
Region | Bucket region. For the enumerated values, see Regions and Access Domain Names | String | Yes |
Key | Object key (object name), a unique ID of an object in a bucket. |
String | Yes |
Sign | Whether to return a signed URL | Boolean | No |
Method | HTTP operation method such as GET , POST , DELETE , and HEAD . Default value: GET |
String | No |
Query | Query parameter object used in the signature calculation | Object | No |
Headers | Header parameter object used in the signature calculation | Object | No |
Expires | Validity period of the signature; unit: second. Default value: 900 | Number | No |
The returned value is a string. There are two possible scenarios:
function(err, data) { ... }
Parameter Name | Description | Type |
---|---|---|
err | Object returned when an error, a network error or service error, occurs. If the request is successful, this parameter will be empty. For more information, see Error Codes | Object |
data | Object returned when the request succeeds. If the request fails, this will be empty | Object |
- Url | Calculated URL | String |
Was this page helpful?