tencent cloud

All product documents
Cloud Object Storage
Getting Pre-Signed URLs
Last updated: 2024-02-02 14:36:36
Getting Pre-Signed URLs
Last updated: 2024-02-02 14:36:36

Overview

This document provides an overview of SDK code samples related to generating pre-signed object URLs.
Note:
You are advised to use a temporary key to generate a pre-signed URL for the security of your requests such as uploads and downloads. When you apply for a temporary key, follow the Principle of Least Privilege to avoid leaking resources besides your buckets and objects.
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.
For more information about how to get a temporary key, please see Generating and Using Temporary Keys.

Generating a Pre-Signed Object URL

Sample code 1. Generate a pre-signed URL

try
{
PreSignatureStruct preSignatureStruct = new PreSignatureStruct();
// For details about how to get the APPID, visit https://console.tencentcloud.com/developer.
preSignatureStruct.appid = "1250000000";
// Bucket region. For the abbreviations of COS regions, visit https://www.tencentcloud.com/zh/document/product/436/6224.
preSignatureStruct.region = "COS_REGION";
// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.
preSignatureStruct.bucket = "examplebucket-1250000000";
preSignatureStruct.key = "exampleobject"; // Object key
preSignatureStruct.httpMethod = "GET"; // HTTP request method
preSignatureStruct.isHttps = true; // Generate an HTTPS request URL
preSignatureStruct.signDurationSecond = 600; // Request signature duration is 600s
preSignatureStruct.headers = null;// Header in the signature that needs to be verified
preSignatureStruct.queryParameters = null; // URL request parameters in the signature that need to be verified

string requestSignURL = cosXml.GenerateSignURL(preSignatureStruct);

// Pre-signed URL for download request (signed URL calculated with the permanent key method)
string localDir = System.IO.Path.GetTempPath();// Local file directory
string localFileName = "my-local-temp-file"; // Filename of the local file
GetObjectRequest request = new GetObjectRequest(null, null, localDir, localFileName);
// Set the pre-signed URL for download requests
request.RequestURLWithSign = requestSignURL;
// Set the progress callback
request.SetCosProgressCallback(delegate (long completed, long total)
{
Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
});
// Execute the request
GetObjectResult result = cosXml.GetObject(request);
// Request succeeded
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
// Request failed
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
// Request failed
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
Note:
For the complete sample, go to GitHub.

Sample code 2. Generate a pre-signed upload URL

try
{
PreSignatureStruct preSignatureStruct = new PreSignatureStruct();
// For details about how to get the APPID, visit https://console.tencentcloud.com/developer.
preSignatureStruct.appid = "1250000000";
// Bucket region. For the abbreviations of COS regions, visit https://www.tencentcloud.com/zh/document/product/436/6224.
preSignatureStruct.region = "COS_REGION";
// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.
preSignatureStruct.bucket = "examplebucket-1250000000";
preSignatureStruct.key = "exampleobject"; // Object key
preSignatureStruct.httpMethod = "PUT"; // HTTP request method
preSignatureStruct.isHttps = true; // Generate an HTTPS request URL
preSignatureStruct.signDurationSecond = 600; // Request signature duration is 600s
preSignatureStruct.headers = null;// Header in the signature that needs to be verified
preSignatureStruct.queryParameters = null; // URL request parameters in the signature that need to be verified

// Pre-signed URL for upload (signed URL calculated with the permanent key method)
string requestSignURL = cosXml.GenerateSignURL(preSignatureStruct);

string srcPath = @"temp-source-file";// Absolute path of the local file
PutObjectRequest request = new PutObjectRequest(null, null, srcPath);
// Set the pre-signed URL for the upload request
request.RequestURLWithSign = requestSignURL;
// Set the progress callback
request.SetCosProgressCallback(delegate (long completed, long total)
{
Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
});
// Execute the request
PutObjectResult result = cosXml.PutObject(request);
// Request succeeded
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
// Request failed
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
// Request failed
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
Note:
For the complete sample, go to GitHub.

Sample code 3. Generate a pre-signed URL with Host in the signature

try
{
PreSignatureStruct preSignatureStruct = new PreSignatureStruct();
// For details about how to get the APPID, visit https://console.tencentcloud.com/developer.
preSignatureStruct.appid = "1250000000";
// Bucket region. For the abbreviations of COS regions, visit https://www.tencentcloud.com/zh/document/product/436/6224.
preSignatureStruct.region = "COS_REGION";
// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.
preSignatureStruct.bucket = "examplebucket-1250000000";
preSignatureStruct.key = "exampleobject"; // Object key
preSignatureStruct.httpMethod = "GET"; // HTTP request method
preSignatureStruct.isHttps = true; // Generate an HTTPS request URL
preSignatureStruct.signDurationSecond = 600; // Request signature duration is 600s
preSignatureStruct.signHost = true; // Whether to include `Host` in the request signature. It is recommended to include `Host` in the signature to avoid unauthorized requests. Note that if `Host` is included in the signature, the `Host` request header must also be carried in the actual request.
preSignatureStruct.headers = null;// Header in the signature that needs to be verified
preSignatureStruct.queryParameters = null; // URL request parameters in the signature that need to be verified

string requestSignURL = cosXml.GenerateSignURL(preSignatureStruct);
Console.WriteLine("requestUrl is:" + requestSignURL);

// Pre-signed URL for download request (signed URL calculated with the permanent key method)
string localDir = System.IO.Path.GetTempPath();// Local file directory
string localFileName = "my-local-temp-file"; // Filename of the local file
GetObjectRequest request = new GetObjectRequest(null, null, localDir, localFileName);
// Set the pre-signed URL for download requests
request.RequestURLWithSign = requestSignURL;
// Set the progress callback
request.SetCosProgressCallback(delegate (long completed, long total)
{
Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
});
// Execute the request
GetObjectResult result = cosXml.GetObject(request);
// Request succeeded
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
// Request failed
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
// Request failed
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}

Sample code 4. Generate a pre-signed URL with request parameters in the signature

try
{
PreSignatureStruct preSignatureStruct = new PreSignatureStruct();
// For details about how to get the APPID, visit https://console.tencentcloud.com/developer.
preSignatureStruct.appid = "1250000000";
// Bucket region. For the abbreviations of COS regions, visit https://www.tencentcloud.com/zh/document/product/436/6224.
preSignatureStruct.region = "COS_REGION";
// Bucket name in the format of `BucketName-APPID`. You can get APPID by referring to https://console.tencentcloud.com/developer.
preSignatureStruct.bucket = "examplebucket-1250000000";
preSignatureStruct.key = "exampleobject"; // Object key
preSignatureStruct.httpMethod = "GET"; // HTTP request method
preSignatureStruct.isHttps = true; // Generate an HTTPS request URL
preSignatureStruct.signDurationSecond = 600; // Request signature duration is 600s
preSignatureStruct.signHost = true; // Whether to include `Host` in the request signature. It is recommended to include `Host` in the signature to avoid unauthorized requests. Note that if `Host` is included in the signature, the `Host` request header must also be carried in the actual request.
preSignatureStruct.headers = null;// Headers in the signature that need to be verified
string ci_params = "imageMogr2/thumbnail/!50p";
preSignatureStruct.queryParameters = new Dictionary<string, string>(); // Request parameters in the URL in the signature that need to be verified. Take a CI image processing request as an example.
preSignatureStruct.queryParameters.Add(ci_params, null);

string requestSignURL = cosXml.GenerateSignURL(preSignatureStruct);
Console.WriteLine("requestUrl is:" + requestSignURL);

// Pre-signed URL for download request (signed URL calculated with the permanent key method)
string localDir = System.IO.Path.GetTempPath();// Local file directory
string localFileName = "my-local-temp-file"; // Filename of the local file
GetObjectRequest request = new GetObjectRequest(null, null, localDir, localFileName);
// Set the pre-signed URL for download requests
request.RequestURLWithSign = requestSignURL;
// Set the progress callback
request.SetCosProgressCallback(delegate (long completed, long total)
{
Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
});
// Execute the request
GetObjectResult result = cosXml.GetObject(request);
// Request succeeded
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
// Request failed
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
// Request failed
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon