The Client is the primary object for interacting with Object Storage Service (OSS) through the Node.js software development kit (SDK). It manages OSS resources, such as buckets and files. To send OSS requests using the Node.js SDK, initialize a Client instance and modify the default configurations as needed.
Notes
You must configure access credentials before you initialize an OSSClient instance. This topic provides an example of using environment variables. For more information, see Configure access credentials (Node.js SDK).
For information about OSS regions and their corresponding endpoints, see Regions and endpoints.
For information about how to create an AccessKey pair for a Resource Access Management (RAM) user, see Create an AccessKey pair.
Prerequisites
Before you configure the client, you must set the environment variables using the AccessKey of a RAM user.
Create an AccessKey for a RAM user that has OSS management permissions.
Set the environment variables using the AccessKey of the RAM user.
Linux
In the command-line interface, run the following commands to append the environment variable settings to the
~/.bashrcfile.echo "export OSS_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc echo "export OSS_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrcRun the following command to apply the changes.
source ~/.bashrcRun the following commands to verify that the environment variables are set.
echo $OSS_ACCESS_KEY_ID echo $OSS_ACCESS_KEY_SECRET
macOS
In the terminal, run the following command to view the default shell type.
echo $SHELLFollow the steps for your default shell type.
Zsh
Run the following commands to append the environment variable settings to the
~/.zshrcfile.echo "export OSS_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc echo "export OSS_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrcRun the following command to apply the changes.
source ~/.zshrcRun the following commands to verify that the environment variables are set.
echo $OSS_ACCESS_KEY_ID echo $OSS_ACCESS_KEY_SECRET
Bash
Run the following commands to append the environment variable settings to the
~/.bash_profilefile.echo "export OSS_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile echo "export OSS_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profileRun the following command to apply the changes.
source ~/.bash_profileRun the following commands to verify that the environment variables are set.
echo $OSS_ACCESS_KEY_ID echo $OSS_ACCESS_KEY_SECRET
Windows
CMD
Run the following commands in Command Prompt.
setx OSS_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID" setx OSS_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET"Run the following commands to verify that the environment variables are set.
echo %OSS_ACCESS_KEY_ID% echo %OSS_ACCESS_KEY_SECRET%
PowerShell
Run the following commands in PowerShell.
[Environment]::SetEnvironmentVariable("OSS_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User) [Environment]::SetEnvironmentVariable("OSS_ACCESS_KEY_SECRET", "YOUR_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)Run the following commands to verify that the environment variables are set.
[Environment]::GetEnvironmentVariable("OSS_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User) [Environment]::GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)
After you modify the system environment variables, you must restart or refresh your development environment. This includes your IDE, command-line interface, desktop applications, and background services. This ensures that the latest system environment variables are loaded.
Default configuration examples
The following code examples show how to configure an OSSClient instance using Signature V4 and Signature V1.
Signature V1 (not recommended)
Configuration examples for common scenarios
The following code examples show how to configure other domain names. By default, these examples use Signature V4 and the AccessKey information of a RAM user.
Internal domain name configuration example
If your application is deployed on an Alibaba Cloud ECS instance and needs to frequently access OSS resources in the same region, you can use an internal domain name to reduce traffic costs and improve access speed.
The following code shows an example of how to configure an OSSClient instance using an OSS internal domain name.
const OSS = require('ali-oss');
const client = new OSS({
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
// Set region to the region where the bucket is located. For example, for China (Hangzhou), set region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
//Use Signature V4.
authorizationV4: true,
// Set bucket to the name of your bucket.
bucket: 'yourBucketName',
// Set endpoint to the internal endpoint of the region where the bucket is located. For example, for China (Hangzhou), set endpoint to https://oss-cn-hangzhou-internal.aliyuncs.com.
endpoint: 'yourEndpoint',
});Custom domain name configuration example
The following code shows an example of how to configure an OSSClient instance using a custom domain name.
You must first attach the custom domain name to the default domain name of the bucket. Otherwise, an error occurs. For more information about how to attach a custom domain name, see Access OSS using a custom domain name.
const OSS = require('ali-oss');
const client = new OSS({
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
// Set region to the region where the bucket is located. For example, for China (Hangzhou), set region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
// Use the Signature V4 algorithm.
authorizationV4: true,
// Set bucket to the name of your bucket.
bucket: 'yourBucketName',
// Set endpoint to your custom domain name. For example, https://static.example.com.
endpoint: 'yourEndpoint',
// Set cname to true to enable the CNAME option.
cname: true,
});Acceleration endpoint configuration example
The following code shows an example of how to configure an OSSClient instance using an acceleration endpoint.
const OSS = require('ali-oss');
const client = new OSS({
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
// Set region to the region where the bucket is located. For example, for China (Hangzhou), set region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
//Use Signature V4.
authorizationV4: true,
// Set bucket to the name of your bucket.
bucket: 'yourBucketName',
// Set endpoint to an acceleration endpoint. For example, oss-accelerate.aliyuncs.com.
endpoint: 'oss-accelerate.aliyuncs.com',
});References
For more information about OSSClient configuration options, see Examples on GitHub.

