You need to enable JavaScript to run this app.
导航

资源管理

最近更新时间2023.05.22 18:34:21

首次发布时间2021.03.15 12:01:33

阅读本文,您可以快速了解 Nodejs SDK 中资源管理相关接口的调用方法。

初始化

设置AK/SK和地域,具体可参考初始化

文件上传

通过指定服务 ID 和上传文件,来获取上传成功的资源 URI。

UploadImages 接口内部依次调用了 ApplyImageUploadCommitImageUpload 这两个 OpenAPI 来实现完整文件上传能力。接口返回参数详见 OpenAPI:CommitImageUpload

const uploadImages = async () => {
  try {
    const options = {
      serviceId: "your service id", // 服务 ID
      files: ["your image path1", "your image path2"], // 本地文件路径,一次上传文件的数量不能大于 10
      fileKeys: ["category/example1.jpg", "category/example2.png"], // 自定义上传文件的存储 Key。默认使用随机生成的字符串作为存储 Key。
    };
    const res = await Client.UploadImages(options);
    // do your work
    // ...
  } catch (err) {
    console.error(err);
  }
}

预览服务下的文件

通过指定服务 ID 以及上传文件 Uri,来获取指定文件的详细信息。

接口请求参数和返回参数详见 OpenAPI:PreviewImageUploadFile

const previewImageUploadFile = async () => {
  try {
    const res = await Client.PreviewImageUploadFile({
      ServiceId: 'your service id', // 服务 ID
      StoreUri: 'store uri', // 目标文件 uri
    });
    // do your work
    // ...
  } catch (err) {
    console.error(err);
  }
}

删除服务下多个文件

通过指定服务 ID 以及上传文件的 URI 列表,来删除指定的文件。

接口请求参数和返回参数详见 OpenAPI:DeleteImageUploadFiles

const deleteImages = async () => {
  try {
    const options = {
      ServiceId: 'your service id', // 服务ID
      StoreUris: ['store uri'], // 文件uri列表,最多1000个
    }
    const res = await Client.DeleteImageUploadFiles(options);
    // do your work
    // ...
  } catch (err) {
    console.error(err);
  }
}