最近做了七牛云存储的有关内容,涉及到与后台交互获取验证的token,这个token是用作验证使用的,官方建议使用后台生成后,客户端再获取使用。但客户端也能独自生成具体生成方法如下:         

#import <QiniuSDK.h>
#import "QN_GTM_Base64.h"
#import <CommonCrypto/CommonHMAC.h>

#define kQiniuScope @""
#define kQiniuAccessKey @""
#define kQiniuSecretKey @""
#define kQiniuLiveTime 1  //有效时间
-(void)createToken {
    if (!kQiniuScope.length || !kQiniuAccessKey.length || !kQiniuSecretKey.length) {
        return;
    }
    // 将上传策略中的scrop和deadline序列化成json格式
    NSMutableDictionary *authInfo = [NSMutableDictionary dictionary];
    [authInfo setObject:kQiniuScope forKey:@"scope"];
    [authInfo
     setObject:[NSNumber numberWithLong:[[NSDate date] timeIntervalSince1970] + kQiniuLiveTime * 24 * 3600]
     forKey:@"deadline"];
    
    NSData *jsonData =
    [NSJSONSerialization dataWithJSONObject:authInfo options:NSJSONWritingPrettyPrinted error:nil];
    
    // 对json序列化后的上传策略进行URL安全的base64编码
    NSString *encodedString = [self urlSafeBase64Encode:jsonData];
    
    // 用secretKey对编码后的上传策略进行HMAC-SHA1加密,并且做安全的base64编码,得到encoded_signed
    NSString *encodedSignedString = [self HMACSHA1:kQiniuSecretKey text:encodedString];
    
    // 将accessKey、encodedSignedString和encodedString拼接,中间用:分开,就是上传的token
    NSString *token =
    [NSString stringWithFormat:@"%@:%@:%@", kQiniuAccessKey, encodedSignedString, encodedString];
    
    self.qiniuToken = token;
}
  
// 将进行URL安全的base64编码
- (NSString *)urlSafeBase64Encode:(NSData *)text {
    NSString *base64 =
    [[NSString alloc] initWithData:[QN_GTM_Base64 encodeData:text] encoding:NSUTF8StringEncoding];
    base64 = [base64 stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
    base64 = [base64 stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    return base64;
}
  
// 对上传策略中进行HMAC-SHA1加密
- (NSString *)HMACSHA1:(NSString *)key text:(NSString *)text {
    const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
    const char *cData = [text cStringUsingEncoding:NSUTF8StringEncoding];
    
    char cHMAC[CC_SHA1_DIGEST_LENGTH];
    
    CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
    
    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:CC_SHA1_DIGEST_LENGTH];
    NSString *hash = [self urlSafeBase64Encode:HMAC];
    return hash;
}
上面涉及到三个参数

20190819155423181

20190819155423182

这样拿到了token,直接上传图片就可以了
- (void)uploadImageToQNFilePath:(NSString *)filePath {
    QNUploadManager *upManager = [[QNUploadManager alloc] init];
    QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {}
                                                                 params:nil
                                                               checkCrc:NO
                                                     cancellationSignal:nil];
    [upManager putFile:filePath key:nil token:self.qiniuToken complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        [self.uploadImageArray addObject:resp[@"key"]];
        NSLog(@"do something");
    } option:uploadOption];
}