You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Granting temporary access
Using pre-signed URLs for secure data downloads
Copy page
Download PDF
Using pre-signed URLs for secure data downloads
You can securely upload and download data to and from TOS using pre-signed URLs. This topic provides essential information and instructions for using pre-signed URLs to access TOS.​
Use cases​
Applications such as video surveillance, mobile apps, and web platforms often require uploading or downloading files, such as images, documents, and videos, to or from the server. In these scenarios, you can use TOS's HTTP capabilities for cost-effective and efficient transfer data. Meanwhile, the serverless nature of TOS allows it to adapt seamlessly to business growth or contraction, providing flexible scalability to meet changing demands.​
Solution approach​
Roles involved​
  • Client: Controlled by the backend server. It facilitates interaction between humans or machines within the final environment and performs data uploads or downloads according to different workflows.​
  • Application server: The brain of the entire business system, responsible for client management and access control. In this scenario, its primary function is to generate pre-signed URLs.​
  • TOS: A key-value storage system that provides HTTP interfaces.​
  • IAM: Provides access control for accounts and users in public cloud services.​
How it works​
  1. First, an application server creates an IAM user and grants this user permission to access the TOS bucket and its objects. Then, it obtains the user's access key ID and secret access key.​
  1. When a client requests a temporary URL from the application server, the server generates a pre-signed URL valid for a period determined by the upload or download. Then, the application server logs the uploaded or downloaded objects in the database for future reference.​
  1. After obtaining the pre-signed URL, the client uses it to interact with TOS and perform upload or download operations.​
  1. When a request to upload or download is received, the TOS server interacts with IAM to obtain the corresponding keys. The server then uses these keys to calculate the signature and compares it with the client’s request to verify its legitimacy. If the request is valid, the server proceeds with it.​
  1. The client receives the results, marking the conclusion of the process.​
Advantages and disadvantages​
The advantages and disadvantages of using pre-signed URLs for uploads and downloads are as follows:​
​
Category​
Description​
Advantages​
  1. Pre-signed URLs provide object-level signature authentication, ensuring data security.​
  1. Since clients only need to support HTTP requests, development is simple and quick.​
  1. Clients do not need to accommodate different object storage interfaces from various cloud providers, enabling seamless transitions between providers.​
Disadvantages​
  1. Each request requires server-side signing. As the number of clients grows, the server load will also increase.​
  1. Concurrent uploads present a challenge, which makes uploading large files less efficient.​
​
Instructions​
Prerequisites​
  • You have created an IAM user, granted it the necessary permissions, and obtained its access key ID and secret access key. Refer to the following documentation for instructions:​
  • Create a TOS bucket and organize your data by creating folders as needed. Refer to the following documentation for instructions:​
Steps​
Step 1: Use the appropriate SDK to generate a pre-signed URL​
  1. Choose an SDK that aligns with your programming language and meets your business needs.​
  • To accelerate development, choose a TOS SDK in a language that matches your application server architecture design.​
  • For a list of SDK languages supported by TOS, see SDK overview.​
  1. Develop and integrate the business code.​
  • This section uses Go as an example to show how to generate a pre-signed URL for downloading objects.​
tip​
For code examples of generating pre-signed URLs in other languages, see SDK overview.​
​
package main​
​
import (​
"context""fmt""github.com/volcengine/ve-tos-golang-sdk/v2/tos"​
)​
func checkErr(err error) {​
if err != nil {​
if ServerErr, ok := err.(*tos.TosServerError); ok {​
fmt.Println("Error:", ServerErr.Error())​
fmt.Println("Request ID:", ServerErr.RequestID)​
fmt.Println("Response Status Code:", ServerErr.StatusCode)​
fmt.Println("Response Header:", ServerErr.Header)​
fmt.Println("Response Err Code:", ServerErr.Code)​
fmt.Println("Response Err Msg:", ServerErr.Message)​
} else if clientErr, ok := err.(*tos.TosClientError); ok {​
fmt.Println("Error:", clientErr.Error())​
fmt.Println("Client Cause Err:", clientErr.Cause.Error())​
} else {​
fmt.Println("Error:", err)​
}​
panic(err)​
}​
}​
​
func main() {​
var (​
accessKey = "*** Provide your access key ***"​
secretKey = "*** Provide your secret key ***"// Provide the endpoint of your bucket, e.g., https://tos-cn-beijing.volces.com (for China (Beijing))​
endpoint = "https://tos-cn-beijing.volces.com"​
region = "cn-beijing"// Enter your RegionID​
bucketName = "*** Provide your bucket name ***"​
objectKey = "example_dir/example.txt" ​
ctx = context.Background()​
// Full path of the local file, e.g., usr/local/testfile.txt​
fileName = "/usr/local/testfile.txt"​
)​
// Initialize client​
client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))​
checkErr(err)​
// Set request method, bucket object name, expiry time​
respTempUrl,errURL:=client.PreSignedURL(&tos.PreSignedURLInput{​
HTTPMethod: enum.HttpMethodGet,​
Bucket: bucketName,​
Key: objectKey,​
Expires: 3600, // 3,600 seconds​
})​
​
checkErr(errURL)​
fmt.Println(respTempUrl.SignedUrl)​
}​
​
Step 2: Use the pre-signed URL to send a GET request​
This section uses Browser.js code to show how to send requests from the client.​
warning​
When developing on a web platform, create a cross-origin resource sharing (CORS) rule. For instructions, see CORS rules.​
​
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>​
<script>​
$(document).ready(function(){​
const Url='https://jsonplaceholder.typicode.com/posts'​
$('.btn').click(function(){​
$.ajax({​
url: Url,​
type: "GET",​
success: function(result){​
console.log(result)​
},​
error:function(error){​
console.log(`Error ${error}`)​
}​
})​
})​
})​
</script>​
​
​
Last updated: 2026.10.12 14:16:23