Commit 3f3c0dae84a93b7d616c88fa54585e0d8d5a2e36
1 parent
8b227b79
更新方法
Showing
2 changed files
with
63 additions
and
50 deletions
CNLiveUploadYunSDKDemo/CNLiveUploadYunSDKDemo/AppDelegate.h
| ... | ... | @@ -8,8 +8,8 @@ |
| 8 | 8 | |
| 9 | 9 | #import <UIKit/UIKit.h> |
| 10 | 10 | |
| 11 | -#define AppId @"60_j5xilfyl12" | |
| 12 | -#define AppKey @"8b58d3dcdd828359e6113d5531029801e5d2b53e9a980c" | |
| 11 | +#define AppId @"60_jaxisljz90" | |
| 12 | +#define AppKey @"1ee33b54e18a66b94eca7f8149408565b02f749d314926" | |
| 13 | 13 | |
| 14 | 14 | @interface AppDelegate : UIResponder <UIApplicationDelegate> |
| 15 | 15 | ... | ... |
CNLiveUploadYunSDKDemo/CNLiveUploadYunSDKDemo/CNLiveUploadController.m
| ... | ... | @@ -16,13 +16,14 @@ |
| 16 | 16 | #define FileBlockSize 5*1024*1024 //一块大小,分块最小5M |
| 17 | 17 | |
| 18 | 18 | //上传 |
| 19 | -#define kUploadBucketKey @"7.6M.mov" //上传时用到的bucket里文件的路径,此为在wz目录下7.6M.mov | |
| 20 | -#define kUploadBucketName @"compose" //上传所用的bucketName | |
| 21 | 19 | #define keyUploadPartNum @"partNum" //需要app本地存储已经传成功的块号,demo为了演示,用NSUserDefaults存储,app可用数据库等 |
| 22 | 20 | #define keyUploadId @"uploadId" //需要app本地存储已经初始化成功的uploadId,用于断点续传,demo为了演示,用NSUserDefaults存储,app可用数据库等 |
| 23 | 21 | |
| 24 | 22 | @interface CNLiveUploadController ()<UITableViewDelegate,UITableViewDataSource,KingSoftServiceRequestDelegate> |
| 25 | - | |
| 23 | +{ | |
| 24 | + NSString *_bucketName;//SP对应的bucketName | |
| 25 | + NSString *_bucketKey;//SP上传时用到的bucket里文件的路径(包含扩展名) | |
| 26 | +} | |
| 26 | 27 | @property (nonatomic ,strong) UITableView * tableView; |
| 27 | 28 | |
| 28 | 29 | @property (assign, nonatomic) long long fileSize; |
| ... | ... | @@ -33,7 +34,6 @@ |
| 33 | 34 | @property (strong, nonatomic) NSFileHandle *fileHandle; |
| 34 | 35 | @property (nonatomic, strong) NSArray *arrItems; |
| 35 | 36 | @property (strong, nonatomic) CNLiveMultipartUpload *muilt; |
| 36 | - | |
| 37 | 37 | @end |
| 38 | 38 | |
| 39 | 39 | @implementation CNLiveUploadController |
| ... | ... | @@ -50,6 +50,17 @@ |
| 50 | 50 | [self.view addSubview:self.tableView]; |
| 51 | 51 | self.tableView.delegate = self; |
| 52 | 52 | self.tableView.dataSource = self; |
| 53 | + | |
| 54 | + _bucketName = [CNLiveUpload getSPBucketName]; | |
| 55 | + NSLog(@"bucketName:%@",_bucketName); | |
| 56 | + [self getBucketKeyFromServer]; | |
| 57 | +} | |
| 58 | + | |
| 59 | +- (void)getBucketKeyFromServer | |
| 60 | +{ | |
| 61 | +#warning 此处建议从server获取 上传文件对应的bucketKey(文件路径包含扩展名) | |
| 62 | +#warning Demo为了演示 bucketKey用固定值 | |
| 63 | + _bucketKey = @"cnlive12/test123-7.6M.mov"; | |
| 53 | 64 | } |
| 54 | 65 | |
| 55 | 66 | #pragma mark 相册方法 |
| ... | ... | @@ -124,21 +135,21 @@ |
| 124 | 135 | */ |
| 125 | 136 | - (void)beginMultipartUpload |
| 126 | 137 | { |
| 127 | - NSString *strKey = kUploadBucketKey; //key 为在bucket下的路径,demo中为根目录下7.6M.mov路径 | |
| 138 | + NSString *strKey = _bucketKey; | |
| 128 | 139 | _partSize = 5; // 文件大于5M为最小5M一块 |
| 129 | - | |
| 140 | + | |
| 130 | 141 | CNLiveAccessControlList *acl = [[CNLiveAccessControlList alloc] init]; |
| 131 | 142 | [acl setContronAccess:CNLiveYun_Permission_Public_Read]; |
| 132 | - | |
| 133 | - CNLiveInitiateMultipartUploadRequest *initMultipartUploadReq = [[CNLiveInitiateMultipartUploadRequest alloc] initWithKey:strKey inBucket:kUploadBucketName acl:acl grantAcl:nil]; | |
| 143 | + | |
| 144 | + CNLiveInitiateMultipartUploadRequest *initMultipartUploadReq = [[CNLiveInitiateMultipartUploadRequest alloc] initWithKey:strKey inBucket:_bucketName acl:acl grantAcl:nil]; | |
| 134 | 145 | [initMultipartUploadReq setCompleteRequest]; |
| 135 | - | |
| 146 | + | |
| 136 | 147 | NSArray *array = [initMultipartUploadReq.CNYHeader componentsSeparatedByString:@"\n"]; |
| 137 | 148 | NSString *headers = [array firstObject]; |
| 138 | - | |
| 139 | - [CNLiveUpload getTokenWithMd5:initMultipartUploadReq.contentMd5 verb:initMultipartUploadReq.httpMethod type:initMultipartUploadReq.contentType res:initMultipartUploadReq.CNYResource headers:headers date:initMultipartUploadReq.strDate success:^(NSString *token) { | |
| 149 | + | |
| 150 | + [CNLiveUpload getTokenWithMd5:initMultipartUploadReq.contentMd5 verb:initMultipartUploadReq.httpMethod type:initMultipartUploadReq.contentType res:initMultipartUploadReq.CNYResource headers:headers date:initMultipartUploadReq.strDate success:^(NSString *token ,NSString *cnliveDate) { | |
| 140 | 151 | [initMultipartUploadReq setStrCNLiveToken:token]; |
| 141 | - | |
| 152 | + [initMultipartUploadReq setStrDate:cnliveDate]; | |
| 142 | 153 | _muilt = [[CNLiveClient initialize] initiateMultipartUploadWithRequest:initMultipartUploadReq]; |
| 143 | 154 | |
| 144 | 155 | if (_muilt == nil) { |
| ... | ... | @@ -196,12 +207,9 @@ |
| 196 | 207 | //list一下所有上传过的块 |
| 197 | 208 | CNLiveListPartsRequest *req2 = [[CNLiveListPartsRequest alloc] initWithMultipartUpload:_muilt]; |
| 198 | 209 | [req2 setCompleteRequest]; |
| 199 | - | |
| 200 | - //使用token签名时从Appserver获取token后设置token,使用Ak sk则忽略,不需要调用 | |
| 201 | - | |
| 202 | - [CNLiveUpload getTokenWithMd5:req2.contentMd5 verb:req2.httpMethod type:req2.contentType res:req2.CNYResource headers:headers date:req2.strDate success:^(NSString *token) { | |
| 210 | + [CNLiveUpload getTokenWithMd5:req2.contentMd5 verb:req2.httpMethod type:req2.contentType res:req2.CNYResource headers:headers date:req2.strDate success:^(NSString *token ,NSString *cnliveDate) { | |
| 203 | 211 | [req2 setStrCNLiveToken:token]; |
| 204 | - | |
| 212 | + [req2 setStrDate:cnliveDate]; | |
| 205 | 213 | CNLiveListPartsResponse *response2 = [[CNLiveClient initialize] listParts:req2]; |
| 206 | 214 | |
| 207 | 215 | NSLog(@"response.listResult.parts.count =%lu",(unsigned long)[response2.listResult.parts count]); |
| ... | ... | @@ -219,8 +227,6 @@ |
| 219 | 227 | |
| 220 | 228 | }]; |
| 221 | 229 | } |
| 222 | - //initMultipartUploadReq.strDate = @"Fri, 15 Jul 2016 09:21:30 GMT"; | |
| 223 | - //initMultipartUploadReq.strKS3Token = @"KSS JYWSSnN5qY/hFiWg/Y1V:s1ICTedN7C+QqGFMD1FQYj6/bYA="; | |
| 224 | 230 | } failure:^(NSDictionary *error) { |
| 225 | 231 | |
| 226 | 232 | }]; |
| ... | ... | @@ -249,20 +255,19 @@ |
| 249 | 255 | [_fileHandle seekToFileOffset:_partLength*(_uploadNum)]; |
| 250 | 256 | } |
| 251 | 257 | } |
| 252 | - | |
| 258 | + | |
| 253 | 259 | CNLiveUploadPartRequest *req = [[CNLiveUploadPartRequest alloc] initWithMultipartUpload:_muilt partNumber:(int32_t)partNumber data:data generateMD5:NO]; |
| 254 | 260 | req.delegate = self; |
| 255 | 261 | req.contentLength = data.length; |
| 256 | 262 | req.contentMd5 = [CNLiveSDKUtil base64md5FromData:data]; |
| 257 | 263 | [req setCompleteRequest]; |
| 258 | - | |
| 264 | + | |
| 259 | 265 | NSArray *array = [req.CNYHeader componentsSeparatedByString:@"\n"]; |
| 260 | 266 | NSString *headers = [array firstObject]; |
| 261 | 267 | |
| 262 | - [CNLiveUpload getTokenWithMd5:req.contentMd5 verb:req.httpMethod type:req.contentType res:req.CNYResource headers:headers date:req.strDate success:^(NSString *token) { | |
| 263 | - //使用token签名时从Appserver获取token后设置token,使用Ak sk则忽略,不需要调用 | |
| 268 | + [CNLiveUpload getTokenWithMd5:req.contentMd5 verb:req.httpMethod type:req.contentType res:req.CNYResource headers:headers date:req.strDate success:^(NSString *token ,NSString *cnliveDate) { | |
| 264 | 269 | [req setStrCNLiveToken:token]; |
| 265 | - | |
| 270 | + [req setStrDate:cnliveDate]; | |
| 266 | 271 | [[CNLiveClient initialize] uploadPart:req]; |
| 267 | 272 | } failure:^(NSDictionary *error) { |
| 268 | 273 | |
| ... | ... | @@ -276,16 +281,16 @@ |
| 276 | 281 | NSLog(@"请先创建上传,再调用Abort"); |
| 277 | 282 | return; |
| 278 | 283 | } |
| 279 | - | |
| 284 | + | |
| 280 | 285 | CNLiveAbortMultipartUploadRequest *request = [[CNLiveAbortMultipartUploadRequest alloc] initWithMultipartUpload:_muilt]; |
| 281 | 286 | [request setCompleteRequest]; |
| 282 | - | |
| 287 | + | |
| 283 | 288 | NSArray *array = [request.CNYHeader componentsSeparatedByString:@"\n"]; |
| 284 | 289 | NSString *headers = [array firstObject]; |
| 285 | 290 | |
| 286 | - [CNLiveUpload getTokenWithMd5:request.contentMd5 verb:request.httpMethod type:request.contentType res:request.CNYResource headers:headers date:request.strDate success:^(NSString *token) { | |
| 291 | + [CNLiveUpload getTokenWithMd5:request.contentMd5 verb:request.httpMethod type:request.contentType res:request.CNYResource headers:headers date:request.strDate success:^(NSString *token ,NSString *cnliveDate) { | |
| 287 | 292 | [request setStrCNLiveToken:token]; |
| 288 | - | |
| 293 | + [request setStrDate:cnliveDate]; | |
| 289 | 294 | CNLiveAbortMultipartUploadResponse *response = [[CNLiveClient initialize] abortMultipartUpload:request]; |
| 290 | 295 | if (response.httpStatusCode == 204) { |
| 291 | 296 | NSLog(@"Abort multipart upload success!"); |
| ... | ... | @@ -310,24 +315,22 @@ |
| 310 | 315 | { |
| 311 | 316 | CNLiveAccessControlList *ControlList = [[CNLiveAccessControlList alloc] init]; |
| 312 | 317 | [ControlList setContronAccess:CNLiveYun_Permission_Public_Read_Write]; |
| 313 | - CNLivePutObjectRequest *putObjRequest = [[CNLivePutObjectRequest alloc] initWithName:kUploadBucketName withAcl:ControlList grantAcl:nil]; | |
| 314 | - | |
| 318 | + CNLivePutObjectRequest *putObjRequest = [[CNLivePutObjectRequest alloc] initWithName:_bucketName withAcl:ControlList grantAcl:nil]; | |
| 315 | 319 | NSString *fileName = [[NSBundle mainBundle] pathForResource:@"7.6M" ofType:@"mov"]; |
| 316 | 320 | putObjRequest.data = [NSData dataWithContentsOfFile:fileName options:NSDataReadingMappedIfSafe error:nil]; |
| 317 | - | |
| 318 | 321 | _fileSize = putObjRequest.data.length; |
| 319 | - | |
| 322 | + | |
| 320 | 323 | putObjRequest.delegate = self; |
| 321 | - putObjRequest.filename = kUploadBucketKey; | |
| 324 | + putObjRequest.filename = _bucketKey; | |
| 322 | 325 | putObjRequest.contentMd5 = [CNLiveSDKUtil base64md5FromData:putObjRequest.data]; |
| 323 | 326 | [putObjRequest setCompleteRequest]; |
| 324 | - | |
| 327 | + | |
| 325 | 328 | NSArray *array = [putObjRequest.CNYHeader componentsSeparatedByString:@"\n"]; |
| 326 | 329 | NSString *headers = [array firstObject]; |
| 327 | 330 | |
| 328 | - [CNLiveUpload getTokenWithMd5:putObjRequest.contentMd5 verb:putObjRequest.httpMethod type:putObjRequest.contentType res:putObjRequest.CNYResource headers:headers date:putObjRequest.strDate success:^(NSString *token) { | |
| 331 | + [CNLiveUpload getTokenWithMd5:putObjRequest.contentMd5 verb:putObjRequest.httpMethod type:putObjRequest.contentType res:putObjRequest.CNYResource headers:headers date:putObjRequest.strDate success:^(NSString *token ,NSString *cnliveDate) { | |
| 329 | 332 | [putObjRequest setStrCNLiveToken:token]; |
| 330 | - | |
| 333 | + [putObjRequest setStrDate:cnliveDate]; | |
| 331 | 334 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ |
| 332 | 335 | CNLivePutObjectResponse *response = [[CNLiveClient initialize] putObject:putObjRequest]; |
| 333 | 336 | |
| ... | ... | @@ -434,6 +437,11 @@ |
| 434 | 437 | if ([request isKindOfClass:[KS3PutObjectRequest class]]) { |
| 435 | 438 | if (response.httpStatusCode == 200) { |
| 436 | 439 | NSLog(@"单块上传成功"); |
| 440 | + [CNLiveUpload getUploadResourceUrlWithBucketKey:_bucketKey uploadType:CNLiveYun_UploadType_VideoFile success:^(NSString *resUrl) { | |
| 441 | + NSLog(@"succ:%@",resUrl); | |
| 442 | + } failure:^(NSDictionary *error) { | |
| 443 | + NSLog(@"error%@",error); | |
| 444 | + }]; | |
| 437 | 445 | }else |
| 438 | 446 | { |
| 439 | 447 | NSLog(@"单块上传失败"); |
| ... | ... | @@ -453,9 +461,9 @@ |
| 453 | 461 | NSArray *array = [req2.CNYHeader componentsSeparatedByString:@"\n"]; |
| 454 | 462 | NSString *headers = [array firstObject]; |
| 455 | 463 | |
| 456 | - [CNLiveUpload getTokenWithMd5:req2.contentMd5 verb:req2.httpMethod type:req2.contentType res:req2.CNYResource headers:headers date:req2.strDate success:^(NSString *token) { | |
| 464 | + [CNLiveUpload getTokenWithMd5:req2.contentMd5 verb:req2.httpMethod type:req2.contentType res:req2.CNYResource headers:headers date:req2.strDate success:^(NSString *token ,NSString *cnliveDate) { | |
| 457 | 465 | [req2 setStrCNLiveToken:token]; |
| 458 | - | |
| 466 | + [req2 setStrDate:cnliveDate]; | |
| 459 | 467 | CNLiveListPartsResponse *response2 = [[CNLiveClient initialize] listParts:req2]; |
| 460 | 468 | CNLiveCompleteMultipartUploadRequest *req = [[CNLiveCompleteMultipartUploadRequest alloc] initWithMultipartUpload:_muilt]; |
| 461 | 469 | NSLog(@"%@",response2.listResult.parts); |
| ... | ... | @@ -468,9 +476,9 @@ |
| 468 | 476 | NSArray *array1 = [req.CNYHeader componentsSeparatedByString:@"\n"]; |
| 469 | 477 | NSString *headers1 = [array1 firstObject]; |
| 470 | 478 | |
| 471 | - [CNLiveUpload getTokenWithMd5:req.contentMd5 verb:req.httpMethod type:req.contentType res:req.CNYResource headers:headers1 date:req.strDate success:^(NSString *token) { | |
| 479 | + [CNLiveUpload getTokenWithMd5:req.contentMd5 verb:req.httpMethod type:req.contentType res:req.CNYResource headers:headers1 date:req.strDate success:^(NSString *token ,NSString *cnliveDate) { | |
| 472 | 480 | [req setStrCNLiveToken:token]; |
| 473 | - | |
| 481 | + [req setStrDate:cnliveDate]; | |
| 474 | 482 | CNLiveCompleteMultipartUploadResponse *resp = [[CNLiveClient initialize] completeMultipartUpload:req]; |
| 475 | 483 | NSString *bodyStr = [[NSString alloc]initWithData:resp.body encoding:NSUTF8StringEncoding]; |
| 476 | 484 | if (resp.httpStatusCode != 200) { |
| ... | ... | @@ -482,6 +490,11 @@ |
| 482 | 490 | [mUserDefaults setInteger:0 forKey:keyUploadPartNum]; |
| 483 | 491 | _uploadNum = 0 ; |
| 484 | 492 | [mUserDefaults synchronize]; |
| 493 | + [CNLiveUpload getUploadResourceUrlWithBucketKey:_bucketKey uploadType:CNLiveYun_UploadType_VideoFile success:^(NSString *resUrl) { | |
| 494 | + NSLog(@"succ: %@",resUrl); | |
| 495 | + } failure:^(NSDictionary *error) { | |
| 496 | + NSLog(@"error: %@",error); | |
| 497 | + }]; | |
| 485 | 498 | } |
| 486 | 499 | } failure:^(NSDictionary *error) { |
| 487 | 500 | |
| ... | ... | @@ -560,14 +573,14 @@ |
| 560 | 573 | } |
| 561 | 574 | |
| 562 | 575 | /* |
| 563 | -#pragma mark - Navigation | |
| 564 | - | |
| 565 | -// In a storyboard-based application, you will often want to do a little preparation before navigation | |
| 566 | -- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { | |
| 567 | - // Get the new view controller using [segue destinationViewController]. | |
| 568 | - // Pass the selected object to the new view controller. | |
| 569 | -} | |
| 570 | -*/ | |
| 576 | + #pragma mark - Navigation | |
| 577 | + | |
| 578 | + // In a storyboard-based application, you will often want to do a little preparation before navigation | |
| 579 | + - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { | |
| 580 | + // Get the new view controller using [segue destinationViewController]. | |
| 581 | + // Pass the selected object to the new view controller. | |
| 582 | + } | |
| 583 | + */ | |
| 571 | 584 | |
| 572 | 585 | @end |
| 573 | 586 | ... | ... |