做过太多
网站
了,当然,都是传统的。虽然跟
阿里云
结缘有两年多了,但一直都拿ecs做普通
服务器
用,真正所谓的
云服务
,基本没涉及过,哎,菜鸟啊……
新的项目用到了
oss
,我相信很多人都会很纳闷,阿里云官方是没有提供oss-for-IOS的
SDK
的。而我本人之前也没有接触过
IOS
开发
,一时各种蛋疼,各种研究!
总算有些眉目了,由于是IOS 0基础的人,就不卖弄来写什么封装好的sdk了,只提供个思路,各路大侠该骂使劲骂……
MD,废话太多了,入主题:
基于AFN来做的话,只需要做好request的header管理就OK。语文不行,尼玛,不知道怎么写,上代码:
复制代码
- #import <CommonCrypto/CommonHMAC.h>
- /**
- * 获取 authorization 所要求的 HMACSHA1
- * @param string 按照OSS文档将HTTPHeaders拼接出来的字符串
- * @param key ALIYUN_ACCESS_KEY
- * @return NSData
- */
- static NSData * AFHMACSHA1EncodedDataFromStringWithKey(NSString *string, NSString *key) {
- NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding];
- CCHmacContext context;
- const char *keyCString = [key cStringUsingEncoding:NSASCIIStringEncoding];
-
- CCHmacInit(&context, kCCHmacAlgSHA1, keyCString, strlen(keyCString));
- CCHmacUpdate(&context, [data bytes], [data length]);
-
- unsigned char digestRaw[CC_SHA1_DIGEST_LENGTH];
- NSInteger digestLength = CC_SHA1_DIGEST_LENGTH;
-
- CCHmacFinal(&context, digestRaw);
-
- return [NSData dataWithBytes:digestRaw length:digestLength];
- }
- //FIXME: You Need to CHANGE the DateFormat for ALIYUN OSS
- static NSString * AFRFC822FormatStringFromDate(NSDate *date) {
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
- [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
- [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
-
- return [dateFormatter stringFromDate:date];
- }
- static NSString * AFBase64EncodedStringFromData(NSData *data) {
- NSUInteger length = [data length];
- NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
-
- uint8_t *input = (uint8_t *)[data bytes];
- uint8_t *output = (uint8_t *)[mutableData mutableBytes];
-
- for (NSUInteger i = 0; i < length; i += 3) {
- NSUInteger value = 0;
- for (NSUInteger j = i; j < (i + 3); j++) {
- value <<= 8;
- if (j < length) {
- value |= (0xFF & input[j]);
- }
- }
-
- static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
- NSUInteger idx = (i / 3) * 4;
- output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F];
- output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F];
- output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6) & 0x3F] : '=';
- output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0) & 0x3F] : '=';
- }
-
- return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding];
- }
复制代码
- -(void )authorizationHeadersForRequest:(NSMutableURLRequest * __autoreleasing *)request {
- // Long header values that are subject to "folding" should split into new lines
- // according to Aliyun-OSS's documentation.
- NSAssert(_accessId && _accessKey, @"ALIYUN_ACCESS_ID && ALIYUN_ACCESS_KEY must not be nil");
-
- NSMutableDictionary *headerFields = [NSMutableDictionary dictionary];
- [[*request allHTTPHeaderFields] enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
- key = [key lowercaseString];
- if ([key hasPrefix:@"x-oss-"]) {
- if ([headerFields objectForKey:key]) {
- value = [[headerFields objectForKey:key] stringByAppendingFormat:@",%@", value];
- }
- [headerFields setObject:value forKey:key];
- }
- }];
-
- NSMutableString *headerString = [NSMutableString string];
- for (NSString *key in [[headerFields allKeys] sortedArrayUsingSelector:@selector(compare:)]) {
- id value = [headerFields objectForKey:key];
- [headerString appendFormat:@"%@:%@\n", key, value];
- }
-
- NSString *canonicalizedResource = [NSString stringWithFormat:@"%@", *request.URL.path];
- NSString *method = [*request HTTPMethod];
- NSString *contentMD5 = [*request valueForHTTPHeaderField:@"Content-MD5"];
- NSString *contentType = [*request valueForHTTPHeaderField:@"Content-Type"];
- NSString *date = AFRFC822FormatStringFromDate([NSDate date]);
- date = (date) ? date : @"";
- [*request setValue:date forHTTPHeaderField:@"Date"];
-
- NSMutableString *mutableString = [NSMutableString string];
- [mutableString appendFormat:@"%@\n", (method) ? method : @""];
- [mutableString appendFormat:@"%@\n", (contentMD5) ? contentMD5 : @""];
- [mutableString appendFormat:@"%@\n", (contentType) ? contentType : @""];
- [mutableString appendFormat:@"%@\n", date];
- [mutableString appendFormat:@"%@", headerString];
- [mutableString appendFormat:@"%@", canonicalizedResource];
-
- NSData *hmac = AFHMACSHA1EncodedDataFromStringWithKey(mutableString, ALIYUN_ACCESS_KEY);
- NSString *signature = AFBase64EncodedStringFromData(hmac);
- NSString *authorization = [NSString stringWithFormat:@"OSS %@:%@", ALIYUN_ACCESS_ID, signature];
-
- [*request setValue:authorization forHTTPHeaderField:@"Authorization"];
-
- }