将节点js与Amazon S3和fakes3 gem一起使用。在尝试测试图像上传时,会出现以下错误:
write EPROTO 140735188193280:error:140770FC:SSLroutines:SSL23_GET_SERVER_HELLO:unknownprotocol:../deps/openssl/openssl/ssl/s23_clnt.c:797:
并且fakes3日志显示错误:
ERROR bad Request-Line `y??b?ot?2?\x00\x00n?/?+?0?,\x00??'\x00g?(\x00k?$?\x14?'.
代码如下,本地与fakes3:
一起使用function imageUploader(options, callback) {
const buffer = new Buffer(options.data_uri.replace(/^data:image\/\w+;base64,/, ''), 'base64');
const s3Client = knox.createClient({
key: 'key',
secret: 'secret',
bucket: 'bucket-name',
endpoint: 'localhost',
port: 10001,
secure: true,
style: 'path',
region: 'eu'
});
uploadImage(options, callback);
// put to a path in our bucket, and make readable by the public
function uploadImage(data, callback) {
const FILE_LENGTH = buffer.length;
const FILE_NAME = data.filename;
const FILE_TYPE = data.filetype;
const header = {
'Content-Length': FILE_LENGTH,
'Content-Type': FILE_TYPE,
'x-amz-acl': 'public-read'
};
console.log(FILE_NAME, header);
const req = s3Client.put('/images/'.concat(FILE_NAME), header);
req.on('response', (res) => {
if (res.statusCode === 200) {
console.log('Image saved on S3 to %s', req.url);
callback(null, req.url);
}
});
req.on('error', (error) => {
console.log('Problem saving image to S3:', error.message);
callback(error);
});
req.end(FILE_NAME);
}
}