I am trying to upload files into amazon s3 using nodejs. I was able to upload ,and create a bucket. However, the return value from the upload does not contain the url that link to that file. Does anyone know how obtain the url ? and how to make the file public so I can access it because I am trying to upload images into my website
var multer = require('multer');
var AWS= require('aws-sdk');
var fs = require('fs');
var S3FS = require('s3fs');
var s3fsImpl = new S3FS('bucketName',{
accessKeyId:'************',
secretAccessKey:'***************'
});
s3fsImpl.create();
var multiparty = require('connect-multiparty'),
multipartyMiddleware = multiparty();
router.use(multipartyMiddleware);
router.post('/upload',function(req,res){
console.log(JSON.stringify(req.files));
var file = req.files;
var stream = fs.createReadStream(file.fileUpload.path);
return s3fsImpl.writeFile(file.fileUpload.originalFilename, stream, 'public-read').then(function(data){
fs.unlink(file.fileUpload.path, function(err){
console.error(err);
})
});
res.send('Sucessfully uploaded to Amazon S3 server');
});
If you are using JavaScript/Node, you should use the official AWS SDK rather than S3FS.
For example: putObject() function
When using that function, there is an ACL parameter that specifies whether the object should be public:
ACL: 'private | public-read | public-read-write | authenticated-read | aws-exec-read | bucket-owner-read | bucket-owner-full-control',
Alternatively, a Bucket Policy can be used to grant access to whole buckets or sub-directories.