0

I've been following many many threads online regarding how to properly get a presigned url from a lambda backend and then use it in React with Axios to upload an image to s3, but I just cant seem to get it to work. Here are some threads i followed:

I am either getting a 403 error (from axios.post), or Access denied (in Postman).

Here is my lambda code:

def getPresignedUrl(objName, contentType, fields=None, conditions=None, expiration=300):

    # Generate a presigned S3 POST URL
    s3_client = boto3.client("s3")
    try:
        response = s3_client.generate_presigned_url(
            "put_object",
            {
                "Bucket": os.environ["S3_BUCKET_NAME"],
                "Key": objName,
                "ContentType": contentType,
            },
            300,
        )
    except ClientError as e:
        logger.error("Error generating presigned url for s3: %s" % (e))
        raise
    except:
        logger.error("Unexpected error generating presigned url for s3!")

    # The response contains the presigned URL and required fields
    return response

Here is my frontend upload code:

const uploadImageToServer = (file) => {
    return new Promise(async (resolve) => {
      const obj = {
        objName: file.name,
        contentType: file.type,
      };
      let response = await axiosInstance.post("/getsignedurl", obj);

      console.log(response.data);
      const requestOptions = {
        method: "PUT",
        headers: {
          "Content-Type": file.type,
        },
        body: file,
      };
      const resp = await fetch(response.data, requestOptions);
      console.log(resp);

With the above code, I am getting the following error after making the fetch request:

{
    ok: false
    redirected: false
    status: 403
    statusText: "Forbidden"
    type: "cors",
    url: "wwww.presignedurl.com"
}

Any ideas for this? Public access is blocked on my s3 buckets, but that shouldn't matter if i'm using a presigned url right? Do i need to enable "private" ACL or "public-read" ACL?

Edit updated code

4
  • What are permissions for lambda? Plz validate the lambda is allowed PutObject, PutACL and the bucket can have public access Commented Aug 24, 2021 at 20:43
  • I am using AWS Cloudformation SAM templates, and yes put object, putacl is allowed. Cors is enabled on the s3 bucket, but I just checked (didnt think of checking this before) but all public access is blocked for s3 buckets on my AWS account. Is this the culprit? Commented Aug 24, 2021 at 21:48
  • all public access is blocked.. and yet you are trying to create a publicly available object. (I see you did edit the question). Does the issue prevails for private objects too? When you get a presigned url, can you put an object outside your app? (curl, postman,..)? Commented Aug 25, 2021 at 5:09
  • i'm only trying to allow my frontend to be able to upload to the s3 bucket. I dont think i need public access enabled for my bucket (seems like public access is for stuff like publicly hosted websites, and mine is an internal facing app). I wasnt able to get it working outside my app (using postman), i get the same issue: 403 error. presigned urls should still allow uploads to a private bucket... so i'm not suer whats wrong Commented Aug 25, 2021 at 16:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.