2013-05-18 85 views
1

我試圖從S3存儲中下載PDF文件。這些文件是公開的,可以通過瀏覽器訪問。使用AFNetworking從S3獲取公用文件並接收403

當使用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>} 

建議,將超強!

+0

我實際上無法在瀏覽器中查看該文件。你確定它像你想象的那樣可用嗎? – Wain

+0

是的,我改變了SO的PDF的名稱。也許應該提到這一點。 –

回答

2

請勿使用完整的PDF URL作爲AFHTTPClientbaseURL,因爲這會導致最終的網址在最後有一個額外的/。相反,請將baseURL設置爲https://s3.amazonaws.com/com.turtletail.java-bucket/dbda2ccb-91db-4672-9ad4-288dd313ef8c,然後在請求中將路徑設置爲pdfnumberone.pdf

+0

完美!事後看來,我應該從日誌中發現自己... –

相關問題