【问题标题】:ErrorCode:AccessDenied, Message:AWS authentication requires a valid Date or x-amz-date headerErrorCode:AccessDenied,消息:AWS 身份验证需要有效的 Date 或 x-amz-date 标头
【发布时间】:2014-01-03 01:16:02
【问题描述】:

我的应用程序在 ios 6 中运行良好..它从亚马逊网络服务器 s3 上传和下载数据..但是当我将我的 ios 6 升级到 ios 7 时...我收到警报消息“无法连接到服务器”这个日志窗口中的错误

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

为了解决这个问题,我将我的 aws ios sdk 1.0.0 升级到 aws ios sdk 1.6.1。并尝试运行我的应用程序,它会冻结 10-12 秒然后应用程序运行。

所以请谁能告诉我解决方案我如何在 aws ios sdk 1.0.0 中删除“x-amz-date header”问题及其在 aws ios sdk 1.6.1 中的替代冻结问题..

【问题讨论】:

  • 我对 aws ios sdk 1.4 有同样的问题
  • 我也有同样的问题;查看发送到亚马逊的数据时,我注意到日期标头的格式在 iOS6 和 iOS7 之间略有不同,这很可能是导致亚马逊结束日期解析器的原因。 iOS6:日期:2013 年 9 月 24 日星期二 07:58:18 GMT+10:00 iOS7:日期:2013 年 9 月 24 日星期二 07:58:18 GMT+10 所以 iOS7 中似乎缺少 TZ 偏移的小时部分。
  • 那么你是怎么解决的?,有什么方法可以解决 ios7 中的这个小时部分吗?

标签: ios xcode amazon-web-services amazon-s3 ios7


【解决方案1】:

AmazonSDKUtil.m中,我们有以下方法:

+(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;
}

在旧版本的 SDK 中,语言环境和时区未正确设置为 en_USGMT。根据您的设备区域设置和时区设置,这可能会导致问题。最新版本的 SDK 修复了该问题。如果由于某种原因无法更新 SDK,您可以修改 AmazonSDKUtil.m 并显式设置语言环境和时区值。

编辑:

如果您在 iOS 6 和 iOS 7 上运行以下代码 sn-p,您可以看到语言环境设置如何影响日期格式。

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);

在 iOS 6 上

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

在 iOS 7 上

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

正如你之前所说,NSDateFormatter 的行为在 iOS 7 中发生了变化;但是,此问题的根本原因是您没有将语言环境显式设置为en_US。当语言环境设置为 en_US 以外的其他值时,可能会导致问题。这就是为什么我们在最新版本的 SDK 中明确设置区域设置,以便它可以在具有任何区域设置的设备上运行。

希望这是有道理的,

【讨论】:

  • 我不相信这是一个解决方案 - 我尝试了两个项目,但都以这个新代码失败了。我仍然同意@patschiboy 的观点,即错误在于 ios7 提供的日期格式
  • 正确设置区域设置和时区应该可以解决我编辑的答案中指出的日期/时间格式问题。希望这是有道理的。
  • 感谢您提供额外的详细信息 - 如果有任何问题,我会尝试报告,但这是有道理的
  • Yosuke - 作为进一步的信息,Apple 回复我并声明关于 ios7:每个语言环境支持两种长度的本地化 GMT 格式,长(例如“GMT-08:00”)和短(例如“GMT-8”)。现在明确指定“z”回退到短本地化 GMT 格式。尽管如此,不同地区之间的外观可能会有所不同。如果你想有一个不变的形式,那么你应该使用 ISO 8601 风格之一,模式字符使用 X 或 x;查看unicode.org/reports/tr35/…的区域部分
  • 终于开始正确测试了,您的 GMT 和 en_US 方法(如日期 2 示例所示)看起来不错 - 谢谢 Yosuke!
【解决方案2】:

我已向 Apple 提交了一份错误报告(以确定这是否是错误)。

与此同时,我创建了一个可怕的 hack 来解决 S3Request.m 中的问题 方法configureURLRequest:

NSString *checkFormat =[self.date requestFormat];
if(![checkFormat hasSuffix:@":00"])
    checkFormat = [NSString stringWithFormat:@"%@:00",checkFormat];
[self.urlRequest setValue:checkFormat forHTTPHeaderField:kHttpHdrDate];

这可能与您的 AWS 开发工具包版本不同。

我不会长期使用此修复程序 - 一旦 Apple 错误报告团队返回推荐的解决方案,我将在此处发布任何回复

我也在这里发了一个问题:https://forums.aws.amazon.com/thread.jspa?threadID=135829#

编辑:在最新版本的工具包中,黑客是:

NSString *checkFormat =[self.date stringWithRFC822Format];
if(![checkFormat hasSuffix:@":00"])
    checkFormat = [NSString stringWithFormat:@"%@:00",checkFormat];
[self.urlRequest setValue:checkFormat forHTTPHeaderField:kHttpHdrDate];

【讨论】:

    【解决方案3】:

    我终于找到了解决方案,这对我有用,我使用 awsios sdk 1.3.1 而不是 awsios sdk 1.6.1。并在方法中对 S3Request.m 进行一些更改

    -(AmazonURLRequest *)configureURLRequest{
       ....
         ....
    
       NSString *checkFormat =[self.date stringWithRFC822Format];
       if(![checkFormat hasSuffix:@":00"])
        checkFormat = [NSString stringWithFormat:@"%@+00:00",checkFormat];
       [self.urlRequest setValue:checkFormat forHTTPHeaderField:kHttpHdrDate];
       ...
         ...
    

    }

    【讨论】:

      【解决方案4】:

      我也遇到了同样的问题,更新到 AWS SDK 1.6 版后问题就消失了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-10
        • 2016-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-20
        • 2016-03-25
        • 2022-10-05
        相关资源
        最近更新 更多