我正在尝试使用AFAmazonS3Manager将一些文件上传到亚马逊s3,但是我有403禁止错误,我尝试列出存储桶中的所有对象,同样的错误。

存储桶对所有对象都是读/写的。

代码:

AFAmazonS3Manager *s3Mamanager=[[AFAmazonS3Manager alloc]initWithAccessKeyID:@"XXXXXX" secret:@"xxxxxx"];

[s3Mamanager getBucket:@"aBucket" success:^(id responseObject) {

} failure:^(NSError *error) {

}];

错误 :
 Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed:
 forbidden (403)" UserInfo=0x170272580
 {AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse:
 0x1782212e0> { URL: https://s3.amazonaws.com/aBucket } { status code:
 403, headers {
     "Content-Type" = "application/xml";
     Date = "Wed, 05 Mar 2014 21:01:55 GMT";
     Server = AmazonS3;
     "Transfer-Encoding" = Identity;
     "x-amz-id-2" = "xxxxxxxxxxxxxx";
     "x-amz-request-id" = xxxxxxxxxxxxxx; } }, NSLocalizedDescription=Request failed: forbidden (403),
 NSErrorFailingURLKey=https://s3.amazonaws.com/aBucket}

我使用传输(mac)和Amazon s3 iOS API的示例,并且工作正常,我想使用NSURLSession上载文件(通过AFNetworking)。

PS:在AFAmazonS3Manager中,某些竞争块的参数“很长”,但是AFNetwoking使用NSIteger,所以我对其进行了修改,我不认为这是问题,但我确实对此非常满意。

最佳答案

失败的原因是因为在AFAmazonS3RequestSerializer中如何计算签名存在错误。它正在尝试使用以下方法创建签名:

NSString *canonicalizedResource = [NSString stringWithFormat:@"/%@%@", self.bucket, request.URL.path];

那时,self.bucket为null,而request.URL.path包含存储桶。您可以通过以下方法解决此问题:
AFAmazonS3Manager *s3Manager = [[AFAmazonS3Manager alloc]
                                initWithAccessKeyID:@"<accessKeyID>" secret:@"<secret>"];
[[s3Manager requestSerializer] setBucket:@"<YOUR BUCKET HERE>"];
[s3Manager getBucket:@"/" success:^(id responseObject) {}
             failure:^(NSError *error) {}];

或这个:
AFAmazonS3Manager *s3Manager = [[AFAmazonS3Manager alloc]
                                initWithAccessKeyID:@"<accessKeyID>" secret:@"<secret>"];
[[s3Manager requestSerializer] setBucket:@"<YOUR BUCKET HERE>"];
[s3Manager enqueueS3RequestOperationWithMethod:@"GET" path:@"/" parameters:nil
                                       success:^(id responseObject) {}
                                       failure:^(NSError *error) {}];

关于ios - 带有AFAmazonS3Manager + AFNetworking的Amazon s3,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22209582/

10-12 20:06