1

我正在尝试从我们的 S3 存储下载 PDF 文件。这些文件是公开的,可以通过浏览器访问。

使用 AFNetworking 时,我收到 403。代码如下所示

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:self.pdfUrl]];

void (^success)(AFHTTPRequestOperation*, id) = ^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"WE HAS SUCCESS :D");
};

void (^failure)(AFHTTPRequestOperation*, id) = ^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"failure :((. OPERATION:\n\n%@ \n\nResponseObject:\n\n%@",operation,responseObject);
};

NSURLRequest *request = [client requestWithMethod:@"GET" path:@"" parameters:nil];
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:success failure:failure];

operation.outputStream = [NSOutputStream outputStreamToFileAtPath:[TTConstants temporaryFilePath] append:NO];

[client enqueueHTTPRequestOperation:operation];

导致输出:

failure :((. OPERATION:
<AFHTTPRequestOperation: 0x95a2f40, state: isFinished, cancelled: NO request: 
<NSMutableURLRequest https://s3.amazonaws.com/com.turtletail.java-bucket/dbda2ccb-91db-4672-9ad4-288dd313ef8c/pdfnumberone.pdf/>,
response: <NSHTTPURLResponse: 0xa692480>> 

ResponseObject:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 403"
UserInfo=0xa6bda40 {AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest https://s3.amazonaws.com/com.turtletail.java-bucket/dbda2ccb-91db-4672-9ad4-288dd313ef8c/pdfnumberone.pdf/>,
NSErrorFailingURLKey=https://s3.amazonaws.com/com.turtletail.java-bucket/dbda2ccb-91db-4672-9ad4-288dd313ef8c/pdfnumberone.pdf/,
NSLocalizedDescription=Expected status code in (200-299), got 403,
AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xa692480>}

建议会很棒!

4

1 回答 1

2

不要使用完整的 PDF URL 作为 URL,baseURL因为AFHTTPClient这会导致最终 URL/在末尾有一个额外的 URL。相反,将 设置baseURLhttps://s3.amazonaws.com/com.turtletail.java-bucket/dbda2ccb-91db-4672-9ad4-288dd313ef8c,然后在请求中将路径设置为pdfnumberone.pdf

于 2013-05-18T09:27:38.930 回答