全部产品
云市场

使用上传地址和凭证上传

更新时间:2018-12-11 23:17:53

简介

背景

开发指南-客户端上传 里,介绍了客户端提供了两种授权方式上传到点播:

本文重点介绍使用上传地址和凭证进行上传的实现过程,这也是点播服务默认推荐的上传方式相比STS方式拥有诸多优势

上传地址和凭证介绍

上传地址和凭证 是点播服务下发的上传媒体文件到点播存储的授权凭证和文件地址。详情参考 上传地址和凭证

基本流程

客户端上传流程.png

实现过程

  1. 完成准备工作,建议使用 子账号AccessKey 访问点播服务,注意务必要授予点播权限(AliyunVODFullAccess)。
  2. 在应用服务器上,部署授权服务,获取上传地址和凭证,建议使用服务端SDK方式。
  3. 客户端上传时,从应用服务器获取上传地址和凭证。
  4. 客户端添加本地文件,并设置上传地址和凭证,开始上传。

设置上传地址和凭证

上传地址和凭证是跟每个文件一一对应的,所以建议在开始上传的回调里面再去用户应用服务器(AppServer)进行获取并设置给上传实例。具体代码如下:

iOS示例代码

  1. // create VODUploadClient object
  2. self.uploader = [VODUploadClient new];
  3. // weakself
  4. __weak typeof(self) weakSelf = self;
  5. // setup callback
  6. OnUploadFinishedListener FinishCallbackFunc = ^(UploadFileInfo* fileInfo, VodUploadResult* result){
  7. NSLog(@"upload finished callback videoid:%@, imageurl:%@", result.videoId, result.imageUrl);
  8. };
  9. OnUploadFailedListener FailedCallbackFunc = ^(UploadFileInfo* fileInfo, NSString *code, NSString* message){
  10. NSLog(@"upload failed callback code = %@, error message = %@", code, message);
  11. };
  12. OnUploadProgressListener ProgressCallbackFunc = ^(UploadFileInfo* fileInfo, long uploadedSize, long totalSize) {
  13. NSLog(@"upload progress callback uploadedSize : %li, totalSize : %li", uploadedSize, totalSize);
  14. };
  15. OnUploadTokenExpiredListener TokenExpiredCallbackFunc = ^{
  16. NSLog(@"upload token expired callback.");
  17. // token过期,设置新的上传凭证,继续上传
  18. };
  19. OnUploadRertyListener RetryCallbackFunc = ^{
  20. NSLog(@"upload retry begin callback.");
  21. };
  22. OnUploadRertyResumeListener RetryResumeCallbackFunc = ^{
  23. NSLog(@"upload retry end callback.");
  24. };
  25. OnUploadStartedListener UploadStartedCallbackFunc = ^(UploadFileInfo* fileInfo) {
  26. NSLog(@"upload upload started callback.");
  27. // 设置上传地址 和 上传凭证
  28. [weakSelf.uploader setUploadAuthAndAddress:fileInfo uploadAuth:`upload auth` uploadAddress:`upload address`];
  29. };
  30. VODUploadListener *listener = [[VODUploadListener alloc] init];
  31. listener.finish = FinishCallbackFunc;
  32. listener.failure = FailedCallbackFunc;
  33. listener.progress = ProgressCallbackFunc;
  34. listener.expire = TokenExpiredCallbackFunc;
  35. listener.retry = RetryCallbackFunc;
  36. listener.retryResume = RetryResumeCallbackFunc;
  37. listener.started = UploadStartedCallbackFunc;
  38. // init with upload address and upload auth
  39. [self.uploader init:listener];

Android示例代码

  1. VODUploadClient uploader = new VODUploadClientImpl(getApplicationContext());
  2. VODUploadCallback callback = new VODUploadCallback() {
  3. public void onUploadSucceed(UploadFileInfo info) {
  4. //上传成功
  5. }
  6. public void onUploadFailed(UploadFileInfo info, String code, String message) {
  7. //上传失败
  8. }
  9. public void onUploadProgress(UploadFileInfo info, long uploadedSize, long totalSize) {
  10. //上传进度
  11. }
  12. public void onUploadTokenExpired() {
  13. //上传凭证过期,需要调用刷新凭证接口。
  14. }
  15. public void onUploadRetry(String code, String message) {
  16. //重试回调
  17. }
  18. public void onUploadRetryResume() {
  19. }
  20. public void onUploadStarted(UploadFileInfo uploadFileInfo) {
  21. OSSLog.logError("onUploadStarted ------------- ");
  22. //TODO:从业务服务器获取 “上传凭证和地址”
  23. //设置:uploadAuth, uploadAddress
  24. uploader.setUploadAuthAndAddress(uploadFileInfo, uploadAuth, uploadAddress);
  25. }
  26. };
  27. uploader.init(callback);

H5 JS示例代码

  1. var uploader = new AliyunUpload.Vod({
  2. partSize: 1048576,//分片大小默认1M,不能小于100K
  3. parallel: 5,//并行上传分片个数,默认5
  4. retryCount: 3,//网络原因失败时,重新上传次数,默认为3
  5. retryDuration: 2,//网络原因失败时,重新上传间隔时间,默认为2秒
  6. 'onUploadstarted': function (uploadInfo) {
  7. uploader.setUploadAuthAndAddress(
  8. uploadInfo,
  9. uploadAuth,
  10. uploadAddress,
  11. videoId);
  12. }
  13. …… //其他回调
  14. });

更多参考

更加详细的说明请阅读下面的文档:包括如何集成,上传文件队列管理,上传成功回调处理,凭证过期回调处理等可以阅读相关平台的上传SDK文档。