开发者社区> 问答> 正文

阿里云osssdkforios

做过太多 网站 了,当然,都是传统的。虽然跟 阿里云 结缘有两年多了,但一直都拿ecs做普通 服务器 用,真正所谓的 云服务 ,基本没涉及过,哎,菜鸟啊…… 新的项目用到了 oss ,我相信很多人都会很纳闷,阿里云官方是没有提供oss-for-IOS的 SDK 的。而我本人之前也没有接触过 IOS 开发 ,一时各种蛋疼,各种研究! 总算有些眉目了,由于是IOS 0基础的人,就不卖弄来写什么封装好的sdk了,只提供个思路,各路大侠该骂使劲骂…… MD,废话太多了,入主题: 基于AFN来做的话,只需要做好request的header管理就OK。语文不行,尼玛,不知道怎么写,上代码:
复制代码

  1. #import <CommonCrypto/CommonHMAC.h>
  2. /**
  3. * 获取 authorization 所要求的 HMACSHA1
  4. * @param string 按照OSS文档将HTTPHeaders拼接出来的字符串
  5. * @param key ALIYUN_ACCESS_KEY
  6. * @return NSData
  7. */
  8. static NSData * AFHMACSHA1EncodedDataFromStringWithKey(NSString *string, NSString *key) {
  9.     NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding];
  10.     CCHmacContext context;
  11.     const char *keyCString = [key cStringUsingEncoding:NSASCIIStringEncoding];
  12.     
  13.     CCHmacInit(&context, kCCHmacAlgSHA1, keyCString, strlen(keyCString));
  14.     CCHmacUpdate(&context, [data bytes], [data length]);
  15.     
  16.     unsigned char digestRaw[CC_SHA1_DIGEST_LENGTH];
  17.     NSInteger digestLength = CC_SHA1_DIGEST_LENGTH;
  18.     
  19.     CCHmacFinal(&context, digestRaw);
  20.     
  21.     return [NSData dataWithBytes:digestRaw length:digestLength];
  22. }
  23. //FIXME: You Need to CHANGE the DateFormat for ALIYUN OSS
  24. static NSString * AFRFC822FormatStringFromDate(NSDate *date) {
  25.     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  26.     [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
  27.     [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
  28.     [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  29.     
  30.     return [dateFormatter stringFromDate:date];
  31. }
  32. static NSString * AFBase64EncodedStringFromData(NSData *data) {
  33.     NSUInteger length = [data length];
  34.     NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
  35.     
  36.     uint8_t *input = (uint8_t *)[data bytes];
  37.     uint8_t *output = (uint8_t *)[mutableData mutableBytes];
  38.     
  39.     for (NSUInteger i = 0; i < length; i += 3) {
  40.         NSUInteger value = 0;
  41.         for (NSUInteger j = i; j < (i + 3); j++) {
  42.             value <<= 8;
  43.             if (j < length) {
  44.                 value |= (0xFF & input[j]);
  45.             }
  46.         }
  47.         
  48.         static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  49.         
  50.         NSUInteger idx = (i / 3) * 4;
  51.         output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F];
  52.         output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F];
  53.         output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6)  & 0x3F] : '=';
  54.         output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0)  & 0x3F] : '=';
  55.     }
  56.     
  57.     return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding];
  58. }

复制代码
  1. -(void )authorizationHeadersForRequest:(NSMutableURLRequest * __autoreleasing *)request {
  2.     // Long header values that are subject to "folding" should split into new lines
  3.     // according to Aliyun-OSS's documentation.
  4.     NSAssert(_accessId && _accessKey, @"ALIYUN_ACCESS_ID && ALIYUN_ACCESS_KEY must not be nil");
  5.     
  6.     NSMutableDictionary *headerFields = [NSMutableDictionary dictionary];
  7.     [[*request allHTTPHeaderFields] enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
  8.         key = [key lowercaseString];
  9.         if ([key hasPrefix:@"x-oss-"]) {
  10.             if ([headerFields objectForKey:key]) {
  11.                 value = [[headerFields objectForKey:key] stringByAppendingFormat:@",%@", value];
  12.             }
  13.             [headerFields setObject:value forKey:key];
  14.         }
  15.     }];
  16.     
  17.     NSMutableString *headerString = [NSMutableString string];
  18.     for (NSString *key in [[headerFields allKeys] sortedArrayUsingSelector:@selector(compare:)]) {
  19.         id value = [headerFields objectForKey:key];
  20.         [headerString appendFormat:@"%@:%@\n", key, value];
  21.     }
  22.     
  23.     NSString *canonicalizedResource = [NSString stringWithFormat:@"%@", *request.URL.path];
  24.     NSString *method = [*request HTTPMethod];
  25.     NSString *contentMD5 = [*request valueForHTTPHeaderField:@"Content-MD5"];
  26.     NSString *contentType = [*request valueForHTTPHeaderField:@"Content-Type"];
  27.     NSString *date = AFRFC822FormatStringFromDate([NSDate date]);
  28.     date = (date) ? date : @"";
  29.     [*request setValue:date forHTTPHeaderField:@"Date"];
  30.     
  31.     NSMutableString *mutableString = [NSMutableString string];
  32.     [mutableString appendFormat:@"%@\n", (method) ? method : @""];
  33.     [mutableString appendFormat:@"%@\n", (contentMD5) ? contentMD5 : @""];
  34.     [mutableString appendFormat:@"%@\n", (contentType) ? contentType : @""];
  35.     [mutableString appendFormat:@"%@\n", date];
  36.     [mutableString appendFormat:@"%@", headerString];
  37.     [mutableString appendFormat:@"%@", canonicalizedResource];
  38.     
  39.     NSData *hmac = AFHMACSHA1EncodedDataFromStringWithKey(mutableString, ALIYUN_ACCESS_KEY);
  40.     NSString *signature = AFBase64EncodedStringFromData(hmac);
  41.     NSString *authorization = [NSString stringWithFormat:@"OSS %@:%@", ALIYUN_ACCESS_ID, signature];
  42.     
  43.     [*request setValue:authorization forHTTPHeaderField:@"Authorization"];
  44.     
  45. }

展开
收起
微雨入寒窗 2017-05-09 10:49:03 3195 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
阿里云产品十一月刊来啦! 立即下载
阿里云产品安全基线白皮书 立即下载
云原生产业大会:阿里云精彩内容集锦 立即下载