This topic describes how to initialize Object Storage Service (OSS) SDK for Node.js.
Create an object named app.js
and write the following content to the object:
let OSS = require('ali-oss');
let client = new OSS({
// Set yourRegion to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou.
region: 'yourRegion',
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
accessKeyId: 'yourAccessKeyId',
accessKeySecret: 'yourAccessKeySecret',
});
The region
parameter indicates the region in which you activate OSS. Example: oss-cn-hangzhou
. For the complete list of regions, see Endpoints and regions.
Note You can also use the following parameters to specify an endpoint based on your actual
scenario:
- To access internal nodes, specify
region
andinternal
and setinternal
totrue
. - To access OSS over HTTPS, specify
region
andsecure
and setsecure
totrue
. - To access OSS by using custom domain names, specify
cname
andendpoint
and setcname
totrue
andendpoint
to the custom domain name that you mapped to the bucket where the required OSS data is stored. - If you set
endpoint
to a value such ashttp://oss-cn-hangzhou.aliyuncs.com
,region
is ignored.endpoint
can be set to an IP address, and you can specify HTTPS. - If
bucket
is not configured, you must first calluseBucket
when you manage objects stored in the bucket. You need to call this operation only once. - To specify a timeout period for API calls to access OSS, use
timeout
. The default value oftimeout
is 60000. Unit: milliseconds.