ios 使用AFNetworking从S3获取公共文件并接收403

yc0p9oo0  于 2023-03-14  发布在  iOS
关注(0)|答案(1)|浏览(169)

我正在尝试从我们的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>}

建议会很棒!

wwodge7n

wwodge7n1#

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

相关问题