问题描述:

I am using AWS-iOS-SDK-1.1.0 to upload files to Amazon S3. Also am using Amazon S3 Anonymous Token Vending Machine.

I am getting the following error message when I tried to upload a file to Amazon S3 from my iPhone.

AmazonServiceRequest didFailWithError :

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."

UserInfo=0x7290 {NSErrorFailingURLStringKey=https://.....amazonaws.com/img.jpg, NSErrorFailingURLKey=https://.....amazonaws.com/img.jpg,

NSLocalizedDescription=The network connection was lost.,

NSUnderlyingError=0x72a4320 "The network connection was lost."}

Here is the code I am using to upload to S3

if ([[AmazonClientManager validateCredentials] wasSuccessful]) {

s3PutRequest = [[S3PutObjectRequest alloc] initWithKey:[NSString stringWithFormat:@"images/%@.jpg", [user filename]] inBucket:@"bucket"];

[s3PutRequest setCannedACL:[S3CannedACL publicRead]];

[s3PutRequest setFilename:[[self videoURL] path]];

[s3PutRequest setDelegate:self];

[[AmazonClientManager s3] putObject:s3PutRequest];

}

else {

UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable to Upload the video. Please try again!!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];

[error show];

}

#pragma mark - Amazon Service Request Delegate Methods

- (void)request:(AmazonServiceRequest *)request didReceiveResponse:(NSURLResponse *)aResponse

{

NSLog(@"didReceiveResponse : %@", aResponse);

}

- (void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)aResponse

{

NSLog(@"didCompleteWithResponse : %@", aResponse);

response = aResponse;

NSLog(@"HTTP Status Code:%i", response.httpStatusCode);

if (response.isFinishedLoading && response.httpStatusCode == 200) {

[self saveFile];

}

else {

[self uploadFailed:[exception reason]];

}

}

- (void)request:(AmazonServiceRequest *)request didReceiveData:(NSData *)data

{

NSLog(@"didReceiveData");

}

- (void)request:(AmazonServiceRequest *)request didSendData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite

{

float percentWritten = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;

int percentageWritten = (int)(percentWritten * 100);

[uploadProgress setProgress:percentWritten];

[uploadPercentage setText:[NSString stringWithFormat:@"%i%%", percentageWritten]];

}

- (void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)theError

{

NSLog(@"didFailWithError : %@", theError);

error = theError;

[self uploadFailed:[error localizedDescription]];

}

- (void)request:(AmazonServiceRequest *)request didFailWithServiceException:(NSException *)theException

{

NSLog(@"didFailWithServiceException : %@", theException);

exception = theException;

[self uploadFailed:[exception reason]];

}

I am able to reproduce this error by resetting the iPhone to some past date (example: 22 aug 2010). If i reset it back to current date, am able to upload the file to Amazon S3.

How can I get to know the exact error message?

网友答案:

As mentioned, the Amazon SDK requires the client and server's time to be in synch with a maximum allowable difference of 15 minutes.

Since this cannot be guaranteed on the user's device, you can get around it with this (Amazon endorsed) solution: http://mobile.awsblog.com/post/Tx2KKPVXE69XJAO/Managing-Device-Time-with-the-AWS-Mobile-SDKs

网友答案:

This is where the AWS SDK falls short, in my experience anyway. There are some checks done server side by S3 that we don't know about, one of them is that the UTC time on the device MUST be synced with S3's UTC time. I haven't found how much buffer room you get, but I do know 1 day is too much.

If the checks don't pass, as far as I can tell S3 cuts off the connection from the remote end. The client isn't notified why so it assumes the connection was simply lost.

I know this isn't the response you wanted, but it's the best I can provide.

Source: I've encountered issues the the time being wrong and having this happen, as well as incorrect eTags being submitted with multipart upload sessions, the behavior of the termination is the same for both (The network connection was lost.)

相关阅读:
Top