当前位置:   article > 正文

iOS阿里云OSS上传多张图片和视频_ios 阿里云oss上传图片

ios 阿里云oss上传图片

1.从服务器获取上传需要的 accessKeyId, accessKeySecret, securityToken;如果你们是本地直接配置,不需要从服务器拿直接省略。

2.配置OSSTask

3.1 上传图片

  1. /// 上传回调
  2. typedef void(^uploadCallblock)(BOOL success, NSString* msg, NSArray<NSString *>* keys);
  3. /// 上传图片
  4. + (void)uploadImages:(NSArray *)images isAsync:(BOOL)isAsync callback:(uploadCallblock)callback {
  5. // 1
  6. [self getOSSAuth:^(OSSAuthModel *OSSAuth) {
  7. if (!OSSAuth) {
  8. if (callback) callback(NO ,@"获取上传token失败", nil);
  9. return ;
  10. }
  11. // 2
  12. id<OSSCredentialProvider> credential = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:OSSAuth.accessKeyId secretKeyId:OSSAuth.accessKeySecret securityToken:OSSAuth.securityToken];
  13. OSSClient *client = [[OSSClient alloc] initWithEndpoint:kOSSEndpoint credentialProvider:credential];
  14. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  15. queue.maxConcurrentOperationCount = images.count;
  16. NSMutableArray *callBackNames = [NSMutableArray array];
  17. int i = 0;
  18. for (UIImage *image in images) {
  19. if (image) {
  20. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  21. //任务执行
  22. OSSPutObjectRequest * put = [OSSPutObjectRequest new];
  23. put.bucketName = kOSSBucketName;
  24. NSString *imageName = [NSString stringWithFormat:@"%@%@", OSSAuth.folder,[NSString randomStringWithLength:8]];
  25. NSString *suffix = [NSString stringWithFormat:@"-%zdx%zd.png", (NSInteger)image.size.width,(NSInteger)image.size.height];
  26. imageName = [imageName stringByAppendingString:suffix];
  27. put.objectKey = imageName;
  28. // 传出url
  29. NSString *imageUrl = [NSString stringWithFormat:@"%@/%@", OSSAuth.domain,imageName];
  30. [callBackNames addObject:imageUrl];
  31. NSData *data = UIImageJPEGRepresentation(image, 0.5);
  32. put.uploadingData = data;
  33. OSSTask * putTask = [client putObject:put];
  34. [putTask waitUntilFinished]; // 阻塞直到上传完成
  35. if (!putTask.error) {
  36. NSLog(@"upload object success! \nimageUrl:%@\n", imageUrl);
  37. } else {
  38. NSLog(@"upload object failed, error: %@" , putTask.error);
  39. }
  40. if (isAsync) {
  41. if (image == images.lastObject) {
  42. NSLog(@"upload object finished!");
  43. if (callback) {
  44. callback( YES, @"全部上传完成" , [callBackNames copy]);
  45. }
  46. }
  47. }
  48. }];
  49. if (queue.operations.count != 0) {
  50. [operation addDependency:queue.operations.lastObject];
  51. }
  52. [queue addOperation:operation];
  53. }
  54. i++;
  55. }
  56. if (!isAsync) {
  57. [queue waitUntilAllOperationsAreFinished];
  58. if (callback) {
  59. callback( YES, @"全部上传完成" , [callBackNames copy]);
  60. }
  61. }
  62. }];
  63. }

3.2 上传视频

 

  1. /// 上传视频
  2. + (void)asyncUploadVideo:(NSData *)data callback:(uploadCallblock)callback {
  3. // 1
  4. [self getOSSAuth:^(OSSAuthModel *OSSAuth) {
  5. if (!OSSAuth) {
  6. if (callback) callback(NO ,@"获取上传token失败" , nil);
  7. return ;
  8. }
  9. if (data.length > 52428800) {
  10. if (callback) callback(NO ,@"视频文件最大不能超过50M" , nil);
  11. return;
  12. }
  13. // 2
  14. id<OSSCredentialProvider> credential = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:OSSAuth.accessKeyId secretKeyId:OSSAuth.accessKeySecret securityToken:OSSAuth.securityToken];
  15. OSSClient *client = [[OSSClient alloc] initWithEndpoint:kOSSEndpoint credentialProvider:credential];
  16. //任务执行
  17. OSSPutObjectRequest * put = [OSSPutObjectRequest new];
  18. put.bucketName = kOSSBucketName;
  19. NSString *imageName = [NSString stringWithFormat:@"%@%@.mp4", OSSAuth.folder,[NSString randomStringWithLength:8]];
  20. put.objectKey = imageName;
  21. // 3
  22. // 传出url
  23. NSString *imageUrl = [NSString stringWithFormat:@"%@/%@", OSSAuth.domain,imageName];
  24. put.uploadingData = data;
  25. OSSTask * putTask = [client putObject:put];
  26. [putTask waitUntilFinished]; // 阻塞直到上传完成
  27. if (!putTask.error) {
  28. NSLog(@"upload object success!");
  29. if (callback) callback( YES, @"上传成功", @[imageUrl]);
  30. } else {
  31. NSLog(@"upload object failed, error: %@" , putTask.error);
  32. if (callback) callback( NO, @"上传失败", nil);
  33. }
  34. }];
  35. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/245988
推荐阅读
相关标签
  

闽ICP备14008679号