通过阅读本文,您可以了解使用上传地址和凭证进行上传的基本流程。

简介

上传地址和凭证是视频点播服务下发的上传媒体文件到视频点播存储的文件地址和授权凭证。相对于STS方式,使用上传地址和凭证上传媒体文件拥有很多优势。更多信息,请参见凭证方式与STS方式对比

基本流程

  1. 建议使用RAM子账号访问视频点播服务,务必要授予视频点播权限(AliyunVODFullAccess),更多信息,请参见创建RAM用户并授权
  2. 在应用服务器上部署授权服务,获取上传地址和凭证,建议使用服务端SDK方式。更多信息,请参见上传地址和凭证
  3. 客户端上传前,从应用服务器获取上传地址和凭证,更多信息,请参见上传地址和凭证
  4. 客户端添加本地文件,并设置上传地址和凭证,开始上传。
client_upload

设置上传地址和凭证

上传地址和凭证与每个文件一一对应,因此需要在开始上传的回调里,去用户应用服务器获取并设置给对应的上传实例。具体代码如下所示:

  • iOS示例代码
    //create VODUploadClient object
    self.uploader = [VODUploadClient new];
    //weakself
    __weak typeof(self) weakSelf = self;
    //setup callback
    OnUploadFinishedListener FinishCallbackFunc = ^(UploadFileInfo* fileInfo, VodUploadResult* result){
        NSLog(@"upload finished callback videoid:%@, imageurl:%@", result.videoId, result.imageUrl);
    };
    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) {
        NSLog(@"upload progress callback uploadedSize : %li, totalSize : %li", uploadedSize, totalSize);
    };
    OnUploadTokenExpiredListener TokenExpiredCallbackFunc = ^{
        NSLog(@"upload token expired callback.");
        //token过期,设置新的上传凭证,继续上传
    };
    OnUploadRertyListener RetryCallbackFunc = ^{
        NSLog(@"upload retry begin callback.");
    };
    OnUploadRertyResumeListener RetryResumeCallbackFunc = ^{
        NSLog(@"upload retry end callback.");
    };
    OnUploadStartedListener UploadStartedCallbackFunc = ^(UploadFileInfo* fileInfo) {
        NSLog(@"upload upload started callback.");
        //设置上传地址和上传凭证
        [weakSelf.uploader setUploadAuthAndAddress:fileInfo uploadAuth:`upload auth` uploadAddress:`upload address`];
    };
    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];
                        
  • Android示例代码
    VODUploadClient uploader = new VODUploadClientImpl(getApplicationContext());
    VODUploadCallback callback = new VODUploadCallback() {
        public void onUploadSucceed(UploadFileInfo info) {
            //上传成功
        }
        public void onUploadFailed(UploadFileInfo info, String code, String message) {
              //上传失败
        }
        public void onUploadProgress(UploadFileInfo info, long uploadedSize, long totalSize) {
            //上传进度
        }
        public void onUploadTokenExpired() {
            //上传凭证过期,需要调用刷新凭证接口。
        }
        public void onUploadRetry(String code, String message) {
            //重试回调
        }
        public void onUploadRetryResume() {
        }
        public void onUploadStarted(UploadFileInfo uploadFileInfo) {
            OSSLog.logError("onUploadStarted ------------- ");
            //TODO:从业务服务器获取“上传凭证和地址”
            //设置:uploadAuth, uploadAddress
            uploader.setUploadAuthAndAddress(uploadFileInfo, uploadAuth, uploadAddress);
        }
    };
    uploader.init(callback);
  • H5 JS示例代码
    var uploader = new AliyunUpload.Vod({
        partSize: 1048576,//分片大小默认1 MB,不能小于100 KB
           parallel: 5,//并行上传分片个数,默认5
        retryCount: 3,//网络原因失败时,重新上传次数,默认为3
        retryDuration: 2,//网络原因失败时,重新上传间隔时间,默认为2秒
        'onUploadstarted': function (uploadInfo) {
              uploader.setUploadAuthAndAddress(
                  uploadInfo,
                uploadAuth,
                uploadAddress,
                videoId);
        }
        …… //其他回调
    });

相关参考

如果您想了解如何SDK集成、上传文件队列管理、上传成功回调处理,凭证过期回调处理等,可以阅读相关平台的上传SDK文档。