问题
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.
├── app
├── public
├── node_modules
├── secure (files stored here)
├── .ebextensions
├── .elasticbeanstalk
├── package.json
└── app.js
the secure/
directory is where all my files are stored (approx 350mb).
here is an example of a link to a file in that directory href="/secure/folder/document.pdf"
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.
Here is my attempt at it!
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);
});
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.
回答1:
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){...}
Hence every request start with /secure/ will be attach to this router. And then you can extract the path by access req.originalUrl .
回答2:
Unless I misunderstood the question, you shouldn't be doing this. Save the relationship between the file and the S3 URI and then, when a file is requested, get the relationship and respond with the S3 URI.
If you want to implement access control on your application, then you should retrieve the file from S3 and serve it to the client but again, you should only save the relationship, not the file.
I usually do something like this:
Pseudocode:
var Model = {
id: {type: "string"},
file: {type: "string"}
}
Where file is the S3 URI. You can extend this model with any kind of access control that you need.
来源:https://stackoverflow.com/questions/30636597/accessing-a-s3-bucket-from-node