- Documentation
Torch Object Storage
Best Practices
Uploading data from WeChat Mini Programs to TOS
Best Practices
Uploading data from WeChat Mini Programs to TOS
Uploading data from WeChat Mini Programs to TOS
This topic describes how to upload files directly to TOS from WeChat Mini Programs.
Background information
WeChat Mini Programs, which can be used without downloading, are attracting a growing number of developers and users. One common concern for developers is how to upload files from a WeChat Mini Program to TOS. You can create a pre-signed post form and use the PostObject API to upload files from a WeChat Mini Program to TOS as an HTML form. For more information, see PostObject and Pre-signed Post Form. Step 1: Create a CORS rule
When data is uploaded from a WeChat Mini Program, a cross-origin request is sent to TOS. TOS then verifies permissions by checking the bucket’s cross-origin resource sharing (CORS) rules. To ensure that WeChat Mini Programs can transfer data to TOS correctly, you must set up CORS rules in TOS. For more information, see CORS rules. - In the left navigation pane, click Buckets. In the Buckets list, click the bucket you want to manage.
- In the left navigation pane, select Permissions > CORS rules.
- On the CORS rules page, click Create rule.
- On the Create CORS Rule page, configure the following parameters:
-
Step 2: Add your domain to the allowlist
To upload files from a WeChat Mini Program, a domain name must be added to its allowlist. Configure the allowlist in your WeChat Mini Program settings to include your TOS bucket's domain. This will allow normal interaction between your Mini Program and your bucket.
tip
We recommend associating your bucket with your own domain name so that you can access the objects stored in TOS via your domain. For instructions, see Associating a custom domain. - Obtain the bucket domain.
- In the left navigation pane, click Buckets. In the Buckets list, click the bucket you want to manage.
- In the left navigation pane, click Overview.
- On the Overview page, navigate to the Access domain area. In the Bucket domain column, find the corresponding domain name for Public Network Access.
- Configure the allowlist.
- Log in to the WeChat Mini Program management console.
- On the Mini Program page, locate the Server Domain Names section and click Start Configuration.
- In the Configure Server Domain Names dialog, enter your bucket's domain name in the uploadFile Valid Domain and downloadFile Valid Domain fields.
Step 3: Upload data to TOS
This JavaScript SDK sample demonstrates how to generate a signature using the calculatePostSignature method for a TOS PostObject request. Then, it shows how to use the WeChat Mini Program's uploadFile API to upload the file. The example code is shown below.
import { TosClient } from '@volcengine/tos-sdk'
async function wxUploadToTOS() {
const client = new TosClient({
accessKeyId: "Provide your access key",
accessKeySecret: "Provide your secret key",
stsToken: "Provide your session token",
region: "Provide your region",
const res = await wx.chooseMessageFile({ count: 1 });
const path = res.tempFiles[0].path;
const bucket = 'test-bucket';
const key = 'wx-miniprogram-test';
For more information on pre-signed post form, refer to Browser-based uploads using HTTP POST.
const formData = await client.calculatePostSignature({
const bucketHost = `${bucket}.tos-cn-beijing.volces.com`;
const url = `https://${bucketHost}/${encodeURI(key)}`
const result = await wx.uploadFile({
if (res.statusCode === 204) {
console.log('Successfully Uploaded');
console.log(result, 'result');
Replace the following parameters in the sample code with the appropriate values for your situation.
Last updated: 2025.11.18 14:49:32