You can use the signatureUrl method in a browser to generate the URL to download or preview an object. You can use the download attribute of the <a> HTML element or the window.open API to obtain the URL of an object.

Usage notes

  • When you use packaging tools such as Webpack and Browserify, install OSS SDK for Browser.js by running the npm install ali-oss command.
  • OSS SDK for Browser.js is usually used in browsers. To prevent your AccessKey pair from being exposed, we recommend that you use a temporary access credential generated by Security Token Service (STS) to access OSS.

    For more information about how to set up STS, see Use a temporary access credential provided by STS to access OSS in OSS Developer Guide. You can call the AssumeRole operation or use STS SDKs for various programming languages to obtain a temporary access credential. For more information, see STS SDK overview. A temporary access credential contains a security token and a temporary AccessKey pair that consists of an AccessKey ID and an AccessKey secret.

Obtain the URL of an object for previews

The following code provides an example on how to obtain the URL of an object named exampleobject.txt in a bucket named examplebucket for previews.

Note To preview an object in a browser, set Content-Disposition to inline and use the custom domain name that is mapped to the bucket to access the object. For more information about specific operations, see Configure object metadata and Map custom domain names.
const OSS = require('ali-oss');

const 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',
  // Specify the temporary AccessKey pair obtained from STS. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the security token obtained from STS. 
  stsToken: 'yoursecurityToken',
  // Specify the bucket name. 
  bucket: 'examplebucket',
});

let url;
// Specify the full path of the object. The full path of the object cannot contain the bucket name. By default, the validity period of the object URL is 1,800 seconds, which equals to 30 minutes. 
url = client.signatureUrl('exampleobject.txt');
console.log(url);

// Set the validity period of the URL to 3600 seconds. If you do not set the validity period, the default value 1800 is used. 
//url = client.signatureUrl('exampleobject.txt', {expires: 3600});
//console.log(url);        

Obtain the URL to download an object

The following code provides an example on how to obtain the URL to download an object named exampleobject.txt in a bucket named examplebucket. By default, the validity period of the URL is 1,800 seconds.

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

const 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',
  // Specify the temporary AccessKey pair obtained from STS. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the security token obtained from STS. 
  stsToken: 'yoursecurityToken',
  // Specify the bucket name. 
  bucket: 'examplebucket',
});

// Set the response header to automatically download an object by using the URL, and set the name of the local file after the object is downloaded. 
const filename = 'examplefile.txt' 
const response = {
  'content-disposition': `attachment; filename=${encodeURIComponent(filename)}`
}
// Specify the full path of the object. The full path of the object cannot contain the bucket name. 
const url = client.signatureUrl('exampleobject.txt', { response });
console.log(url);