1

I am trying to retrieve images from Amazon S3 for my React project. However, my react project setup seems to contradict with aws-sdk which can be traced back to this post.

Therefore, I am trying to have AWS lambda as my MiddleWare to retrieve the Image. I utilised s3.getSignedUrlPromise() to get a signed url of the image, then pass the url back to the frontend. In the local environment, this approach works and I can render the requested image with the signed url retrieved from backend.

However, when I deployed the lambda function, the signed url reads 403 forbidden file. Hence I believe that the signed url is bound to the domain which is localhost. The question is, is there a way to tell getSingedUrlPromise() which host is the signed url supposed to be bound to?

I also attempted to use s3.getObject() api but I had a very hard time trying to convert the Buffer object to Image.

inkredusk
  • 919
  • 4
  • 16
ILoveET
  • 109
  • 1
  • 6
  • which version of the aws-sdk are you using ? – inkredusk Mar 31 '23 at 12:32
  • Did you check [this question](https://stackoverflow.com/questions/46869962/getting-403-forbidden-when-uploading-to-s3-with-a-signed-url)? I've resolved same situation referring to that. – iamippei Mar 31 '23 at 12:38

1 Answers1

0

Assuming you are using @aws-sdk/client-s3, I had implemented a similar example to fetch image from my S3 bucket using the below code:

const AWS = require('aws-sdk');
//*/ get reference to S3 client 
var s3 = new AWS.S3();
exports.handler = (event, context, callback) => {
    var params = {
      "Bucket": "your-bucket-name",
      "Key": "your-key"
    };
    s3.getObject(params, function(err, data){
       if(err) {
           callback(err, null);
       } else {
           let base64String= data.Body.toString('base64');
           let src = "data:image/jpeg;base64,"+base64String;
           
           let response = {
             "statusCode": 200,
             "base64image": base64String
           };

           callback(null, response);
       }
    });
    
};
inkredusk
  • 919
  • 4
  • 16