aliyun-oss-react-native

RNAliyunOSS+MULTIPARTUPLOAD.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // RNAliyunOSS+MULTIPARTUPLOAD.m
  3. // aliyun-oss-rn-sdk
  4. // Created by 罗章 on 2018/5/10.
  5. #import "RNAliyunOSS+MULTIPARTUPLOAD.h"
  6. @implementation RNAliyunOSS (MULTIPARTUPLOAD)
  7. /*
  8. *initMultipartUpload
  9. */
  10. RCT_REMAP_METHOD(initMultipartUpload,withBucketName:(NSString *)bucketName objectKey:(NSString *)objectKey resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
  11. /**init mulitpart*/
  12. __block NSString * uploadId = nil;
  13. NSString * uploadToBucket = bucketName;
  14. NSString * uploadObjectkey = objectKey;
  15. OSSInitMultipartUploadRequest * init = [OSSInitMultipartUploadRequest new];
  16. init.bucketName = uploadToBucket;
  17. init.objectKey = uploadObjectkey;
  18. // init.contentType = @"application/octet-stream";
  19. OSSTask * initTask = [self.client multipartUploadInit:init];
  20. [initTask waitUntilFinished];
  21. if (!initTask.error) {
  22. OSSInitMultipartUploadResult * result = initTask.result;
  23. uploadId = result.uploadId;
  24. NSLog(@"initMultipartLoad success!");
  25. resolve(uploadId);
  26. } else {
  27. NSLog(@"multipart upload failed, error: %@", initTask.error);
  28. reject(@"Error",@"multipart upload failed",initTask.error);
  29. return;
  30. }
  31. }
  32. /*
  33. multipartUpload
  34. */
  35. RCT_REMAP_METHOD(multipartUpload, withBucketName:(NSString *)bucketName objectKey:(NSString *)objectKey uploadId:(NSString*)uploadId withFilePath:(NSString*)filePath options:(NSDictionary*)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
  36. __block NSMutableArray * partInfos = [NSMutableArray new];
  37. filePath = [NSHomeDirectory() stringByAppendingPathComponent:filePath];
  38. //分片上传数量
  39. int chuckCount = 2;
  40. //获取文件大小
  41. long fileSize;
  42. NSFileManager* manager =[NSFileManager defaultManager];
  43. if ([manager fileExistsAtPath:filePath]){
  44. fileSize = [[manager attributesOfItemAtPath:filePath error:nil]fileSize];
  45. }
  46. //分片大小
  47. uint64_t offset = fileSize/chuckCount;
  48. for (int i = 1; i <= chuckCount; i++) {
  49. OSSUploadPartRequest * uploadPart = [OSSUploadPartRequest new];
  50. uploadPart.bucketName = bucketName;
  51. uploadPart.objectkey = objectKey;
  52. uploadPart.uploadId = uploadId;
  53. uploadPart.partNumber = i; // part number start from 1
  54. NSFileHandle* readHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
  55. [readHandle seekToFileOffset:offset * (i -1)];
  56. NSData* data = [readHandle readDataOfLength:offset];
  57. uploadPart.uploadPartData = data;
  58. OSSTask * uploadPartTask = [self.client uploadPart:uploadPart];
  59. [uploadPartTask waitUntilFinished];
  60. if (!uploadPartTask.error) {
  61. OSSUploadPartResult * result = uploadPartTask.result;
  62. uint64_t fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:uploadPart.uploadPartFileURL.absoluteString error:nil] fileSize];
  63. [partInfos addObject:[OSSPartInfo partInfoWithPartNum:i eTag:result.eTag size:fileSize]];
  64. } else {
  65. NSLog(@"upload part error: %@", uploadPartTask.error);
  66. return;
  67. }
  68. }
  69. //完成分片上传
  70. OSSCompleteMultipartUploadRequest * complete = [OSSCompleteMultipartUploadRequest new];
  71. complete.bucketName = bucketName;
  72. complete.objectKey = objectKey;
  73. complete.uploadId = uploadId;
  74. complete.partInfos = partInfos;
  75. OSSTask * completeTask = [self.client completeMultipartUpload:complete];
  76. [[completeTask continueWithBlock:^id(OSSTask *task) {
  77. if (!task.error) {
  78. OSSCompleteMultipartUploadResult * result = task.result;
  79. NSLog(@"upload server success");
  80. resolve(@"upload server success");
  81. } else {
  82. NSLog(@"upload server failed",task.error);
  83. reject(@"Error",@"upload server failed",task.error);
  84. }
  85. return nil;
  86. }] waitUntilFinished];
  87. }
  88. /*
  89. * abortMultipartUpload
  90. */
  91. RCT_REMAP_METHOD(abortMultipartUpload,withBucketName: (NSString*)bucketName objectKey: (NSString*)objectKey uploadId:(NSString*)uploadId resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
  92. OSSAbortMultipartUploadRequest * abort = [OSSAbortMultipartUploadRequest new];
  93. abort.bucketName = bucketName;
  94. abort.objectKey = objectKey;
  95. abort.uploadId = uploadId;
  96. OSSTask * abortTask = [self.client abortMultipartUpload:abort];
  97. [abortTask waitUntilFinished];
  98. if (!abortTask.error) {
  99. OSSAbortMultipartUploadResult * result = abortTask.result;
  100. // uploadId = result.uploadId;
  101. NSLog(@"abort success!");
  102. resolve(@"abort success");
  103. } else {
  104. NSLog(@"multipart upload failed, error: %@", abortTask.error);
  105. reject(@"Error",@"multipart upload failed",abortTask.error);
  106. return;
  107. }
  108. }
  109. /*
  110. * list parts
  111. */
  112. RCT_REMAP_METHOD(listParts, withBucketName:(NSString*)bucketName withObjectKey: (NSString*)objectKey uploadId:(NSString*)uploadId resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
  113. OSSListPartsRequest * listParts = [OSSListPartsRequest new];
  114. listParts.bucketName = bucketName;
  115. listParts.objectKey = objectKey;
  116. listParts.uploadId = uploadId;
  117. OSSTask * listPartTask = [self.client listParts:listParts];
  118. [listPartTask continueWithBlock:^id(OSSTask *task) {
  119. if (!task.error) {
  120. NSLog(@"list part result success!");
  121. OSSListPartsResult * listPartResult = task.result;
  122. for (NSDictionary * partInfo in listPartResult.parts) {
  123. NSLog(@"each part: %@", partInfo);
  124. }
  125. resolve(@"listParst success");
  126. } else {
  127. NSLog(@"list part result error: %@", task.error);
  128. reject(@"Error",@"list part result error",task.error);
  129. }
  130. return nil;
  131. }];
  132. }
  133. @end