This topic describes how to upload a local file to OSS.

The following code provides an example on how to use the put operation to upload a local file to OSS:

The sample code in this topic uses the catch method. To understand the syntax of this method, learn the Promise, async, and wait features defined in ECMAScript 6.

const OSS = require('ali-oss')

const client = new OSS({
  bucket: '<Your BucketName>',
  // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
});

async function put () {
  try {
    // Set object-name to a name such as file.txt or a folder such as abc/test/file.txt to upload the object to the current bucket or the specified folder in the bucket.
    let result = await client.put('object-name', 'local-file');
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

put();