跳到主要内容

文件存储

getUploadMetaData

1. API 描述

功能:获取文件上传属性

2. 请求地址

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:getUploadMetaData

3. 请求体

字段类型必填说明
pathString指定的云端文件路径

4. 响应体

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data
字段类型必填说明
urlString上传 url
tokenStringtoken 字段,上传请求用
authorizationStringauthorization 字段,上传请求用
fileIDString文件 ID
cosFileIDStringcos 文件 ID

5. 使用示例

// Node 示例
const axios = require("axios"); // 请求库,需npm 安装依赖
const FormData = require('form-data'); // 表单库,用于构造HTTP请求的表单数据
const envId = "<envId>"; // 环境ID
const cloudPath = "<cloudPath>" // 云端路径

const fs = require('fs')
const fileStream = fs.createReadStream('<localFile>') // 本地文件

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 => {
// 获取到元数据后,使用元数据内的信息,上传文件到云存储
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 描述

功能:获取文件下载链接

2. 请求地址

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:batchGetTempUrls

3. 请求体

字段类型必填说明
fileListArray<FileItem>文件信息数组

FileItem

字段类型必填说明
fileIDString文件 ID
maxAgeNumber文件链接有效期

4. 响应体

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data
字段类型必填说明
fileListArray<DownloadFileItem>文件下载信息列表

DownloadFileItem

字段类型必填说明
fileIDString下载的文件 ID
codeStringSUCCESS 表示成功
tempFileURLString文件下载链接

5. 使用示例

// Node 示例
const request = require("request"); // 请求库,需npm 安装依赖
const envId = "testEnv"; // 环境ID
const { parseString } = require("xml2js"); // 解析 xml 文件 到 json 文件库, 需npm安装依赖

async function parseXML(str) {
return new Promise((resolve, reject) => {
parseString(str, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}

async function test() {
// 1. 上传文件
const fileContent = fs.createReadStream(
path.resolve(__dirname, "./my-photo.png")
); // 获取本地的文件内容
const cloudPath = "cloud-my-photo.png"; // 定义的云端文件路径
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. 获取下载链接
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 描述

功能:批量删除文件

2. 请求地址

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:batchDelete

3. 请求体

字段类型必填说明
fileListArray<String>文件 ID 数组

4. 响应体

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data
字段类型必填说明
fileListArray<DeleteFileItem>删除文件信息列表

DeleteFileItem

字段类型必填说明
fileIDString删除的文件 ID
codeStringSUCCESS 表示成功

5. 使用示例

// Node 示例
const request = require("request"); // 请求库,需npm 安装依赖
const envId = "testEnv"; // 环境ID
const { parseString } = require("xml2js"); // 解析 xml 文件 到 json 文件库, 需npm安装依赖

async function parseXML(str) {
return new Promise((resolve, reject) => {
parseString(str, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}

async function test() {
// 1. 上传文件
const fileContent = fs.createReadStream(
path.resolve(__dirname, "./my-photo.png")
); // 获取本地的文件内容
const cloudPath = "cloud-my-photo.png"; // 定义的云端文件路径
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. 获取下载链接
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. 删除文件
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 描述

功能:批量复制文件,可通过参数实现移动文件效果

2. 请求地址

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:batchCopyFile

3. 请求体

字段类型必填说明
fileListArray<FileItem>复制文件信息列表
FileItem
字段类型必填说明
srcPathstring源文件的绝对路径,包含文件名。例如 foo/bar.jpg、foo/bar/baz.jpg 等,不能包含除[0-9 , a-z , A-Z]、/、!、-、_、.、、*和中文以外的字符,使用 / 字符来实现类似传统文件系统的层级结构
dstPathstring目标文件的绝对路径,包含文件名。例如 foo/bar.jpg、foo/bar/baz.jpg 等,不能包含除[0-9 , a-z , A-Z]、/、!、-、_、.、、*和中文以外的字符,使用 / 字符来实现类似传统文件系统的层级结构
overwriteboolean当目标文件已经存在时,是否允许覆盖已有文件,默认 true
removeOriginalboolean复制文件后是否删除源文件,默认 false

4. 响应体

字段类型必填说明
statusCodeNumberHTTP 状态码
bodyObject响应体,结构如下
body
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data
字段类型必填说明
fileListArray<CopyFileItem>复制文件信息列表

CopyFileItem

字段类型必填说明
fileIDString复制的文件 ID
codeString复制失败错误码
messageString复制失败时错误描述信息

5. 使用示例

const request = require("request") // 请求库,需 npm 安装依赖

async function main() {
const result = await new Promise((resolve, reject) => {
request(
{
url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:batchCopyFile`, // 请求地址
method: "POST",
headers: {
// 填写凭证请求头
"X-CloudBase-Authorization": "your auth",
"X-CloudBase-SessionToken": "your token",
"X-CloudBase-TimeStamp": Math.floor(new Date().getTime() / 1000),
},
body: {
// 复制文件列表
fileList: [
{
srcPath: 'filename.jpg', // 源文件路径
dstPath: 'target/filename.jpg' // 目标文件路径
}
]
},
json: true,
},
(err, response) => {
if (err) {
reject(err)
}
resolve(response.body)
}
)
})
console.log(result) // 打印响应结果
}

main()