Feature Overview
This document provides an overview of the API and sample code for getting an object access URL.
Getting an Object Access URL
Note
This API is used to query the URL to access an object. This API does not verify whether the object exists or not.
Note
To make the generated object URL a preview URL instead of a download URL, concatenate the
response-content-disposition=inline parameter to the end of the obtained URL.To make the generated object URL a download URL instead of a preview URL, concatenate the
response-content-disposition=attachment parameter to the end of the obtained URL.To rename a file during download, append the "filename" parameter to the obtained URL, such as response-content-disposition=attachment;filename=custom_filename.
If you are using temporary keys to generate pre-signed URLs, make sure the action for requesting temporary keys includes the
"name/cos:GetObject" permission.Use Cases
Getting the download URL:
cos.getObjectUrl({Bucket: 'examplebucket-1250000000', // Enter your own storage bucket, required fieldRegion: 'COS_REGION', // The region where the bucket is located, e.g., ap-beijing, required fieldKey: 'avatar.jpg', // The object key stored in the bucket (e.g., 1.jpg, a/b/test.txt), supports Chinese characters, required field.Sign: true, // Get a signed object URL},function (err, data) {if (err) return console.log(err);/* url is the object access URL */var url = data.Url;/* Copy the value of downloadUrl and open it in a browser to automatically trigger the download */var downloadUrl =url +(url.indexOf('?') > -1 ? '&' : '?') +'response-content-disposition=attachment;filename=Image.jpg'; // Add parameters to force download and rename the downloaded file});
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), the unique identifier of an object in a bucket. For more information, see Object Overview. | String | Required |
Sign | Whether to return a signed URL. Default value: true. If the object is configured with the private read permission, you still do not have the access permission after you obtain the unsigned URL. | 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 |
Callback function description
function(err, data) { ... }
Parameter | ParameterDescription | Local Disk Types |
err | Object 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 |