iOS Upload SDK

更新时间:2023-02-02 10:57:22

云点播提供iOS上传SDK供您嵌入到您的APP中,方便您将iOS移动设备中的文件上传至云点播。

入门

下载SDK
源代码和安装指南

获取上传令牌和 URL

有关详细信息,请访问获取视频上传令牌

初始化

初始化设置上传域名、超时时间、上传并发和重试次数。

/**
 Create a Client to send upload request to Cloud VoD
 
 @param baseURL --the upload domain(required)
 @param timeout 
 @param concurrentCount --the concurrencies to do multi-part upload,range 5~10。
 @param retryTimes  */
- (instancetype _Nonnull)initWithBaseURL:(NSURL * _Nullable)baseURL
                              andTimeout:(NSTimeInterval)timeout
                         concurrentCount:(NSUInteger)concurrentCount
                              retryTimes:(NSUInteger)retryTimes;

如何上传

普通上传

  WCSUploadObjectRequest *request = [[WCSUploadObjectRequest alloc] init];
  request.token = @"the upload token,get the token via get-upload-token API";
  request.fileName = @"origin name of the fie to be uploaded";
  request.key = @"file name to be displayed on Cloud VoD console, will use fileName by default";
  request.fileData = fileData; // file content to be uploaded
  request.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
    NSLog(@"%lld bytes sent, %lld total bytes sent, %lld total byte exptected", bytesSent, totalBytesSent, totalBytesExpectedToSend);
  };
  WCSClient *client = [[WCSClient alloc] initWithBaseURL:[NSURL URLWithString:"upload.cloudv.haplat.net"] andTimeout:30];
  [[client uploadRequest:request] continueWithBlock:^id _Nullable(WCSTask<WCSUploadObjectResult *> * _Nonnull task) {
    if (task.error) {
      NSLog(@"The request failed. error: [%@]", task.error);
    } else {
      // Below is the response if request successfully
      NSDictionary *responseResult = task.result.results;
    }
    return nil;
  }];

分段上传

在移动设备上上传大文件需要很长时间,一旦文件上传出现异常终止,需要重新上传所有内容,影响用户体验。因此,我们建议您在上传大文件时使用 Multi-part Upload。
Multi-part Upload 是将一个大文件切割成多个块和自定义大小的块,然后将这些块并行上传。如果一个块上传失败,客户端只需要重新上传这个区块即可。

注意:每个块最大不能超过100M;最小尺寸不能小于4M。

 WCSUploadObjectRequest *request = [[WCSUploadObjectRequest alloc] init];
  request.token = @"the upload token,get the token via get-upload-token API";
  request.fileName = @"origin name of the fie to be uploaded";
  request.key = @"file name to be displayed on Cloud VoD console, will use fileName by default";
  request.fileData = fileData; // file content to be uploaded
  request.blockSize = 8 * 1024 * 1024; // block size
  request.chunkSize = 64 * 1024; // chunk size
  request.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
    NSLog(@"%lld bytes sent, %lld total bytes sent, %lld total byte exptected", bytesSent, totalBytesSent, totalBytesExpectedToSend);
  };
  WCSClient *client = [[WCSClient alloc] initWithBaseURL:nil andTimeout:30];
  [[client uploadRequest:request] continueWithBlock:^id _Nullable(WCSTask<WCSUploadObjectResult *> * _Nonnull task) {
    if (task.error) {
      NSLog(@"The request failed. error: [%@]", task.error);
    } else {
      // Below is the response if request successfully
      NSDictionary *responseResult = task.result.results;
    }
    return nil;
  }];
本篇文档内容对您是否有帮助?
有帮助
我要反馈
提交成功!非常感谢您的反馈,我们会继续努力做到更好!