You can use range download to download parts of the content 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.

Sample code

The following code provides an example on how to perform range download:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>Document</title>
</head>

<body>
  <button id='download'>Download</button>
  <!-- import an SDK file -->
  <script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.0.min.js"></script>
  <script type="text/javascript">    
    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 name of the bucket. Example: examplebucket. 
      bucket: "examplebucket",
    });

    // Obtain the DOM elements of the range download operation. 
    const download = document.getElementById('download')
    // For an object that is 1,000 bytes in size, the valid range is within the range from byte 0 to byte 999. 
    // If the specified value range is invalid, for example, the specified range includes a negative number, or the specified value is larger than the object size, the entire object is downloaded. 
    const start = 0, end = 999
    // Create a button that is used to start the range download operation. 
    download.addEventListener('click', () => {
      client.get('example.txt',{
        headers: {          
          Range: `bytes=${start}-${end}`,
        },
      }).then(r => {
        // Convert the returned data to a Blob object that can be downloaded. 
        const newBlob = new Blob([r.content], { type: r.res.headers['content-type'] });
        // Create an <a> tag. 
        const link = document.createElement('a')
        // Add the href attribute to the tag. 
        link.href = window.URL.createObjectURL(newBlob)
        // Specify the name of the object after it is downloaded to your local device. 
        link.download = 'example.txt'
        // Download the object. 
        link.click()
        // Remove the URL added to the tag. 
        window.URL.revokeObjectURL(link.href)
      })
    })

  </script>
</body>

</html>

References

  • For more information about the complete sample code for range download, visit GitHub.
  • For more information about the API operation that is called to perform range download, see GetObject.