文件存储
getUploadMetaData
1. API 描述
功能:获取文件上传属性
2. 请求地址
POST https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/storages:getUploadMetaData
3. 请求体
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
path | String | 是 | 指定的云端文件路径 |
4. 响应体
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
statusCode | Number | 是 | 状态码,200 |
body | Object | 是 | 响应体,结构如下 |
body
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
requestId | String | 否 | 请求 ID |
data | Object | 否 | 返回结果 |
code | String | 否 | 错误码 |
message | String | 否 | 错误信息 |
data
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
url | String | 是 | 上传 url |
token | String | 是 | token 字段,上传请求用 |
authorization | String | 是 | authorization 字段,上传请求用 |
fileID | String | 是 | 文件 ID |
cosFileID | String | 是 | cos 文件 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. 请求体
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
fileList | Array<FileItem> | 是 | 文件信息数组 |
FileItem
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
fileID | String | 是 | 文件 ID |
maxAge | Number | 否 | 文件链接有效期 |
4. 响应体
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
statusCode | Number | 是 | 状态码,200 |
body | Object | 是 | 响应体,结构如下 |
body
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
requestId | String | 否 | 请求 ID |
data | Object | 否 | 返回结果 |
code | String | 否 | 错误码 |
message | String | 否 | 错误信息 |
data
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
fileList | Array<DownloadFileItem> | 是 | 文件下载信息列表 |
DownloadFileItem
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
fileID | String | 是 | 下载的文件 ID |
code | String | 是 | SUCCESS 表示成功 |
tempFileURL | String | 是 | 文件下载链接 |
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. 请求体
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
fileList | Array<String> | 是 | 文件 ID 数组 |
4. 响应体
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
statusCode | Number | 是 | 状态码,200 |
body | Object | 是 | 响应体,结构如下 |
body
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
requestId | String | 否 | 请求 ID |
data | Object | 否 | 返回结果 |
code | String | 否 | 错误码 |
message | String | 否 | 错误信息 |
data
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
fileList | Array<DeleteFileItem> | 是 | 删除文件信息列表 |
DeleteFileItem
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
fileID | String | 是 | 删除的文件 ID |
code | String | 是 | SUCCESS 表示成功 |
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();