访问从节点A S3桶 [英] Accessing a s3 bucket from Node
问题描述
我有一个基本节点应用程序,其中在用户登录并能够以查看链接到存储在所述应用程序内的一个目录文件。该文件结构看起来是这样的。
I have a basic node application where a user logs in and is able to view links to files stored in a directory within the app. The file structure looks something like this.
├──应用
├──公共
├──node_modules
├── node_modules
├──安全(文件存放于此)
├── secure (files stored here)
├──.ebextensions
├── .ebextensions
├──.elasticbeanstalk
├── .elasticbeanstalk
├──的package.json
├── package.json
└──app.js
在安全/
目录是我所有的文件都存储(约350MB)。
the secure/
directory is where all my files are stored (approx 350mb).
下面是该目录中的文件链接 HREF =/安全/文件夹/ document.pdf
here is an example of a link to a file in that directory href="/secure/folder/document.pdf"
我搬到了整个安全/
文件夹复制到S3存储让我申请下来的文件大小。
I've moved the whole secure/
folder to a s3 bucket to keep the file size of my application down.
会有人对如何去让我的节点应用访问此斗有什么建议?
Would anyone have any suggestions on how to go about allowing my node application to access this bucket?
我有中间件上的所有航线安全/
检查,如果用户进行身份验证,从而使链接不能分发到没有登录谁的用户
I have middleware on all routes to secure/
to check if the user is authenticated, so that the link can't be distributed to users who aren't logged in.
下面是我的尝试吧!
var AWS = require('aws-sdk'),
s3 = new AWS.S3({
region : 'example_region'
});
app.get('/secure/:endpoint',function(req,res){
var
Stream = s3.getObject({
Bucket: 'exampleBucket',
Key: 'secure/' + req.params.endpoint
}).createReadStream();
Stream.pipe(res);
});
我能够获得类似这样的链接 HREF =/安全/ userguide.pdf
,但是当你开始要1个多层次深入到文件系统,例如 HREF =/安全/ foldar1 / userguide.pdf
,它抛出一个404错误。
I'm able to access a link similar to this href="/secure/userguide.pdf"
, but when you start going more than 1 level deep into the file system e.g href="/secure/foldar1/userguide.pdf"
it throws a 404 error.
推荐答案
让我评论到答案,因为这导致你的解决方案。
Making my comment into an answer since this led you to the solution.
您应该重写你的路由是这样的:
You should rewrite your routing like this:
app.get('/secure/*', function(req, res){...}
因此,每个请求以 /安全/ 将被连接到该路由器。然后你就可以提取由接入路径的 req.originalUrl
Hence every request start with /secure/ will be attach to this router. And then you can extract the path by access req.originalUrl .
这篇关于访问从节点A S3桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!