Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios get a file from URL and upload to s3

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);
      }
    });
  });
});

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?

like image 239
John Xu Avatar asked May 05 '20 03:05

John Xu


Video Answer


2 Answers

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) {
like image 147
John Xu Avatar answered Oct 28 '22 13:10

John Xu


Axios encodes the response body in utf8.
You should use other library like request.

like image 1
HelloWorld Avatar answered Oct 28 '22 13:10

HelloWorld