应考虑使用节点 js 可伸缩性和性能将文件上传到 s3 的良好做法
good practice to upload file to s3 using nodejs scalability and perfomance should be considered
我已经使用 multer 在 s3 中上传文件,我尝试了三种方法将文件上传到 s3
- 内存存储(默认)
- *自定义S3stroage引擎(通过直接从请求中提取文件并上传到s3)
- Diskstroage(默认)
在我的例子中,second 方法在性能方面很好,但我认为它不可扩展,much.but 第二种方法节省了大量时间(而不是写入磁盘然后从那里流式传输它更直接)。但我不知道这个 approach.can 的缺点是什么,有人建议我一个好的方法。
您应该考虑使用 S3 Pre-signed URL
因此,与其将您的实例用作交易的中间人,不如创建一个 API 允许客户端检索 短暂的唯一(s3 直接)url,客户端可以使用它直接将文件上传到 S3 中您想要的位置,而无需通过您的实例。我相信这比您现有的解决方案更具可扩展性。
但是,有一个警告。您的预签名 url(如建议的名称)是 "pre-authenticated",在向您的客户提供此信息时要小心,并确保 url 的有效期足够短以防止进一步滥用。
此外,如果您希望在文件成功上传到存储时收到通知,您可能需要额外实施侦听 S3 事件。参见 https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html
使用 aws SDK。它快速且安全。
https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-creating-buckets.html
您不需要预签名 url 即可上传任何内容 file.I 不知道为什么它会回答任何问题。
始终从磁盘存储 Diskstroage 中读取(默认)。如果文件未完全读取,您可以重试。
check these github samples
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
// Create S3 service object
s3 = new AWS.S3({apiVersion: '2006-03-01'});
// call S3 to retrieve upload file to specified bucket
var uploadParams = {Bucket: process.argv[2], Key: '', Body: ''};
var file = process.argv[3];
// Configure the file stream and obtain the upload parameters
var fs = require('fs');
var fileStream = fs.createReadStream(file);
fileStream.on('error', function(err) {
console.log('File Error', err);
});
uploadParams.Body = fileStream;
var path = require('path');
uploadParams.Key = path.basename(file);
// call S3 to retrieve upload file to specified bucket
s3.upload (uploadParams, function (err, data) {
if (err) {
console.log("Error", err);
} if (data) {
console.log("Upload Success", data.Location);
}
});
To run the example, type the following at the command line.
node s3_upload.js BUCKET_NAME FILE_NAME
aws-cli 非常好,但如果您担心扩展问题,可以使用后台工作程序将文件上传到 s3。首先,将文件保存到 DiskStorage 并将作业添加到将文件上传到 aws-s3 的后台进程的队列中。
此方法为您提供了一个选项,可以在上传失败时重试上传,并在上传后将其删除,从而让您更加自由 space。你可以试试bull(队列系统)。
我已经使用 multer 在 s3 中上传文件,我尝试了三种方法将文件上传到 s3
- 内存存储(默认)
- *自定义S3stroage引擎(通过直接从请求中提取文件并上传到s3)
- Diskstroage(默认)
在我的例子中,second 方法在性能方面很好,但我认为它不可扩展,much.but 第二种方法节省了大量时间(而不是写入磁盘然后从那里流式传输它更直接)。但我不知道这个 approach.can 的缺点是什么,有人建议我一个好的方法。
您应该考虑使用 S3 Pre-signed URL
因此,与其将您的实例用作交易的中间人,不如创建一个 API 允许客户端检索 短暂的唯一(s3 直接)url,客户端可以使用它直接将文件上传到 S3 中您想要的位置,而无需通过您的实例。我相信这比您现有的解决方案更具可扩展性。
但是,有一个警告。您的预签名 url(如建议的名称)是 "pre-authenticated",在向您的客户提供此信息时要小心,并确保 url 的有效期足够短以防止进一步滥用。
此外,如果您希望在文件成功上传到存储时收到通知,您可能需要额外实施侦听 S3 事件。参见 https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html
使用 aws SDK。它快速且安全。 https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-creating-buckets.html
您不需要预签名 url 即可上传任何内容 file.I 不知道为什么它会回答任何问题。
始终从磁盘存储 Diskstroage 中读取(默认)。如果文件未完全读取,您可以重试。 check these github samples
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'});
// Create S3 service object
s3 = new AWS.S3({apiVersion: '2006-03-01'});
// call S3 to retrieve upload file to specified bucket
var uploadParams = {Bucket: process.argv[2], Key: '', Body: ''};
var file = process.argv[3];
// Configure the file stream and obtain the upload parameters
var fs = require('fs');
var fileStream = fs.createReadStream(file);
fileStream.on('error', function(err) {
console.log('File Error', err);
});
uploadParams.Body = fileStream;
var path = require('path');
uploadParams.Key = path.basename(file);
// call S3 to retrieve upload file to specified bucket
s3.upload (uploadParams, function (err, data) {
if (err) {
console.log("Error", err);
} if (data) {
console.log("Upload Success", data.Location);
}
});
To run the example, type the following at the command line.
node s3_upload.js BUCKET_NAME FILE_NAME
aws-cli 非常好,但如果您担心扩展问题,可以使用后台工作程序将文件上传到 s3。首先,将文件保存到 DiskStorage 并将作业添加到将文件上传到 aws-s3 的后台进程的队列中。 此方法为您提供了一个选项,可以在上传失败时重试上传,并在上传后将其删除,从而让您更加自由 space。你可以试试bull(队列系统)。