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

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.

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,
})

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 "https://<bucketname>.s3.amazonaws.com" 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.

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"