使用AWS SDK V2在GO中上传图像失败。

huangapple go评论82阅读模式
英文:

Image upload failed using AWS SDK V2 in GO

问题

我正在使用AWS S3服务上传图片。昨天我将SDK v1更新为v2,发现图片上传失败,并显示以下错误:

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.

更新
我在Linux的主文件夹中的.aws文件夹中以以下格式存储了AWS凭证:

[default]
aws_access_key_id = XXXXXXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx

以下是代码:

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 {
		// 处理错误
	}
	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)
}

我没有在代码中更改任何凭证/存储桶/区域设置。然而,如果我使用SDK v1运行代码,则可以正常工作并且图片可以上传。

SDK v2出了什么问题?

英文:

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 ?

答案1

得分: 3

在花了几天的时间后,我了解到SDK V2对于Bucket和Key字段采用以下格式:

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

基本上,SDK V1和V2在这些字段上有相反的行为。上面是V2的格式,下面是V1的格式:

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

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/"

huangapple
  • 本文由 发表于 2021年6月21日 19:58:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/68067571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定