我正在尝试使用 axios.get 从网站获取文件,然后将其直接上传到 S3。但是,文件已损坏或编码不正确,上传后无法打开。文件类型范围从 .jpg、.png 到 .pdf。这是我的代码:
axios.get(URL, {
responseEncoding: 'binary',
responseType: 'document',
}).then((response) => {
return new Promise((resolve, reject) => {
const s3Bucket = nconf.get('AWS_S3_BUCKET');
s3.upload({
'ACL': 'public-read',
'Body': response.data,
'Bucket': s3Bucket,
'Key': `static/${filePath}/${fileManaged.get('filename')}`,
}, function(err) {
if (err) {
return reject(err);
}
});
});
});
我尝试过修改responseType
to arraybuffer
并使用创建缓冲区Buffer.from(response.data, 'binary').toString('base64')
,无济于事。我缺少什么?
我能够通过使用让它工作arraybuffer
和.putObject
函数而不是.upload
axios.get(encodeURI(url), {
responseType: 'arraybuffer',
}).then((response) => {
s3.putObject({
'ACL': 'public-read',
'Body': response.data,
'Bucket': s3Bucket,
'Key': `static/${filePath}/${fileManaged.get('filename')}`,
} function(err) {
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)