我正在使用 Nodejs 尝试使用 aws-sdk 将图像推送到 S3 实例。目前,它从客户端上的文件读取,然后将其保存在服务器上(我使用的是meteor框架。)我想将其推送到S3服务器,而不是将其保存在meteor服务器上。当我尝试将其迁移过来时,图像在 S3 上时似乎增加了约 30%。如果我尝试从 S3 下载它们,图像也不再可见,因此看起来它已经更改了编码或其他内容。
以下是在客户端加载文件的代码:
saveFile = function( blob, name, path, type, callback ) {
var fileReader = new FileReader();
var method;
var encoding = 'binary';
var type = type || 'binary';
switch( type ) {
case 'text':
method = 'readAsText';
encoding = 'utf8';
break;
case 'binary':
method = 'readAsBinaryString';
encoding = 'binary';
break;
default:
method = 'readAsBinaryString';
encoding = 'binary';
break;
}
// Call the save function on the server after the file has been read.
fileReader.onload = function( file ) {
console.log( "File loaded..." );
Meteor.call( 'saveFile', file.srcElement.result, name, path, encoding, callback );
}
// Read the file
fileReader[ method ]( blob );
}
在服务器端:
saveFile: function( file, name, path, encoding ) {
s3.createBucket({Bucket: bucketName}, function() {
var params = {Bucket: bucketName, Key: keyName, ContentType: 'binary', ContentEncoding: 'utf8', Body: file};
s3.putObject(params, function(err, data) {
if (err)
console.log(err)
else
console.log("Successfully uploaded data to " + bucketName + "/" + keyName);
});
});
最佳答案
我找到了解决方案,即将"file"对象封装在
new Buffer()
很简单,但是很难找到!!
关于node.js - putObject 使 Nodejs 中的服务器上的对象变大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21056525/