iOS 如何在 AWS 上上传图片?

How to upload image on AWS in iOS?

AWSStaticCredentialsProvider *credentialsProvider = [[AWSStaticCredentialsProvider alloc] initWithAccessKey:@"AKIA..........." secretKey:@"6FuqRt................."];

AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionEUWest2 credentialsProvider:credentialsProvider];

[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
self.transferManager = [AWSS3TransferManager defaultS3TransferManager];

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"qr" ofType:@"jpeg"];
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = @"my-flologic-bucket";
uploadRequest.key = @"monday.jpeg";
uploadRequest.body = [NSURL fileURLWithPath:filePath];

[[self.transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task)
{
    if (task.error)
    {
        NSLog(@"Error: %@", task.error);
    }
    else {
        NSLog(@"The file uploaded successfully.");

    }
    return nil;
}];

错误:

Bucket=my-flologic-bucket, Endpoint=my-flologic-bucket.s3-us-west-2.amazonaws.com, Message=The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint., Code=PermanentRedirect, RequestId=E7471ACE4D6049F8}

根据 AWS Regions and Endpoints 中的区域和端点文档,以下区域端点对 S3 us-west-2 有效:

  • s3.us-西-2.amazonaws.com
  • s3-美国西部-2.amazonaws.com
  • s3.dualstack.us-西-2.amazonaws.com

您可以尝试使用 https://<bucket-name>.s3.us-west-2.amazonaws.com 而不是 s3-us-west-2.amazonaws.com。我知道 s3-us-west-2.amazonaws.com 也是有效的,但我一直使用前一个没有任何问题。