I am using javascript, node.js and 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. Any suggestion, please?
我用的是javascript, node。js和aws sdk。有许多关于使用签名URL直接将现有文件上载到S3的示例,但是现在我尝试上载字符串并在S3中创建一个文件,而不需要任何本地保存的文件。任何建议,请吗?
3 个解决方案
#1
0
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服务、amazon-s3或aws-sdk,但是如果您能够上传文件或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");
#2
2
Please follow the example here http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
请按照下面的示例:http://docs.aws.amazon.com/awsjavascriptsdk/latest/aws3.html #putObject-property
Convert your string to buffer and pass it. It should work.
将字符串转换为缓冲区并传递它。它应该工作。
#3
1
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();
}
});
});
});
};
#1
0
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服务、amazon-s3或aws-sdk,但是如果您能够上传文件或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");
#2
2
Please follow the example here http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
请按照下面的示例:http://docs.aws.amazon.com/awsjavascriptsdk/latest/aws3.html #putObject-property
Convert your string to buffer and pass it. It should work.
将字符串转换为缓冲区并传递它。它应该工作。
#3
1
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();
}
});
});
});
};