Download objects

Last Updated: Oct 31, 2017

You can download an object from OSS through any of the following methods:

  • Download to a local file
  • Stream download
  • Download to the buffer
  • HTTP download (by using a browser)

Download an object to a local file

Download an object to a local file through the ‘get’ interface:

  1. var co = require('co');
  2. var OSS = require('ali-oss');
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: 'Your bucket name'
  8. });
  9. co(function* () {
  10. var result = yield client.get('object-key', 'local-file');
  11. console.log(result);
  12. }).catch(function (err) {
  13. console.log(err);
  14. });

Stream download

When you use ‘getStream’ to download an object, a ‘Readable Stream’ is returned for you to process object content in a streaming way.

  1. var co = require('co');
  2. var OSS = require('ali-oss');
  3. var fs = require('fs);
  4. var client = new OSS({
  5. region: '<Your region>',
  6. accessKeyId: '<Your AccessKeyId>',
  7. accessKeySecret: '<Your AccessKeySecret>',
  8. bucket: 'Your bucket name'
  9. });
  10. co(function* () {
  11. var result = yield client.getStream('object-key');
  12. console.log(result);
  13. var writeStream = fs.createWriteStream('local-file');
  14. result.stream.pipe(writeStream);
  15. }).catch(function (err) {
  16. console.log(err);
  17. });

Download to buffer

You can download the object content to the buffer through the ‘get’ interface:

  1. var co = require('co');
  2. var OSS = require('ali-oss');
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: 'Your bucket name'
  8. });
  9. co(function* () {
  10. var result = yield client.get('object-key');
  11. console.log(result.content);
  12. }).catch(function (err) {
  13. console.log(err);
  14. });

HTTP download

Objects stored in OSS can be downloaded through HTTP directly without using the SDK. HTTP download supports browsers and command-line interface (CLI) tools (such as ‘wget’ and ‘curl’). The URL of the object to be downloaded is generated by the SDK. You can use the ‘signatureUrl’ method to generate the downloadable HTTP address. The validity period of the URL is half an hour by default:

  1. var co = require('co');
  2. var OSS = require('ali-oss');
  3. var client = new OSS({
  4. region: '<Your region>',
  5. accessKeyId: '<Your AccessKeyId>',
  6. accessKeySecret: '<Your AccessKeySecret>',
  7. bucket: 'Your bucket name'
  8. });
  9. var url = client.signatureUrl('object-key');
  10. console.log(url);
  11. var url = client.signatureUrl('object-key', {expires: 3600});
  12. console.log(url);
  13. // signed URL for PUT
  14. var url = client.signatureUrl('object-key', {method: 'PUT'});
  15. console.log(url);
Thank you! We've received your feedback.