Tencent Cloud Cloud Object Storage (COS) Node.js SDK v5

repository·master·Indexed 42 days ago

https://github.com/tencentyun/cos-nodejs-sdk-v5

A Node.js SDK (v3.0.0) for interacting with Tencent Cloud Cloud Object Storage (COS) via the XML API. It enables programmatic management of files, buckets, and other COS resources, featuring advanced file upload capabilities via the COS class.

Tokens
652
Snippets
3
Records
3
Agent score
4%

What's inside Tencent Cloud COS

  1. Initialize the COS client and perform an advanced file upload

    master

    To use the SDK, require the cos-nodejs-sdk-v5 module and instantiate the COS class with your SecretId and SecretKey.

    To upload a file using the uploadFile method (Advanced Upload), you must provide:

    • Bucket: The bucket name in the format bucketname-appid.
    • Region: The region code (e.g., ap-guangzhou).
    • Key: The destination path/name in the bucket.
    • FilePath: The local path to the file.
    • SliceSize (Optional): The threshold in bytes that triggers multipart upload. If the file exceeds this size, it will be uploaded in chunks.
    // 引入模块
    var COS = require('cos-nodejs-sdk-v5');
    // 创建实例
    var cos = new COS({
      SecretId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
      SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    });
    
    // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。
    var Bucket = 'test-1250000000';
    // 存储桶Region可以在COS控制台指定存储桶的概览页查看
    var Region = 'ap-guangzhou';
    
    // 高级上传
    cos.uploadFile(
      {
        Bucket: Bucket,
        Region: Region,
        Key: '1.zip',
        FilePath: './1.zip', // 本地文件地址,需自行替换
        SliceSize: 1024 * 1024 * 5, // 触发分块上传的阈值,超过5MB使用分块上传,非必须
      },
      function (err, data) {
        console.log(err, data);
      }
    );
  2. Initialize the COS client

    master

    The cos-nodejs-sdk-v5 package exports the COS class as its primary entry point. You can require the package to access the COS constructor, which is used to initialize the client for interacting with Tencent Cloud Object Storage (COS).

    const COS = require('cos-nodejs-sdk-v5');
    
    // The COS class is used to initialize the client
    const cos = new COS({
      // Configuration options go here
    });