Search code examples
iphoneios5amazon-s3

iOS - Amazon S3 - Error Message: "The network connection was lost" when phone date set to some past date


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?


Solution

  • 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