File Storage
getUploadMetaData
1. API Description
Function: Get File Upload Attributes
2. Request URL
POST https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:getUploadMetaData
3. Request Body
Field | Type | Required | Description |
---|---|---|---|
path | String | Yes | Specified cloud file path |
4. Response Body
Field | Type | Required | Description |
---|---|---|---|
statusCode | Number | Required | Status code, 200 |
body | Object | Required | Response body, structure as follows |
body
Field | Type | Required | Description |
---|---|---|---|
requestId | String | No | Request ID |
data | Object | No | Return result |
code | String | No | Error code |
message | String | No | Error message |
data
Field | Type | Required | Description |
---|---|---|---|
url | String | Required | Upload URL |
token | String | Required | Token for upload requests |
authorization | String | Required | Authorization field for upload requests |
fileID | String | Required | File ID |
cosFileID | String | Required | cos file ID |
5. Usage Example
// Node Example
const axios = require("axios"); // Request library, requires npm installation of dependencies
const FormData = require('form-data'); // Form library used to construct form data for HTTP requests
const envId = "<envId>"; // Environment ID
const cloudPath = "<cloudPath>" // Cloud path
const fs = require('fs')
const fileStream = fs.createReadStream('<localFile>') // Local file
axios({
url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:getUploadMetaData`,
method: "POST",
headers: {
"X-CloudBase-Authorization": "your authorization",
"X-CloudBase-SessionToken": "your token",
"X-CloudBase-TimeStamp": "the timestamp",
},
data: {
path: cloudPath,
},
}).then(response => {
// After obtaining the metadata, use the information within the metadata to upload files to cloud storage
const { authorization, token, cosFileID, url } = response.data.data;
const form = new FormData();
form.append("Signature", authorization);
form.append("x-cos-security-token", token);
form.append("x-cos-meta-fileid", cosFileID);
form.append("key", cloudPath);
form.append("file", fileStream);
return axios.post(url, form, { headers: form.getHeaders() });
});
batchGetTempUrls
1. API Description
Function: Get file download URL
2. Request URL
POST https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:batchGetTempUrls
3. Request Body
Field | Type | Required | Description |
---|---|---|---|
fileList | Array<FileItem> | Required | File information array |
FileItem
Field | Type | Required | Description |
---|---|---|---|
fileID | String | Required | File ID |
maxAge | Number | Optional | Validity period of the file link |
4. Response Body
Field | Type | Required | Description |
---|---|---|---|
statusCode | Number | Required | Status code, 200 |
body | Object | Required | Response body, structure as follows |
body
Field | Type | Required | Description |
---|---|---|---|
requestId | String | No | Request ID |
data | Object | No | Return result |
code | String | No | Error code |
message | String | No | Error message |
data
Field | Type | Required | Description |
---|---|---|---|
fileList | Array<DownloadFileItem> | Required | List of file download information |
DownloadFileItem
Field | Type | Required | Description |
---|---|---|---|
fileID | String | Required | Download file ID |
code | String | Required | 'SUCCESS' represents success |
tempFileURL | String | Required | File download link |
5. Usage Example
// Node Example
const request = require("request"); // Request library, requires npm installation of dependencies
const envId = "testEnv"; // Environment ID
const { parseString } = require("xml2js"); // Parses xml files to json, requires npm installation of dependencies
async function parseXML(str) {
return new Promise((resolve, reject) => {
parseString(str, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
async function test() {
// 1. Upload files
const fileContent = fs.createReadStream(
path.resolve(__dirname, "./my-photo.png")
); // Get local file content
const cloudPath = "cloud-my-photo.png"; // Defined cloud file path
const getUploadMetaDataPath = `/envs/${envId}/storages:getUploadMetaData`;
const getUploadMetaDataRes = await new Promise((resolve, reject) => {
request(
{
url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:getUploadMetaData`,
method: "POST",
headers: {
"X-CloudBase-Authorization": "your authorization",
"X-CloudBase-SessionToken": "your token",
"X-CloudBase-TimeStamp": "the timestamp",
},
body: {
data: {
path: cloudPath,
},
},
json: true,
},
(err, response, body) => {
console.log(err);
console.log(response.statusCode);
console.log(response.body.data);
resolve(response.body);
}
);
});
console.log("getUploadMetaDataRes:", getUploadMetaDataRes);
const {
data: { url, token, authorization, fileID, cosFileID },
} = getUploadMetaDataRes;
const formData = {
Signature: authorization,
"x-cos-security-token": token,
"x-cos-meta-fileid": cosFileID,
key: cloudPath,
file: fileContent,
};
let body: any = await new Promise((resolve, reject) => {
request(
{ url, formData: formData, method: "post" },
function (err, res, body) {
if (err) {
reject(err);
} else {
resolve(body);
}
}
);
});
body = await parseXML(body);
if (body && body.Error) {
throw body.Error;
}
// 2. Get the download URL
const batchGetDownloadUrlPath = await new Promise((resolve, reject) => {
request(
{
url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:batchGetTempUrls`,
method: "POST",
headers: {
"X-CloudBase-Authorization": "your authorization",
"X-CloudBase-SessionToken": "your token",
"X-CloudBase-TimeStamp": "the timestamp",
},
body: {
data: {
fileList: [{ fileID: fileID }],
},
},
json: true,
},
(err, response, body) => {
console.log(err);
console.log(response.statusCode);
console.log(response.body.data);
resolve(response.body);
}
);
});
console.log("batchGetDownloadUrlRes", batchGetDownloadUrlRes.data);
}
test();
batchDelete
1. API Description
Function: Batch delete files
2. Request URL
POST https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:batchDelete
3. Request Body
Field | Type | Required | Description |
---|---|---|---|
fileList | Array<String> | Required | File ID array |
4. Response Body
Field | Type | Required | Description |
---|---|---|---|
statusCode | Number | Required | Status code, 200 |
body | Object | Required | Response body, structure as follows |
body
Field | Type | Required | Description |
---|---|---|---|
requestId | String | No | Request ID |
data | Object | No | Return result |
code | String | No | Error code |
message | String | No | Error message |
data
Field | Type | Required | Description |
---|---|---|---|
fileList | Array<DeleteFileItem> | Required | List of files to delete |
DeleteFileItem
Field | Type | Required | Description |
---|---|---|---|
fileID | String | Required | File ID to delete |
code | String | Required | 'SUCCESS' represents success |
5. Usage Example
// Node Example
const request = require("request"); // Request library, requires npm installation of dependencies
const envId = "testEnv"; // Environment ID
const { parseString } = require("xml2js"); // Parses xml files to json, requires npm installation of dependencies
async function parseXML(str) {
return new Promise((resolve, reject) => {
parseString(str, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
async function test() {
// 1. Upload files
const fileContent = fs.createReadStream(
path.resolve(__dirname, "./my-photo.png")
); // Get local file content
const cloudPath = "cloud-my-photo.png"; // Defined cloud file path
const getUploadMetaDataPath = `/envs/${envId}/storages:getUploadMetaData`;
const getUploadMetaDataRes = await new Promise((resolve, reject) => {
request(
{
url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:getUploadMetaData`,
method: "POST",
headers: {
"X-CloudBase-Authorization": "your authorization",
"X-CloudBase-SessionToken": "your token",
"X-CloudBase-TimeStamp": "the timestamp",
},
body: {
data: {
path: cloudPath,
},
},
json: true,
},
(err, response, body) => {
console.log(err);
console.log(response.statusCode);
console.log(response.body.data);
resolve(response.body);
}
);
});
console.log("getUploadMetaDataRes:", getUploadMetaDataRes);
const {
data: { url, token, authorization, fileID, cosFileID },
} = getUploadMetaDataRes;
const formData = {
Signature: authorization,
"x-cos-security-token": token,
"x-cos-meta-fileid": cosFileID,
key: cloudPath,
file: fileContent,
};
let body: any = await new Promise((resolve, reject) => {
request(
{ url, formData: formData, method: "post" },
function (err, res, body) {
if (err) {
reject(err);
} else {
resolve(body);
}
}
);
});
body = await parseXML(body);
if (body && body.Error) {
throw body.Error;
}
// 2. Get the download URL
const batchGetDownloadUrlPath = await new Promise((resolve, reject) => {
request(
{
url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:batchGetTempUrls`,
method: "POST",
headers: {
"X-CloudBase-Authorization": "your authorization",
"X-CloudBase-SessionToken": "your token",
"X-CloudBase-TimeStamp": "the timestamp",
},
body: {
data: {
fileList: [{ fileID: fileID }],
},
},
json: true,
},
(err, response, body) => {
console.log(err);
console.log(response.statusCode);
console.log(response.body.data);
resolve(response.body);
}
);
});
console.log("batchGetDownloadUrlRes", batchGetDownloadUrlRes.data);
// 3. Delete files
const batchDeleteRes = await new Promise((resolve, reject) => {
request(
{
url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:batchDelete`,
method: "POST",
headers: {
"X-CloudBase-Authorization": "your authorization",
"X-CloudBase-SessionToken": "your token",
"X-CloudBase-TimeStamp": "the timestamp",
},
body: {
data: {
fileList: [fileID],
},
},
json: true,
},
(err, response, body) => {
console.log(err);
console.log(response.statusCode);
console.log(response.body.data);
resolve(response.body);
}
);
});
console.log("batchDeleteRes", batchDeleteRes.data);
}
test();
batchCopyFile
1. API Description
Function: Batch copy files, which can achieve the effect of moving files by setting parameters
2. Request URL
POST https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:batchCopyFile
3. Request Body
Field | Type | Required | Description |
---|---|---|---|
fileList | Array<FileItem> | Required | Copied files list |
FileItem
Field | Type | Required | Description |
---|---|---|---|
srcPath | string | Required | The absolute path of the source file, including the file name. Examples: foo/bar.jpg, foo/bar/baz.jpg. Only allowed characters: [0-9, a-z, A-Z], /, !, -, _, ., ,, *, and Chinese. Use / to implement hierarchical structure like traditional file systems |
dstPath | string | Required | The absolute path of the destination file, including the file name. Examples: foo/bar.jpg, foo/bar/baz.jpg. Only allowed characters: [0-9, a-z, A-Z], /, !, -, _, ., ,, *, and Chinese. Use / to implement hierarchical structure like traditional file systems |
overwrite | boolean | Optional | Whether to overwrite existing files when the target file already exists. Defaults to true |
removeOriginal | boolean | Optional | Whether to delete source files after copying, defaults to false |
4. Response Body
Field | Type | Required | Description |
---|---|---|---|
statusCode | Number | Required | HTTP status code |
body | Object | Required | Response body, structure as follows |
body
Field | Type | Required | Description |
---|---|---|---|
requestId | String | No | Request ID |
data | Object | No | Return result |
code | String | No | Error code |
message | String | No | Error message |
data
Field | Type | Required | Description |
---|---|---|---|
fileList | Array<CopyFileItem> | Required | Copied files list |
CopyFileItem
Field | Type | Required | Description |
---|---|---|---|
fileID | String | No | Copied File ID |
code | String | No | Error code for copy failure |
message | String | No | Error message on copy failure |
5. Usage Example
const request = require("request") // Request library, requires npm installation of dependencies
async function main() {
const result = await new Promise((resolve, reject) => {
request(
{
url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:batchCopyFile`, // Request URL
method: "POST",
headers: {
// Add credentials to headers
"X-CloudBase-Authorization": "your auth",
"X-CloudBase-SessionToken": "your token",
"X-CloudBase-TimeStamp": Math.floor(new Date().getTime() / 1000),
},
body: {
// Copy file list
fileList: [
{
srcPath: 'filename.jpg', // Source file path
dstPath: 'target/filename.jpg' // Target file path
}
]
},
json: true,
},
(err, response) => {
if (err) {
reject(err)
}
resolve(response.body)
}
)
})
console.log(result) // Print the response result
}
main()