我试图用AWS-SDK-Core Ruby Gem删除上传的图像文件。

我有以下代码:

require 'aws-sdk-core'

def pull_picture(picture)
    Aws.config = {
        :access_key_id => ENV["AWS_ACCESS_KEY_ID"],
        :secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"],
        :region => 'us-west-2'
    }

    s3 = Aws::S3::Client.new

    test = s3.get_object(
        :bucket => ENV["AWS_S3_BUCKET"],
        :key => picture.image_url.split('/')[-2],   
    )
end

然而,我得到以下错误:

您试图访问的桶必须使用指定的端点进行寻址。请将所有将来的请求发送到此端点。

我知道这个区域是正确的,因为如果我把它改为us-east-1,会出现以下错误:

指定的密钥不存在。

我哪里做错了?


当前回答

我住在英国,一直在尝试“美国西部2”地区。所以重定向到'eu-west-2'。S3的正确区域是'eu-west-2'

其他回答

对于使用@aws-sdk/client-s3的用户,请确保在发送命令之前将桶的区域提供给客户端。 使用CLI获取:

$ aws s3api get-bucket-location --bucket <bucket_name>
{
    "LocationConstraint": "ca-central-1"
}
const client = new S3Client({ region: "ca-central-1", credentials...

虽然S3桶是全局的,但在访问桶时,我们需要给出区域。我在.netcore中得到错误,一旦我在下面的代码中添加了区域,它就开始工作了。

var s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USWest2);

在c#中,你可以做以下检查,我假设类似的代码在其他sdk中是可能的:

        var client = new AmazonS3Client(
            credentials.AccessKey,
            credentials.ClientSecret,
            new AmazonS3Config{}
        );

        var bucketLocationRequest = new GetBucketLocationRequest
        {
            BucketName = amazonS3Bucket.BucketName
        };

        var response = await client.GetBucketLocationAsync(bucketLocationRequest);
        var region = response.Location;

        var regionEndpoint = region != null ? RegionEndpoint.GetBySystemName(region.Value) : RegionEndpoint.EUCentral1;
       
        var clientWithRegion = new AmazonS3Client(
            credentials.AccessKey,
            credentials.ClientSecret,
            new AmazonS3Config
            {
                RegionEndpoint = regionEndpoint
            }
        );

对于许多S3 API包(我最近在npm S3包中遇到了这个问题),你可能会遇到区域被假设为美国标准的问题,如果你选择在该区域之外托管一个桶,通过名称查找将要求你显式定义该区域。

经过长时间的寻找,我找到了一个可行的解决方案。这个问题是因为错误的地区代码。

下面是区域代码列表,设置适当的一个,您的问题将得到解决。

Code                         Name
US East (Ohio)               us-east-2

US East (N. Virginia)       us-east-1

US West (N. California)     us-west-1

US West (Oregon)            us-west-2

Asia Pacific (Hong Kong)    ap-east-1

Asia Pacific (Mumbai)       ap-south-1

Asia Pacific (Osaka-Local)  ap-northeast-3

Asia Pacific (Seoul)        ap-northeast-2

Asia Pacific (Singapore)    ap-southeast-1

Asia Pacific (Sydney)       ap-southeast-2

Asia Pacific (Tokyo)        ap-northeast-1

Canada (Central)            ca-central-1

Europe (Frankfurt)          eu-central-1

Europe (Ireland)            eu-west-1

Europe (London)             eu-west-2

Europe (Paris)             eu-west-3

Europe (Stockholm)         eu-north-1

Middle East (Bahrain)      me-south-1

South America (São Paulo)   sa-east-1

你可以在点击桶名的右上角找到你的区域代码。

查看模式详情单击