JAN
JAN

Reputation: 21875

Get URL of uploaded file to S3 (after the file has been uploaded)

Given a link that when the user hits it a PDF is downloaded ,

I want to upload the file to S3 and then get an Amazon S3 URL that would be public (I don't want the user to see the real Link , so that's why I'd rather upload it to S3).

Consider the code :

module.exports = class S3Uploader {
  uploadPdfFromURLToS3 = urlToUpload => {
    import aws from "aws-sdk";
    const request = require("request");
    const s3 = new aws.S3();
    const config = require("config");
    var uuidv4 = require("uuid/v4");
    var filename = uuidv4() + ".pdf";

    aws.config.update({
      accessKeyId: config.get("-------------"),
      secretAccessKey: config.get("-----------------")
    });

    var promise = new Promise((resolve, reject) => {
      return request({ url: urlToUpload, encoding: null }, function(
        err,
        res,
        body
      ) {
        if (err) return reject({ status: 500, error: err });

        return resolve({ status: 200, body: body });
      });
    });

    promise.then(pdf => {
      if (pdf.status == 200) {

        s3.putObject(
          {
            Bucket: "-----Your-Bucket-Name",
            Body: pdf.body,
            Key: filename,
            ACL: "public-read"
          },
          (err, data) => {
            if (err) console.log(err);
            else 
            {
                console.log("uploaded");    
                // Get the S3 Public link ????
            }
          }
        );
      }
    });
  };
};

How can I get the link after the file has been uploaded successfully , in the callback ?

Upvotes: 3

Views: 6857

Answers (3)

Parshav
Parshav

Reputation: 9

**TRY THIS **The main change is in s3.putObject()

  module.exports = class S3Uploader {
  uploadPdfFromURLToS3 = urlToUpload => {
    import aws from "aws-sdk";
    const request = require("request");
    const s3 = new aws.S3();
    const config = require("config");
    var uuidv4 = require("uuid/v4");
    var filename = uuidv4() + ".pdf";

    aws.config.update({
      accessKeyId: config.get("-------------"),
      secretAccessKey: config.get("-----------------")
    });

    var promise = new Promise((resolve, reject) => {
      return request({ url: urlToUpload, encoding: null }, function(
        err,
        res,
        body
      ) {
        if (err) return reject({ status: 500, error: err });

        return resolve({ status: 200, body: body });
      });
    });

    promise.then(pdf => {
      if (pdf.status == 200) {

        s3.putObject(
          {
            Bucket: "-----Your-Bucket-Name",
            Body: pdf.body,
            Key: filename,
            ACL: "public-read"
          },async(err,data)=>{if(err){console.log("error")}
           else
           console.log(data.location) //get pdf url
         }

        );
      }
    });
  };
};

Upvotes: -1

disp_name
disp_name

Reputation: 1488

You can try adding $(data.Location) in your console.log("uploaded") line.

console.log("uploaded. $(data.Location)");

Upvotes: 0

David Webster
David Webster

Reputation: 2331

You can build up the url using string concatentation.

https://your-bucket-name.s3-eu-west-1.amazonaws.com/filename

Make sure you are using the correct region.

Upvotes: 2

Related Questions