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

Note The sample code in this topic uses the catch method. For more information about the syntax of this method, learn the Promise function defined in ECMAScript 6 and the async and await methods of this function. For more information about how to use the OSS SDK for Node.js, see Installation.

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

let OSS = require('ali-oss')

let client = new OSS({
  region: '<Your region>',
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
  bucket: '<Your bucket name>',
});

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

put();