tencent cloud

Cloud Object Storage

Generate a Pre-Signed URL

PDF
Modo Foco
Tamanho da Fonte
Última atualização: 2026-02-02 17:32:43

Introduction

This document provides sample code on generating presigned URLs for objects.
For instructions on uploading with a presigned URL, see Uploading with Presigned URLs. For instructions on downloading with a presigned URL, see Downloading with Presigned URLs.
Note
It is recommended that users use temporary keys to generate presigned URLs, further enhancing the security of presigned upload, download, and other requests through temporary authorization. When applying for temporary keys, please follow the principle of least privilege to prevent leakage of resources beyond the target bucket or object.
If you must use permanent keys to generate presigned URLs, it is recommended to limit the scope of permissions to upload or download operations only to mitigate risks.

Generate Object Presigned URL

Sample Code 1: Generate Presigned Upload URL

// Bucket name, which consists of bucketname-appid (appid must be included), can be viewed in the COS console. https://console.tencentcloud.com/cos5/bucket
let bucket = "examplebucket-1250000000";
//The location identifier of an object in a bucket, also known as the object key
let cosPath = "exampleobject.txt";

let request = new PresignedUrlRequest(bucket, cosPath, http.RequestMethod.PUT); try { let result = await CosXmlBaseService.default().buildPresignedUrl(request) // result is the signed upload URL
if(result){
// Upload data let httpRequest = http.createHttp(); try { let data = await httpRequest.request(result, { method: http.RequestMethod.PUT, extraData: 'data to put' }); httpRequest.destroy(); } catch (err) {
httpRequest.destroy(); // Upload exception handling } } } catch (e) { // Exception handling }

Sample Code 2: Generate Presigned Download URL

// Bucket name, which consists of bucketname-appid (appid must be included), can be viewed in the COS console. https://console.tencentcloud.com/cos5/bucket
let bucket = "examplebucket-1250000000";
//The location identifier of an object in a bucket, also known as the object key
let cosPath = "exampleobject.txt";

let request = new PresignedUrlRequest(bucket, cosPath, http.RequestMethod.GET); try { let result = await CosXmlBaseService.default().buildPresignedUrl(request) // result is the signed download URL
if(result){
// Download data let httpRequest = http.createHttp(); try { let data = await httpRequest.request(result, { method: http.RequestMethod.GET }); httpRequest.destroy(); } catch (err) {
httpRequest.destroy(); // Download exception handling } } } catch (e) { // Exception handling }


Ajuda e Suporte

Esta página foi útil?

comentários