之前写过一篇封装七牛上传SDK的文章,那我们今天就说说阿里的上传SDK
因为公司的业务是短视频方面的,相机拍摄用的是美摄,拍好后资源上传至阿里。属于视频点播
点播视频上传
阿里推荐使用上传地址和凭证上传,为了安全性考虑,我们就使用凭证和地址进行上传吧。不使用STS
首先我们先看下流程
首先我们要把我们要上传的资源的路径传给我们自己的server,我们自己server 将从阿里那里拿到 上传凭证和地址,这时会返回videoId,使用上传地址和凭证上传,上传成功后是不会返回videoId的,需要我们自己记住。这点很重要。
#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);
};
// 进度
OnUploadProgressListener ProgressCallbackFunc = ^(UploadFileInfo* fileInfo, long uploadedSize, long totalSize) {
if (weakSelf.uploadProgressBlock) {
weakSelf.uploadProgressBlock(uploadedSize/totalSize);
}
};
// token过期
OnUploadTokenExpiredListener TokenExpiredCallbackFunc = ^{
NSLog(@"upload token expired callback.");
[weakSelf getNewUploadAuth];
// token过期,设置新的STS,继续上传
// [weakSelf.uploader resumeWithToken:`STS Key Id` accessKeySecret:`STS Key Secret` secretToken:`STS Secret Token` expireTime:`STS Expire Time`];
};
// 上传开始重试回调
OnUploadRertyListener RetryCallbackFunc = ^{
NSLog(@"upload retry begin callback.");
};
//上传结束重试,继续上传回调
OnUploadRertyResumeListener RetryResumeCallbackFunc = ^{
NSLog(@"upload retry end callback.");
};
// 开始上传回调
OnUploadStartedListener UploadStartedCallbackFunc = ^(UploadFileInfo* fileInfo) {
NSLog(@"upload upload started callback.");
// 设置上传地址和凭证
[weakClient setUploadAuthAndAddress:fileInfo uploadAuth:weakSelf.uploadAuth uploadAddress:weakSelf.uploadAddress];
};
// 回调的监听
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 = @"上传测试";
vodInfo.desc = @"这是上传测试";
// 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