0

Trying to request an image from Google CDN and upload it to S3. Using the https://github.com/request/request library and Node / Express;

A little confused how to handle payload coming back from Google CDN. enter image description here

The image comes back in the body field and in encoded. Not sure how it is encoded.

Given a URL to a Google CDN:

const fileURL = https://lh4.googleusercontent.com/EWE1234rFL006WfQKuAVrsYMOiKnM6iztPtLgXM5U…3i26LoPHQwPTQME7ne3XoMriKVjUo3hrhwWw1211223

  request(fileURL, (err, res, body) => {

     //NOT sure how to handle the response here??
     //Trying base64
     fs.writeFileSync(`.tmp/file1.png`, body, {encoding: 'base64'});

     //Trying Binary
     fs.writeFileSync(`.tmp/file.png`, body, {encoding: 'binary'});
  }

body comes back as:

�PNG↵↵IHDRv&vл� IDATx�}���<z�f���];��o]��A�N�.po�/�/R���..............

1). Request an image from googleusercontent Google CDN (image was originally pasted in a Google Doc)

2). Create an image file and write to disk on the Server.

Neither of the fs.writeFileSync seem to produce a readable image file.

Any advice on handling this would be awesome..

GN.
  • 8,672
  • 10
  • 61
  • 126
  • What are you using to upload to S3? You can most probably pass the body as it is. – Daniel Bang Oct 31 '16 at 23:05
  • Sorry, I should emphasize that the issue is that the image being written is not readable. Going to edit my post. Trying to understand how to handle the string returned from Google and create a file from it. – GN. Oct 31 '16 at 23:54

1 Answers1

0

Pass the response as the body to your S3 upload.

var request = require('request'),
    fs = require('fs'),
    aws = require('aws-sdk'),
    s3 = new aws.S3(),
    url = 'https://lh4.googleusercontent.com/-2XOcvsAH-kc/VHvmCm1aOoI/AAAAAAABtzg/SDdN1Vg5FFs/s346/14%2B-%2B1';

# Store in a file
request(url).pipe(fs.createWriteStream('file.gif'));

request(url, {encoding: 'binary'}, function(error, response, body) {
    # Another way to store in a file
    fs.writeFile('file.gif', body, 'binary', function(err) {});

    # Upload to S3
    s3.upload({
        Body: body,
        Bucket: 'BucketName',
        Key: 'file.gif',
    }, function(err, data) {});
});
Daniel Bang
  • 715
  • 6
  • 21