tencent cloud

Cloud Object Storage

Release Notes and Announcements
Release Notes
Announcements
Product Introduction
Overview
Features
Use Cases
Strengths
Concepts
Regions and Access Endpoints
Specifications and Limits
Service Regions and Service Providers
Billing
Billing Overview
Billing Method
Billable Items
Free Tier
Billing Examples
Viewing and Downloading Bill
Payment Overdue
FAQs
Getting Started
Console
Getting Started with COSBrowser
User Guide
Creating Request
Bucket
Object
Data Management
Batch Operation
Global Acceleration
Monitoring and Alarms
Operations Center
Data Processing
Content Moderation
Smart Toolbox
Data Processing Workflow
Application Integration
User Tools
Tool Overview
Installation and Configuration of Environment
COSBrowser
COSCLI (Beta)
COSCMD
COS Migration
FTP Server
Hadoop
COSDistCp
HDFS TO COS
GooseFS-Lite
Online Tools
Diagnostic Tool
Use Cases
Overview
Access Control and Permission Management
Performance Optimization
Accessing COS with AWS S3 SDK
Data Disaster Recovery and Backup
Domain Name Management Practice
Image Processing
Audio/Video Practices
Workflow
Direct Data Upload
Content Moderation
Data Security
Data Verification
Big Data Practice
COS Cost Optimization Solutions
Using COS in the Third-party Applications
Migration Guide
Migrating Local Data to COS
Migrating Data from Third-Party Cloud Storage Service to COS
Migrating Data from URL to COS
Migrating Data Within COS
Migrating Data Between HDFS and COS
Data Lake Storage
Cloud Native Datalake Storage
Metadata Accelerator
GooseFS
Data Processing
Data Processing Overview
Image Processing
Media Processing
Content Moderation
File Processing Service
File Preview
Troubleshooting
Obtaining RequestId
Slow Upload over Public Network
403 Error for COS Access
Resource Access Error
POST Object Common Exceptions
API Documentation
Introduction
Common Request Headers
Common Response Headers
Error Codes
Request Signature
Action List
Service APIs
Bucket APIs
Object APIs
Batch Operation APIs
Data Processing APIs
Job and Workflow
Content Moderation APIs
Cloud Antivirus API
SDK Documentation
SDK Overview
Preparations
Android SDK
C SDK
C++ SDK
.NET(C#) SDK
Flutter SDK
Go SDK
iOS SDK
Java SDK
JavaScript SDK
Node.js SDK
PHP SDK
Python SDK
React Native SDK
Mini Program SDK
Error Codes
Harmony SDK
Endpoint SDK Quality Optimization
Security and Compliance
Data Disaster Recovery
Data Security
Cloud Access Management
FAQs
Popular Questions
General
Billing
Domain Name Compliance Issues
Bucket Configuration
Domain Names and CDN
Object Operations
Logging and Monitoring
Permission Management
Data Processing
Data Security
Pre-signed URL Issues
SDKs
Tools
APIs
Agreements
Service Level Agreement
Privacy Policy
Data Processing And Security Agreement
Contact Us
Glossary

Getting Pre-Signed URLs

PDF
Focus Mode
Font Size
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());
}


Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback