Ad

Upload Image To S3 From Url

I am trying to upload a image, for which i have a url into s3

I want to do the same without downloading the image to local storage

filePath = imageURL;
          let params = {
            Bucket: 'bucketname',
            Body: fs.createReadStream(filePath),
            Key: "folder/" + id + "originalImage"
          };
          s3.upload(params, function (err, data) {
            if (err) console.log(err);
            if (data) console.log("original image success");
          });

expecting a success but getting error : myURL is a https publicly accesible url.

[Error: ENOENT: no such file or directory, open '<myURL HERE>']
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path:
   '<myURL HERE>' }
Ad

Answer

There are two ways to place files into an Amazon S3 bucket:

  • Upload the contents via PutObject(), or
  • Copy an object that is already in Amazon S3

So, in the case of your "web accessible Cloudinary image", it will need to be retrieved, then uploaded. You could either fully download the image and then upload it, or you could do some fancy stuff with streaming bodies where the source image is read into a buffer and then written to S3. Either way, the file will need to be "read" from Cloudinary the "written" to S3.

As for the "web accessible Amazon S3 link", you could use the CopyObject() command to instruct S3 to directly copy the object from the source bucket to the destination bucket. (It also works within a bucket.)

Ad
source: stackoverflow.com
Ad