跳到主要内容

文件存储

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 request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境ID
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);  });

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"; // 环境IDconst { 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"; // 环境IDconst { 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();