1

I am using AWS S3 service to upload images. Yesterday I updated the SDK v1 to v2 and found that the image upload is failing with the following error:

operation error S3: PutObject, https response error StatusCode: 403, RequestID: XXXXXXXXXXX, HostID: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX, api error SignatureDoesNotMatch: The request signature we calculated does not match the signature you provided. Check your key and signing method.

UPDATED: I have aws credentials on my home folder in linux in .aws folder in the following format:

[default]
aws_access_key_id = XXXXXXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx

Here is the code:

package main

import (
    "context"
    "fmt"
    "io"
    "net/http"

    "github.com/aws/aws-sdk-go-v2/aws"
    awsconfig "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/feature/s3/manager"
    "github.com/aws/aws-sdk-go-v2/service/s3"
)

func main() {
    fileName := "test123.jpg"
    filePath := "/BUCKET_NAME/uploads/aman/2021/6/25/"

    res, err := http.Get("https://images.app.goo.gl/mpQ5nXYXjdUMKGgW7")
    if err != nil || res.StatusCode != 200 {
        // handle errors
    }
    defer res.Body.Close()
    UploadFileInS3Bucket(res.Body, fileName, filePath)
}

func UploadFileInS3Bucket(file io.Reader, fileName, filePath string) {
    cfg, err := awsconfig.LoadDefaultConfig(context.TODO(),
        awsconfig.WithRegion("REGION"),
    )
    client := s3.NewFromConfig(cfg)
    uploader := manager.NewUploader(client)
    uploadResp, err := uploader.Upload(context.TODO(), &s3.PutObjectInput{
        Bucket:      aws.String(filePath),
        Key:         aws.String(fileName),
        Body:        file,
        ContentType: aws.String("image"),
    })
    fmt.Println(uploadResp)
    fmt.Println(err)
}


I did not change any credentials/buckets/regions in my code.However if I run the code with SDK v1 then it works fine & images are uploading.

What is going wrong with the SDK v2 ?

4
  • Could it be a leading slash? stackoverflow.com/questions/30518899/… provide a full sample to reptoducce. Commented Jun 21, 2021 at 15:10
  • @DmitryHarnitski I have checked this. Leading slash does not seem to be the problem here. Commented Jun 22, 2021 at 2:19
  • Publish reproducible sample. It is not clear what are you sending into that function. V2 can be more strict for something. Commented Jun 24, 2021 at 18:33
  • @DmitryHarnitski I have added a reproducible example and details about credentials above. Commented Jun 25, 2021 at 11:09

1 Answer 1

3

After spending a couple of days, I came to know that SDK V2 takes following format for Bucket & Key field:

fileName := "uploads/2021/6/25/test123.jpg"
filePath := "BUCKET_NAME"

Basically for these fields there is vice versa behaviour in SDK V1 & V2. Above is the V2. Below is the V1:

fileName := "test123.jpg"
filePath := "/BUCKET_NAME/uploads/2021/6/25/"
Sign up to request clarification or add additional context in comments.

2 Comments

That actually makes sense. You are sending now Bucket = "BUCKET_NAME". I am sure V1 would accept that as well. SDK is less forgiving now for incorrect data. The error message is misleading though. I understand your confusion.
@DmitryHarnitski Yes the error message confused me. It should be correctly descriptive.

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.