之前寫(xiě)過(guò)一篇封裝七牛上傳SDK的文章,那我們今天就說(shuō)說(shuō)阿里的上傳SDK
因?yàn)楣镜臉I(yè)務(wù)是短視頻方面的锭魔,相機(jī)拍攝用的是美攝例证,拍好后資源上傳至阿里。屬于視頻點(diǎn)播
點(diǎn)播視頻上傳
阿里推薦使用上傳地址和憑證上傳迷捧,為了安全性考慮战虏,我們就使用憑證和地址進(jìn)行上傳吧。不使用STS
首先我們先看下流程
首先我們要把我們要上傳的資源的路徑傳給我們自己的server党涕,我們自己server 將從阿里那里拿到 上傳憑證和地址,這時(shí)會(huì)返回videoId,使用上傳地址和憑證上傳巡社,上傳成功后是不會(huì)返回videoId的膛堤,需要我們自己記住。這點(diǎn)很重要晌该。
#import <VODUpload/VODUploadClient.h>
NS_ASSUME_NONNULL_BEGIN
@interface FFAliyunUploadManager : NSObject
@property (nonatomic , copy) void (^uploadProgressBlock)(CGFloat progress);
@property (nonatomic , copy) void (^uploadFinishBlock)(NSString *videoId);
@property (nonatomic , copy) void (^upLoadFailBlock)(void);
@property (nonatomic , copy) NSString *videoId;
@property (nonatomic , copy) NSString *uploadAuth;
@property (nonatomic , copy) NSString *uploadAddress;
+(FFAliyunUploadManager *)shareManager;
- (void)uploadVideoWithFilePath:(NSString *)filePath andChallengeType:(FFUserChallengeModel *)challengeModel andDesc:(NSString *)desc;
@end
@interface FFAliyunUploadManager ()
@property (nonatomic , strong) VODUploadClient *uploader;
@end
@implementation FFAliyunUploadManager
+(FFAliyunUploadManager *)shareManager{
static FFAliyunUploadManager *sharedGlobalInstance = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
sharedGlobalInstance = [[self alloc] init];
});
return sharedGlobalInstance;
}
- (instancetype)init {
self = [super init];
if (self) {
[self initWithConfig];
}
return self;
}
- (void)initWithConfig {
_uploader = [VODUploadClient new];
__weak VODUploadClient *weakClient = _uploader;
__weak typeof(self) weakSelf = self;
// 完成
OnUploadFinishedListener FinishCallbackFunc = ^(UploadFileInfo* fileInfo, VodUploadResult* result){
NSLog(@"upload finished callback videoid:%@, imageurl:%@", result.videoId, result.imageUrl);
if (weakSelf.uploadFinishBlock) {
weakSelf.uploadFinishBlock(weakSelf.videoId);
}
};
// 失敗
OnUploadFailedListener FailedCallbackFunc = ^(UploadFileInfo* fileInfo, NSString *code, NSString* message){
NSLog(@"upload failed callback code = %@, error message = %@", code, message);
};
// 進(jìn)度
OnUploadProgressListener ProgressCallbackFunc = ^(UploadFileInfo* fileInfo, long uploadedSize, long totalSize) {
if (weakSelf.uploadProgressBlock) {
weakSelf.uploadProgressBlock(uploadedSize/totalSize);
}
};
// token過(guò)期
OnUploadTokenExpiredListener TokenExpiredCallbackFunc = ^{
NSLog(@"upload token expired callback.");
[weakSelf getNewUploadAuth];
// token過(guò)期肥荔,設(shè)置新的STS,繼續(xù)上傳
// [weakSelf.uploader resumeWithToken:`STS Key Id` accessKeySecret:`STS Key Secret` secretToken:`STS Secret Token` expireTime:`STS Expire Time`];
};
// 上傳開(kāi)始重試回調(diào)
OnUploadRertyListener RetryCallbackFunc = ^{
NSLog(@"upload retry begin callback.");
};
//上傳結(jié)束重試朝群,繼續(xù)上傳回調(diào)
OnUploadRertyResumeListener RetryResumeCallbackFunc = ^{
NSLog(@"upload retry end callback.");
};
// 開(kāi)始上傳回調(diào)
OnUploadStartedListener UploadStartedCallbackFunc = ^(UploadFileInfo* fileInfo) {
NSLog(@"upload upload started callback.");
// 設(shè)置上傳地址和憑證
[weakClient setUploadAuthAndAddress:fileInfo uploadAuth:weakSelf.uploadAuth uploadAddress:weakSelf.uploadAddress];
};
// 回調(diào)的監(jiān)聽(tīng)
VODUploadListener *listener = [[VODUploadListener alloc] init];
listener.finish = FinishCallbackFunc;
listener.failure = FailedCallbackFunc;
listener.progress = ProgressCallbackFunc;
listener.expire = TokenExpiredCallbackFunc;
listener.retry = RetryCallbackFunc;
listener.retryResume = RetryResumeCallbackFunc;
listener.started = UploadStartedCallbackFunc;
// init with upload address and upload auth
[self.uploader init:listener];
}
#pragma mark - 獲取最新的上傳憑證
- (void)getNewUploadAuth {
[self.uploader resumeWithAuth:self.uploadAuth];
}
- (void)uploadVideoWithFilePath:(NSString *)filePath andChallengeType:(FFUserChallengeModel *)challengeModel andDesc:(NSString *)desc {
VodInfo *vodInfo = [[VodInfo alloc] init];
vodInfo.title = @"上傳測(cè)試";
vodInfo.desc = @"這是上傳測(cè)試";
// vodInfo.cateId = [NSNumber numberWithInteger:challengeModel.itemId.integerValue];
vodInfo.cateId = @(2);
vodInfo.tags = @"";
[self.uploader addFile:filePath vodInfo:vodInfo];
[self startUpload];
}
- (void)startUpload {
[self.uploader start];
}
- (void)stopUpload {
[self.uploader stop];
}
@end