2
我想使用nodejs將文件上傳到amazon s3。我能夠上傳並創建一個存儲桶。但是,上傳的返回值不包含鏈接到該文件的url。有誰知道如何獲得網址?如何使文件公開,所以我是因爲我想上傳圖片到我的網站如何將文件上傳到s3fs並獲取公共網址?
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');
});