All Products
Search
Document Center

Object Storage Service:Initialization

Last Updated:Mar 12, 2024

An OSSClient instance serves as the client of Object Storage Service (OSS) SDK for Node.js to manage OSS resources such as buckets and objects. When you use OSS SDK for Node.js SDK to initiate an OSS request, you must initialize an OSSClient instance and modify the default configurations based on your business requirements.

Create an OSSClient instance

Use the V1 signature algorithm

Create an OSSClient instance by using an OSS endpoint

The following sample code provides an example on how to create an OSSClient instance by using an OSS endpoint:

const OSS = require('ali-oss');

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'yourBucketName',
});

Create an OSSClient instance by using a custom domain name

The following code provides an example on how to create an OSSClient instance by using a custom domain name. For more information, see Map a custom domain name to the default domain name of a bucket.

Important

If you use a custom domain name, you cannot use the client.listBuckets() method.

const OSS = require('ali-oss')

const client = new OSS({  
 // Use a custom domain name as the endpoint of a bucket to access the bucket. 
 endpoint: 'http://img.example.com', 
 // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
 accessKeyId: process.env.OSS_ACCESS_KEY_ID,
 accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
 cname: true,
 // Specify the name of the bucket. 
 bucket: 'yourBucketName',
});

Use the V4 signature algorithm (recommended)

If you want to use the V4 signature algorithm which is more secure, you must set authorizationV4 to true during initialization. OSS SDK for Node.js 6.20.0 and later support V4 signatures.

The following sample code provides an example on how to create an OSSClient instance by using an OSS endpoint and sign the request by using the V4 signature algorithm. If you want to use other methods to create an OSSClient instance, refer to the following sample code and change the variables.

const OSS = require('ali-oss');

const client = new OSS({
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  authorizationV4: true,
  // Specify the name of the bucket. 
  bucket: 'yourBucketName',
});

Configure an OSSClient instance

When you initialize an OSSClient instance, you can specify configuration parameters based on your business requirements. For example, you can specify a request timeout period by specifying timeout or temporary access credentials by specifying stsToken. For more information about the supported configuration parameters and configuration examples, see Configuration parameters.