An OSSClient instance is 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 to initiate an OSS request, you must initialize an OSSClient instance and modify the default configuration items based on your business requirements.
Create an OSSClient instance
(Recommended) Use the V4 signature algorithm
(Not recommended) Use the V1 signature algorithm
We recommend that you use the V4 signature algorithm which provides better security. In this case, you must declare authorizationV4. OSS SDK for Node.js 6.20.0 and later support V4 signatures.
The following sample code provides an example on how to use an OSS endpoint to create an OSSClient instance by using the V4 signature algorithm. If you want to use other methods to create an OSSClient instance by using the V4 signature algorithm, refer to the following sample code and change the variables:
const OSS = require('ali-oss');
const client = new OSS({
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
region: 'yourRegion',
authorizationV4: true,
bucket: 'yourBucketName',
});
Important
From March 1, 2025, the V1 signature algorithm of OSS is no longer available to new customers with new UIDs. From September 1, 2025, OSS no longer updates and maintains the V1 signature algorithm, and the V1 signature algorithm is no longer available for new buckets. Upgrade V1 signatures to V4 signatures at the earliest opportunity to prevent impact on your business.
Create an OSSClient instance by using an OSS endpoint
Create an OSSClient instance by using a custom domain name
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({
region: 'yourRegion',
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
bucket: 'yourBucketName',
});
The following sample 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({
endpoint: 'http://img.example.com',
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
cname: true,
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.