通过 Node js Express 访问 AWS S3 签名 URL [英] Accessing AWS S3 Signed URL via Node js Express

查看:27
本文介绍了通过 Node js Express 访问 AWS S3 签名 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用在 Node JS 和 Express 中创建的包装器 API 访问 AWS S3 签名 URL.

I am trying to access the AWS S3 signed URL using a wrapper API created in Node JS and Express.

我正在重定向一个 url 以使用 nginx 调用 Node API.在 Node API 中,在从 Node API 返回响应之前,我根据 S3 URL 返回的文件类型设置Content-Type"响应标头.

I am redirecting an url to call the Node API using nginx. In Node API I am setting the 'Content-Type' response headers according to the file type being returned by S3 URL before returning the repsonse from Node API.

当我访问 URL 以通过包装器节点 API 获取 S3 中的文档时,我没有获取任何内容.如果是 PDF 文件,我会看到白屏.

When I access the URL to fetch the document in S3 via wrapper Node API I am not getting any content. In case of PDF files I am getting blank white screen.

下面是我用来从 Node 包装器 API 中的 S3 获取文档并中继相同响应的代码块.

Below is the block of code I am using to fetch the document from S3 in Node wrapper API and relay the same response.

const request = require('request');

request(s3SignedURL, function (error, result, body) {
    if (!error) {
        res.header('Content-Type','application/pdf');
        res.end(body);
    } else {
        res.status(500).send(error);
    }
});

我将 S3 上传文件的详细信息存储在一个表中,并在有人尝试访问它时使用该表来获取文档.以下是使用的完整代码.

I am storing the details of file uploaded S3 in an table and using that table to fetch the document when someone tries to access it. Below is the complete code used.

const S3 = new AWS.S3();

app.get("/getFile/*", function (req, res) {
    var urlInfo = req.url;
    var urlInfoList = urlInfo.split("/");
    var accessCode = urlInfoList[2];
    var accessID = urlInfoList[3];

    if (!!accessID && !!accessCode) {
        db.getFileInfo(accessCode, accessID).then(function (fileInfo) {
            if (fileInfo.length > 0) {
                var S3Options = {
                    Bucket: Bucketname,
                    Key: fileInfo[0].fileS3Key,
                };
                S3.getObject(S3Options, function (err, data) {
                    res.attachment(fileInfo[0].fileS3Key);
                    res.send(data.Body);
                });

                // request(fileInfo[0].fileS3URL).pipe(res.set('Content-Type', 'application/pdf').set('Content-Disposition', 'inline'));
            } else {
                res.status(404).send("<html><head><title>404 Not Found</title></head><body bgcolor=\"white\"><center><h1>404 Not Found</h1></center><hr></body></html>");
            }
        }, function (fileErr) {
            console.log('fileErr');
            console.log(fileErr);
            res.status(404).send("<html><head><title>404 Not Found</title></head><body bgcolor=\"white\"><center><h1>404 Not Found</h1></center><hr></body></html>");
        });
    } else {
        res.status(404).send("<html><head><title>404 Not Found</title></head><body bgcolor=\"white\"><center><h1>404 Not Found</h1></center><hr></body></html>");
    }
});

这方面的任何帮助都会非常有用.

Any help in this regard will be very useful.

推荐答案

出现空白屏幕,因为您还需要发送 Content-Disposition 参数.

You are getting a blank screen because you need to send the Content-Disposition parameter as well.

顺便说一下,预签名 url 的目的是从公开暴露的环境(AWS 环境之外)临时访问 s3 对象.您不必使用后端服务的预签名 URL 机制,您可以通过 SDK 安全地访问对象

By the way, the purpose of the pre-signed url is to get temporary access to s3 objects from environments that are publicly exposed (outside AWS environment) . You don't have to use pre-signed URL mechanism from a backend service where you can access the object securely via SDK

const express = require('express');
const app = express();
const request = require('request');

app.get('/download-file', function (req, res) {

  // sdk way
  var s3 = new AWS.S3({});
  var options = {
    Bucket: 'my-bucket-name',
    Key: file,
  };
  s3.getObject(options, function (err, data) {
    res.setHeader('Content-disposition', `inline; filename="${filename}"`);
    res.end(data.Body, 'binary');
  });

  // http way
  request('https://s3-ap-southeast-2.amazonaws.com/my-bucket-name/mypdf.pdf')
    .pipe(res.set('Content-Type', 'application/pdf').set('Content-Disposition', 'inline; filename="mypdf.pdf"'))
})

这篇关于通过 Node js Express 访问 AWS S3 签名 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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