Axios从URL获取文件并上传到s3 [英] Axios get a file from URL and upload to s3
问题描述
我正在尝试使用axios.get从站点获取文件,然后将其直接上传到S3.但是,文件已损坏或编码不正确,并且上传后无法打开.文件类型范围从.jpg,.png到.pdf.这是我的代码:
I'm trying to get files from a site using axios.get, and then uploading it directly to S3. However, the files are corrupted or not encoded properly, and can't be opened after upload. File types range from .jpg, .png to .pdf. Here is my code:
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
修改为 arraybuffer
,并使用 Buffer.from(response.data,'binary').toString('base64')
,无济于事.我想念什么?
I've tried modifying responseType
to arraybuffer
and creating a buffer using Buffer.from(response.data, 'binary').toString('base64')
, to no avail. What am I missing?
推荐答案
我能够通过使用 arraybuffer
和 .putObject
函数而不是<代码>.上传代码>
I was able to get it working by using an arraybuffer
and the .putObject
function instead of .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) {
这篇关于Axios从URL获取文件并上传到s3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!