使用REST API从Amazon S3下载文件 [英] Download file from Amazon S3 using REST API

查看:224
本文介绍了使用REST API从Amazon S3下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自己的 REST API 来调用下载文件。 (最后,文件可以存储在不同类型的服务器中...亚马逊s3,本地等...)

I have my own REST API to call in order to download a file. (At the end, the file could be store in different kind of server... Amazon s3, locally etc...)

To从s3获取一个文件,我应该使用这个方法:

To get a file from s3, I should use this method:

var url = s3.getSignedUrl('getObject', params);

这将给我一个可下载的电话链接。

This will give me a downloadable link to call.

现在,我的问题是,如果文件来自该链接,我如何使用自己的rest API下载文件?有没有办法重定向呼叫?

Now, my question is, how can I use my own rest API to download a file when it comes from that link? Is there a way to redirect the call?

我正在使用 Hapi 为我的 REST 服务器。

I'm using Hapi for my REST server.

{
    method: "GET", path: "/downloadFile",
    config: {auth: false},
    handler: function (request, reply) {
        // TODO
        reply({})
    }
},


推荐答案

而不是使用重定向来下载所需的文件,只需返回从S3返回一个unbufferedStream。 unbufferedStream 可以从 AWS-SDK 中的 HttpResponse 返回。这意味着无需从S3下载文件,然后将其读入,然后让请求者下载文件。

Instead of using a redirect to download the desired file, just return back an unbufferedStream instead from S3. An unbufferedStream can be returned from the HttpResponse within the AWS-SDK. This means there is no need to download the file from S3, then read it in, and then have the requester download the file.

仅供参考我使用此 getObject()使用Express进行操作并且从未使用过Hapi,但我认为我对路径定义非常接近,但希望它能捕捉到我想要实现的内容。

FYI I use this getObject() approach with Express and have never used Hapi, however I think that I'm pretty close with the route definition but hopefully it will capture the essence of what I'm trying to achieve.

const getObject = require('./getObject');

{
    method: "GET", path: "/downloadFile",
    config: {auth: false},
    handler: function (request, reply) {
        let key = ''; // get key from request
        let bucket = ''; // get bucket from request

        return getObject(bucket, key)
          .then((response) => {
            reply.statusCode(response.statusCode);

            response.headers.forEach((header) => {
              reply.header(header, response.headers[header]);
            });

            return reply(response.readStream);
          })
          .catch((err) => {
            // handle err
            reply.statusCode(500);
            return reply('error');
          });
    }
},



getObject.js



getObject.js

const AWS = require('aws-sdk');
const S3 = new AWS.S3(<your-S3-config>);

module.exports = function getObject(bucket, key) {
  return new Promise((resolve, reject) => {
    // Get the file from the bucket
    S3.getObject({
      Bucket: bucket,
      Key: key
    })
      .on('error', (err) => {            
        return reject(err);
      })
      .on('httpHeaders', (statusCode, headers, response) => {
        // If the Key was found inside Bucket, prepare a response object
        if (statusCode === 200) {
          let responseObject = {
            statusCode: statusCode,
            headers: {
              'Content-Disposition': 'attachment; filename=' + key
            }
          };

          if (headers['content-type'])
            responseObject.headers['Content-Type'] = headers['content-type'];
          if (headers['content-length'])
            responseObject.headers['Content-Length'] = headers['content-length'];

          responseObject.readStream = response.httpResponse.createUnbufferedStream();
          return resolve(responseObject);
        }
      })
      .send();
  });
}

这篇关于使用REST API从Amazon S3下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆