MFQiNiuUtil.m 方法:
+ (NSString *)makeToken:(NSString *)accessKey secretKey:(NSString *)secretKey{
const char *secretKeyStr = [secretKey UTF8String];
NSString *policy = [MFQiNiuUtil marshal];
NSData *policyData = [policy dataUsingEncoding:NSUTF8StringEncoding];
NSString *encodedPolicy = [QN_GTM_Base64 stringByWebSafeEncodingData:policyData padded:TRUE];
const char *encodedPolicyStr = [encodedPolicy cStringUsingEncoding:NSUTF8StringEncoding];
char digestStr[CC_SHA1_DIGEST_LENGTH];
bzero(digestStr, 0);
CCHmac(kCCHmacAlgSHA1, secretKeyStr, strlen(secretKeyStr), encodedPolicyStr, strlen(encodedPolicyStr), digestStr);
NSString *encodedDigest = [QN_GTM_Base64 stringByWebSafeEncodingBytes:digestStr length:CC_SHA1_DIGEST_LENGTH padded:TRUE];
NSString *token = [NSString stringWithFormat:@"%@:%@:%@", accessKey, encodedDigest, encodedPolicy];
return token;//得到了token
}
+ (NSString *)marshal{
NSInteger _expire = 0;
time_t deadline;
time(&deadline);//返回当前系统时间
//@property (nonatomic , assign) int expires; 怎么定义随你...
deadline += (_expire > 0) ? _expire : 3600; // +3600秒,即默认token保存1小时.
NSNumber *deadlineNumber = [NSNumber numberWithLongLong:deadline];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
// 修改成七牛存储空间的名字
[dic setObject:kQiNiuBucketName forKey:@"scope"];//根据
[dic setObject:deadlineNumber forKey:@"deadline"];
NSString *json = [MFQiNiuUtil convertToJsonData:dic];
return json;
}
+(NSString *)convertToJsonData:(NSMutableDictionary *)dict{
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString;
if (!jsonData) {
NSLog(@"%@",error);
}else{
jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
}
NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
NSRange range = {0,jsonString.length};
[mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
NSRange range2 = {0,mutStr.length};
[mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
return mutStr;
}
上传示例:
NSString *token = [MFQiNiuUtil makeToken:kQiNiuAccessKey secretKey:kQiNiuSecretKey];
QNUploadManager *upManager = [[QNUploadManager alloc] init];
NSData *data = [feedbackInfo dataUsingEncoding : NSUTF8StringEncoding];
[upManager putData:data key:[NSString stringWithFormat:@"%@.txt",feedbackStr] token:token
complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
NSLog(@"%@", info);
NSLog(@"%@", resp);
} option:nil];