I am trying to download PDF files from our S3 storage. The files are public, and can be accessed through a browser.
When using AFNetworking I receive a 403. The code looks like this
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];
Resulting in the outputs:
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>}
Suggestions would be super!
Don't use the full PDF URL as the baseURL
of the AFHTTPClient
as this results in the final URL having an extra /
at the end. Instead, set the baseURL
to https://s3.amazonaws.com/com.turtletail.java-bucket/dbda2ccb-91db-4672-9ad4-288dd313ef8c
and then in the request set the path to pdfnumberone.pdf
.