上传图片之前说过,但是今天在上传录音时遇到问题。还是用putData 的方式 发现传上去并不能播放。
需要将key加后缀.mp3即可
上传时调用
NSString *key = [NSString stringWithFormat:@"%@.mp3",self.qiniuImageName];
NSData *urlData = [NSData dataWithContentsOfURL:urlPlay];
[self.upManager putData:urlData key:key token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
NSLog(@"七牛错误信息%@", info.error);
NSLog(@"七牛返回信息%@", resp);
if ([resp objectForKey:@"key"] != nil ||![[resp objectForKey:@"key"] isEqualToString:@""]) {
}
} option:nil];
这样就好了
附上客户端直传方式代码
需要导入
- <CommonCrypto/CommonCrypto.h>
- "QN_GTM_Base64.h"
- (NSString *)makeToken:(NSString *)accessKey secretKey:(NSString *)secretKey
{
const char *secretKeyStr = [secretKey UTF8String];
NSString *policy = [self 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];
self.token = [NSString stringWithFormat:@"%@:%@:%@", accessKey, encodedDigest, encodedPolicy];
return self.token;//得到了token
}
- (NSString *)marshal
{
time_t deadline;
time(&deadline);//返回当前系统时间
deadline += (self.expires > 0) ? self.expires : 3600; // +3600秒,即默认token保存1小时.
NSNumber *deadlineNumber = [NSNumber numberWithLongLong:deadline];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:self.qiniuFilekey forKey:@"scope"];
[dic setObject:deadlineNumber forKey:@"deadline"];
NSString *json = [dic mj_JSONString];
return json;
}
//生成uuid
- (NSString *)getUniqueStrByUUID
{
CFUUIDRef uuidObj = CFUUIDCreate(nil);//create a new UUID
//get the string representation of the UUID
NSString *str = (__bridge_transfer NSString *)CFUUIDCreateString(nil, uuidObj);
CFRelease(uuidObj);
return str ;
}
viewdidload调用
self.qiniuImageName = [self getUniqueStrByUUID];
self.qiniuFilekey = QINIUFOLDER;
self.upManager = [[QNUploadManager alloc]init];
[self makeToken:QINIUAk secretKey:QINIUSK];