# 文件存储

# 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 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. 请求体

字段 类型 必填 说明
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();