[英]direct upload string from browser to s3 without local file
I am using javascript, node.js and aws sdk. 我正在使用javascript,node.js和aws sdk。 There are many examples about uploading existing files to S3 directly with signed URL, but now I am trying to upload strings and create a file in S3, without any local saved files.
有很多关于使用签名URL直接将现有文件上传到S3的示例,但是现在我尝试上传字符串并在S3中创建文件,而没有任何本地保存的文件。 Any suggestion, please?
有什么建议吗?
Please follow the example here http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property 请在此处遵循示例http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
Convert your string to buffer and pass it. 将您的字符串转换为缓冲区并传递。 It should work.
它应该工作。
You could try something like this: 您可以尝试这样的事情:
var fs = require('fs');
exports.upload = function (req, res) {
var file = req.files.file;
fs.readFile(file.path, function (err, data) {
if (err) throw err; // Something went wrong!
var s3bucket = new AWS.S3({params: {Bucket: 'mybucketname'}});
s3bucket.createBucket(function () {
var params = {
Key: file.originalFilename, //file.name doesn't exist as a property
Body: data
};
s3bucket.upload(params, function (err, data) {
// Whether there is an error or not, delete the temp file
fs.unlink(file.path, function (err) {
if (err) {
console.error(err);
}
console.log('Temp File Delete');
});
console.log("PRINT FILE:", file);
if (err) {
console.log('ERROR MSG: ', err);
res.status(500).send(err);
} else {
console.log('Successfully uploaded data');
res.status(200).end();
}
});
});
});
};
Have not tried amazon-web-services
, amazon-s3
or aws-sdk
, though if you are able to upload File
or FormData
objects you can create either or both at JavaScript and upload the object. 尚未尝试过
amazon-web-services
, amazon-s3
或aws-sdk
,但是如果能够上载File
或FormData
对象,则可以在JavaScript上创建一个或两个对象,然后上载该对象。
// create a `File` object
const file = new File(["abc"], "file.txt", {type:"text/plain"});
// create a `Blob` object
// will be converted to a `File` object when passed to `FormData`
const blob = new Blob(["abc"], {type:"text/plain"});
const fd = new FormData();
fd.append("file", blob, "file.txt");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.