最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Axios从URL获取文件并上传到s3

IT培训 admin 10浏览 0评论

Axios从URL获取文件并上传到s3

我正在尝试使用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);
      }
    });
  });
});

我尝试将responseEncoding修改为arraybuffer,并使用Buffer.from(response.data, 'binary').toString('base64')创建缓冲区,但无济于事。我想念什么?

回答如下:

Axios在utf8中对响应主体进行编码。您应该使用其他库,例如request

Axios从URL获取文件并上传到s3

我正在尝试使用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);
      }
    });
  });
});

我尝试将responseEncoding修改为arraybuffer,并使用Buffer.from(response.data, 'binary').toString('base64')创建缓冲区,但无济于事。我想念什么?

回答如下:

Axios在utf8中对响应主体进行编码。您应该使用其他库,例如request

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论