Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ErrorCode:AccessDenied, Message:AWS authentication requires a valid Date or x-amz-date header

my app is working fine in ios 6.. it uploading and downloading the data from amazon web server s3..but when i upgrade my ios 6 to ios 7...i got alert message " cannot connect to server" this error in log window

"Exception = AmazonServiceException { RequestId:5DC8AEF01DD9FB91, ErrorCode:AccessDenied, Message:AWS authentication requires a valid Date or x-amz-date header} ".

to solve this problem i upgrade my aws ios sdk 1.0.0 to aws ios sdk 1.6.1.and try to run my app it freezes for 10-12 second then app running.

so please can anyone tell me the solution how i remove " x-amz-date header" problem in aws ios sdk 1.0.0 and its alternate freeze problem in aws ios sdk 1.6.1..

like image 586
pradeep Avatar asked Sep 21 '13 13:09

pradeep


1 Answers

In AmazonSDKUtil.m, we have the following methods:

+(NSDate *)convertStringToDate:(NSString *)string usingFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:dateFormat];
    [dateFormatter setLocale:[AmazonSDKUtil timestampLocale]];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

    NSDate *parsed = [dateFormatter dateFromString:string];

    NSDate *localDate = [parsed dateByAddingTimeInterval:_clockskew];

    [dateFormatter release];

    return localDate;
}

+(NSString *)convertDateToString:(NSDate *)date usingFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    [dateFormatter setDateFormat:dateFormat];
    [dateFormatter setLocale:[AmazonSDKUtil timestampLocale]];

    NSDate *realDate =  [date dateByAddingTimeInterval:-1*_clockskew];

    NSString *formatted = [dateFormatter stringFromDate:realDate];

    [dateFormatter release];

    return formatted;
}

In older versions of the SDK, the locale and timezone weren't properly set to en_US and GMT. This may cause issues depending on your device locale and timezone settings. The latest version of the SDK fixes the issue. If, for some reason, you cannot update the SDK, you can modify AmazonSDKUtil.m and explicitly set locale and timezone values.

EDIT:

If you run the following code snippet on iOS 6 and iOS 7, you can see how locale setting affects date format.

NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss z";
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"PDT"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];

NSString *dateWithoutTimezoneAndLocale = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"Date 1: %@", dateWithoutTimezoneAndLocale);

dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

NSString *dateWithTimezoneAndLocale = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"Date 2: %@", dateWithTimezoneAndLocale);

On iOS 6

Date 1: Wed, 25 Sep 2013 16:25:29 PDT
Date 2: Wed, 25 Sep 2013 23:25:29 GMT

On iOS 7

Date 1: Wed, 25 Sep 2013 16:24:11 GMT-7
Date 2: Wed, 25 Sep 2013 23:24:11 GMT

As you stated before, the behavior of NSDateFormatter changed in iOS 7; however, the root cause of this issue is that you are not explicitly setting the locale to en_US. When the locale is set to something other than en_US, it may cause an issue. This is why we explicitly set the locale in the latest version of our SDK so that it runs on devices with any locale settings.

Hope this makes sense,

like image 96
Yosuke Matsuda Avatar answered Oct 22 '22 17:10

Yosuke Matsuda