OSSCompat.m 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // OSSCompat.m
  3. // oss_ios_sdk_new
  4. //
  5. // Created by zhouzhuo on 9/10/15.
  6. // Copyright (c) 2015 aliyun.com. All rights reserved.
  7. //
  8. #import "OSSDefine.h"
  9. #import "OSSCompat.h"
  10. #import "OSSBolts.h"
  11. #import "OSSModel.h"
  12. @implementation OSSClient (Compat)
  13. - (OSSTaskHandler *)uploadData:(NSData *)data
  14. withContentType:(NSString *)contentType
  15. withObjectMeta:(NSDictionary *)meta
  16. toBucketName:(NSString *)bucketName
  17. toObjectKey:(NSString *)objectKey
  18. onCompleted:(void(^)(BOOL, NSError *))onCompleted
  19. onProgress:(void(^)(float progress))onProgress {
  20. OSSTaskHandler * bcts = [OSSCancellationTokenSource cancellationTokenSource];
  21. [[[OSSTask taskWithResult:nil] continueWithExecutor:self.ossOperationExecutor withSuccessBlock:^id(OSSTask *task) {
  22. OSSPutObjectRequest * put = [OSSPutObjectRequest new];
  23. put.bucketName = bucketName;
  24. put.objectKey = objectKey;
  25. put.objectMeta = meta;
  26. put.uploadingData = data;
  27. put.contentType = contentType;
  28. put.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
  29. if (totalBytesExpectedToSend) {
  30. onProgress((float)totalBytesSent / totalBytesExpectedToSend);
  31. }
  32. };
  33. [bcts.token registerCancellationObserverWithBlock:^{
  34. [put cancel];
  35. }];
  36. OSSTask * putTask = [self putObject:put];
  37. [putTask waitUntilFinished];
  38. onProgress(1.0f);
  39. return putTask;
  40. }] continueWithBlock:^id(OSSTask *task) {
  41. if (task.error) {
  42. onCompleted(NO, task.error);
  43. } else {
  44. onCompleted(YES, nil);
  45. }
  46. return nil;
  47. }];
  48. return bcts;
  49. }
  50. - (OSSTaskHandler *)downloadToDataFromBucket:(NSString *)bucketName
  51. objectKey:(NSString *)objectKey
  52. onCompleted:(void (^)(NSData *, NSError *))onCompleted
  53. onProgress:(void (^)(float))onProgress {
  54. OSSTaskHandler * bcts = [OSSCancellationTokenSource cancellationTokenSource];
  55. [[[OSSTask taskWithResult:nil] continueWithExecutor:self.ossOperationExecutor withBlock:^id(OSSTask *task) {
  56. OSSGetObjectRequest * get = [OSSGetObjectRequest new];
  57. get.bucketName = bucketName;
  58. get.objectKey = objectKey;
  59. get.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  60. if (totalBytesExpectedToWrite) {
  61. onProgress((float)totalBytesWritten / totalBytesExpectedToWrite);
  62. }
  63. };
  64. [bcts.token registerCancellationObserverWithBlock:^{
  65. [get cancel];
  66. }];
  67. OSSTask * getTask = [self getObject:get];
  68. [getTask waitUntilFinished];
  69. onProgress(1.0f);
  70. return getTask;
  71. }] continueWithBlock:^id(OSSTask *task) {
  72. if (task.error) {
  73. onCompleted(nil, task.error);
  74. } else {
  75. OSSGetObjectResult * result = task.result;
  76. onCompleted(result.downloadedData, nil);
  77. }
  78. return nil;
  79. }];
  80. return bcts;
  81. }
  82. - (OSSTaskHandler *)downloadToFileFromBucket:(NSString *)bucketName
  83. objectKey:(NSString *)objectKey
  84. toFile:(NSString *)filePath
  85. onCompleted:(void (^)(BOOL, NSError *))onCompleted
  86. onProgress:(void (^)(float))onProgress {
  87. OSSTaskHandler * bcts = [OSSCancellationTokenSource cancellationTokenSource];
  88. [[[OSSTask taskWithResult:nil] continueWithExecutor:self.ossOperationExecutor withBlock:^id(OSSTask *task) {
  89. OSSGetObjectRequest * get = [OSSGetObjectRequest new];
  90. get.bucketName = bucketName;
  91. get.objectKey = objectKey;
  92. get.downloadToFileURL = [NSURL fileURLWithPath:filePath];
  93. get.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  94. if (totalBytesExpectedToWrite) {
  95. onProgress((float)totalBytesWritten / totalBytesExpectedToWrite);
  96. }
  97. };
  98. [bcts.token registerCancellationObserverWithBlock:^{
  99. [get cancel];
  100. }];
  101. OSSTask * getTask = [self getObject:get];
  102. [getTask waitUntilFinished];
  103. onProgress(1.0f);
  104. return getTask;
  105. }] continueWithBlock:^id(OSSTask *task) {
  106. if (task.error) {
  107. onCompleted(NO, task.error);
  108. } else {
  109. onCompleted(YES, nil);
  110. }
  111. return nil;
  112. }];
  113. return bcts;
  114. }
  115. - (void)deleteObjectInBucket:(NSString *)bucketName
  116. objectKey:(NSString *)objectKey
  117. onCompleted:(void (^)(BOOL, NSError *))onCompleted {
  118. [[[OSSTask taskWithResult:nil] continueWithExecutor:self.ossOperationExecutor withBlock:^id(OSSTask *task) {
  119. OSSDeleteObjectRequest * delete = [OSSDeleteObjectRequest new];
  120. delete.bucketName = bucketName;
  121. delete.objectKey = objectKey;
  122. OSSTask * deleteTask = [self deleteObject:delete];
  123. [deleteTask waitUntilFinished];
  124. return deleteTask;
  125. }] continueWithBlock:^id(OSSTask *task) {
  126. if (task.error) {
  127. onCompleted(NO, task.error);
  128. } else {
  129. onCompleted(YES, nil);
  130. }
  131. return nil;
  132. }];
  133. }
  134. - (OSSTaskHandler *)uploadFile:(NSString *)filePath
  135. withContentType:(NSString *)contentType
  136. withObjectMeta:(NSDictionary *)meta
  137. toBucketName:(NSString *)bucketName
  138. toObjectKey:(NSString *)objectKey
  139. onCompleted:(void (^)(BOOL, NSError *))onCompleted
  140. onProgress:(void (^)(float))onProgress {
  141. OSSTaskHandler * bcts = [OSSCancellationTokenSource cancellationTokenSource];
  142. [[[OSSTask taskWithResult:nil] continueWithExecutor:self.ossOperationExecutor withSuccessBlock:^id(OSSTask *task) {
  143. OSSPutObjectRequest * put = [OSSPutObjectRequest new];
  144. put.bucketName = bucketName;
  145. put.objectKey = objectKey;
  146. put.objectMeta = meta;
  147. put.uploadingFileURL = [NSURL fileURLWithPath:filePath];
  148. put.contentType = contentType;
  149. put.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
  150. if (totalBytesExpectedToSend) {
  151. onProgress((float)totalBytesSent / totalBytesExpectedToSend);
  152. }
  153. };
  154. [bcts.token registerCancellationObserverWithBlock:^{
  155. [put cancel];
  156. }];
  157. OSSTask * putTask = [self putObject:put];
  158. [putTask waitUntilFinished];
  159. onProgress(1.0f);
  160. return putTask;
  161. }] continueWithBlock:^id(OSSTask *task) {
  162. if (task.error) {
  163. onCompleted(NO, task.error);
  164. } else {
  165. onCompleted(YES, nil);
  166. }
  167. return nil;
  168. }];
  169. return bcts;
  170. }
  171. - (OSSTaskHandler *)resumableUploadFile:(NSString *)filePath
  172. withContentType:(NSString *)contentType
  173. withObjectMeta:(NSDictionary *)meta
  174. toBucketName:(NSString *)bucketName
  175. toObjectKey:(NSString *)objectKey
  176. onCompleted:(void(^)(BOOL, NSError *))onComplete
  177. onProgress:(void(^)(float progress))onProgress {
  178. OSSTaskHandler * bcts = [OSSCancellationTokenSource cancellationTokenSource];
  179. [[[OSSTask taskWithResult:nil] continueWithBlock:^id(OSSTask *task) {
  180. NSURL * fileURL = [NSURL fileURLWithPath:filePath];
  181. NSDate * lastModified;
  182. NSError * error;
  183. [fileURL getResourceValue:&lastModified forKey:NSURLContentModificationDateKey error:&error];
  184. if (error) {
  185. return [OSSTask taskWithError:error];
  186. }
  187. OSSResumableUploadRequest * resumableUpload = [OSSResumableUploadRequest new];
  188. resumableUpload.bucketName = bucketName;
  189. resumableUpload.deleteUploadIdOnCancelling = NO;//cancel not delete record file
  190. resumableUpload.contentType = contentType;
  191. resumableUpload.completeMetaHeader = meta;
  192. NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
  193. resumableUpload.recordDirectoryPath = cachesDir; //default record file path
  194. resumableUpload.uploadingFileURL = fileURL;
  195. resumableUpload.objectKey = objectKey;
  196. resumableUpload.uploadId = task.result;
  197. resumableUpload.uploadingFileURL = [NSURL fileURLWithPath:filePath];
  198. __weak OSSResumableUploadRequest * weakRef = resumableUpload;
  199. resumableUpload.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
  200. onProgress((float)totalBytesSent/totalBytesExpectedToSend);
  201. if (bcts.token.isCancellationRequested || bcts.isCancellationRequested) {
  202. [weakRef cancel];
  203. }
  204. OSSLogDebugNoFile(@"%lld %lld %lld", bytesSent, totalBytesSent, totalBytesExpectedToSend);
  205. };
  206. return [self resumableUpload:resumableUpload];
  207. }] continueWithBlock:^id(OSSTask *task) {
  208. if (task.cancelled) {
  209. onComplete(NO, [NSError errorWithDomain:OSSClientErrorDomain
  210. code:OSSClientErrorCodeTaskCancelled
  211. userInfo:@{OSSErrorMessageTOKEN: @"This task is cancelled"}]);
  212. } else if (task.error) {
  213. onComplete(NO, task.error);
  214. } else if (task.faulted) {
  215. onComplete(NO, [NSError errorWithDomain:OSSClientErrorDomain
  216. code:OSSClientErrorCodeExcpetionCatched
  217. userInfo:@{OSSErrorMessageTOKEN: [NSString stringWithFormat:@"Catch exception - %@", task.exception]}]);
  218. } else {
  219. onComplete(YES, nil);
  220. }
  221. return nil;
  222. }];
  223. return bcts;
  224. }
  225. @end