AWS S3 Golang SDK – 上传文件 – 错误的区域

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

AWS S3 Golang SDK - uploading files - wrong region

问题

我遇到了一个问题,我正在尝试通过官方的Golang AWS SDK将文件上传到S3。

我正在针对一个指定为北加利福尼亚地区(us-west-1)的存储桶进行操作。我可以在S3资源管理器中看到它,但是当我运行put操作时,我收到以下错误:

BucketRegionError: 错误的区域,存储桶不在'Northern California'区域

这是我设置连接的方式:

creds := credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key, token)
_,err := creds.Get()

config := &aws.Config{
	Region          :aws.String("us-west-1"),
	Endpoint        :aws.String("s3.amazonaws.com"),
	S3ForcePathStyle:aws.Bool(true),
	Credentials     :creds,
	//LogLevel        :0,
}

s3_client := s3.New(session.New(config))

params := &s3.PutObjectInput{
	Bucket       :aws.String(p_bucket_name_str),         
	Key          :aws.String(p_target_file__s3_path_str),
	ACL          :aws.String("public-read"),
	Body         :file_bytes,
	ContentLength:aws.Int64(size),
	ContentType  :aws.String(file_type),
	Metadata     :map[string]*string{
	     "Key":aws.String("MetadataValue"),
	},
}

result,err := p_s3_client.PutObject(params)

有什么建议或修复方法将会非常有帮助,谢谢。

英文:

I'm having an issue where I'm trying to upload a file to S3 via the official Golang AWS SDK.

I'm targeting a bucket that's specified to be in Northern California region (us-west-1). I can see it in the S3 explorer; however, when I run the put operation I get the following error:

BucketRegionError: incorrect region, the bucket is not in 'Northern California' region

This is how I am setting up my connection:

creds := credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key, token)
_,err := creds.Get()

config := &aws.Config{
	Region          :aws.String("us-west-1"),
	Endpoint        :aws.String("s3.amazonaws.com"),
	S3ForcePathStyle:aws.Bool(true),
	Credentials     :creds,
	//LogLevel        :0,
}

s3_client := s3.New(session.New(config))

params := &s3.PutObjectInput{
	Bucket       :aws.String(p_bucket_name_str),         
	Key          :aws.String(p_target_file__s3_path_str),
	ACL          :aws.String("public-read"),
	Body         :file_bytes,
	ContentLength:aws.Int64(size),
	ContentType  :aws.String(file_type),
	Metadata     :map[string]*string{
	     "Key":aws.String("MetadataValue"),
	},
}

result,err := p_s3_client.PutObject(params)

Any suggestion or fix would be of huge help, thank you.

答案1

得分: 2

aws-sdk-go提供了一个s3manager工具,使得这个过程更加方便。你可以按照以下方式上传文件:

creds := credentials.NewStaticCredentials(AccessKey, SecretKey, "")
sess := session.New(&aws.Config{
    Credentials: creds,
    Region:      &Region,
})

uploader := s3manager.NewUploader(sess)
uploader.Upload(&s3manager.UploadInput{
    ACL:         aws.String(acl),
    Bucket:      aws.String(bucket),
    Key:         aws.String(key),
    ContentType: aws.String(contentType),
    Body:        r,
})

请注意,上述代码中的AccessKeySecretKeyRegionaclbucketkeycontentType需要根据你的具体情况进行替换。

英文:

the aws-sdk-go offers a s3manager tool to make this work more convenient. You can upload a file like below:

creds := credentials.NewStaticCredentials(AccessKey, SecretKey, "")
sess := session.New(&aws.Config{
    Credentials: creds,
    Region:      &Region,
})

uploader := s3manager.NewUploader(sess)
uploader.Upload(&s3manager.UploadInput{
    ACL:         aws.String(acl),
    Bucket:      aws.String(bucket),
    Key:         aws.String(key),
    ContentType: aws.String(contentType),
    Body:        r,
})

答案2

得分: 2

您看到此错误是因为您认为您的存储桶存在于us-west-1区域,而您的请求正在发送到us-east-1区域。对于AWS SDK for Go,端点s3.amazon.com映射到us-east-1区域。

使用curl命令curl -I "https://<bucketname>.s3.amazonaws.com"来验证您的存储桶实际所在的区域,使用头部x-amz-bucket-region

一旦您获得了正确的区域,从配置中删除Endpoint值,您的请求应该开始工作。SDK将根据提供的区域自动确定端点URL。Endpoint值正在覆盖SDK为服务查找的URL。

当请求发送到s3.amazon.com端点时,SDK不会自动切换区域并重新发送请求,即使存储桶存在于不同的区域中。

英文:

You're seeing this error because you think your bucket exists in us-west-1 region, and your request is being made to the us-east-1 region. For the AWS SDK for Go the endpoint s3.amazon.com maps to the us-east-1 region.

Use the curl command curl -I &quot;https://&lt;bucketname&gt;.s3.amazonaws.com&quot; to verify the region your bucket is actually in with the header x-amz-bucket-region

Once you have the correct region remove the Endpoint value from your config your request should start working. The SDK will determine the endpoint URL automatically based on the region provided. The Endpoint value is overriding the URL the SDK would look up for the service.

The SDK does not automatically switch regions and resend the request when the request was made to the s3.amazon.com endpoint when the bucket exists in a different region.

答案3

得分: 1

错误是由于终端点引起的。默认情况下,终端点“s3.amazonaws.com”是用于us-east-1区域的。如果您想为us-west-1创建存储桶,您必须将终端点设置为“s3-us-west-1.amazonaws.com”。

英文:

The error is because of end-point. End-point "s3.amazonaws.com" is for us-east-1 region by default. If you want to create bucket for us-west-1 you have to put end-point as "s3-us-west-1.amazonaws.com"

huangapple
  • 本文由 发表于 2016年4月7日 09:25:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/36465106.html
匿名

发表评论

匿名网友

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

确定