OSSClient.m 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268
  1. //
  2. // OSSClient.m
  3. // oss_ios_sdk
  4. //
  5. // Created by zhouzhuo on 8/16/15.
  6. // Copyright (c) 2015 aliyun.com. All rights reserved.
  7. //
  8. #import "OSSClient.h"
  9. #import "OSSDefine.h"
  10. #import "OSSModel.h"
  11. #import "OSSUtil.h"
  12. #import "OSSLog.h"
  13. #import "OSSBolts.h"
  14. #import "OSSNetworking.h"
  15. #import "OSSXMLDictionary.h"
  16. #import "OSSIPv6Adapter.h"
  17. #import "OSSNetworkingRequestDelegate.h"
  18. #import "OSSAllRequestNeededMessage.h"
  19. #import "OSSURLRequestRetryHandler.h"
  20. #import "OSSHttpResponseParser.h"
  21. #import "OSSGetObjectACLRequest.h"
  22. #import "OSSDeleteMultipleObjectsRequest.h"
  23. #import "OSSGetBucketInfoRequest.h"
  24. #import "OSSPutSymlinkRequest.h"
  25. #import "OSSGetSymlinkRequest.h"
  26. #import "OSSRestoreObjectRequest.h"
  27. #import "OSSGetObjectTaggingRequest.h"
  28. #import "OSSPutObjectTaggingRequest.h"
  29. #import "OSSDeleteObjectTaggingRequest.h"
  30. static NSString * const kClientRecordNameWithCommonPrefix = @"oss_partInfos_storage_name";
  31. static NSString * const kClientRecordNameWithCRC64Suffix = @"-crc64";
  32. static NSString * const kClientRecordNameWithSequentialSuffix = @"-sequential";
  33. static NSUInteger const kClientMaximumOfChunks = 5000; //max part number
  34. static NSUInteger const kPartSizeAlign = 4 * 1024; // part size byte alignment
  35. static NSString * const kClientErrorMessageForEmptyFile = @"the length of file should not be 0!";
  36. static NSString * const kClientErrorMessageForCancelledTask = @"This task has been cancelled!";
  37. /**
  38. * extend OSSRequest to include the ref to networking request object
  39. */
  40. @interface OSSRequest ()
  41. @property (nonatomic, strong) OSSNetworkingRequestDelegate * requestDelegate;
  42. @end
  43. @interface OSSClient()
  44. - (void)enableCRC64WithFlag:(OSSRequestCRCFlag)flag requestDelegate:(OSSNetworkingRequestDelegate *)delegate;
  45. - (OSSTask *)preChecksForRequest:(OSSMultipartUploadRequest *)request;
  46. - (void)checkRequestCrc64Setting:(OSSRequest *)request;
  47. - (OSSTask *)checkNecessaryParamsOfRequest:(OSSMultipartUploadRequest *)request;
  48. - (OSSTask *)checkPartSizeForRequest:(OSSMultipartUploadRequest *)request;
  49. - (NSUInteger)judgePartSizeForMultipartRequest:(OSSMultipartUploadRequest *)request fileSize:(unsigned long long)fileSize;
  50. - (unsigned long long)getSizeWithFilePath:(nonnull NSString *)filePath error:(NSError **)error;
  51. - (NSString *)readUploadIdForRequest:(OSSResumableUploadRequest *)request recordFilePath:(NSString **)recordFilePath sequential:(BOOL)sequential;
  52. - (NSMutableDictionary *)localPartInfosDictoryWithUploadId:(NSString *)uploadId;
  53. - (OSSTask *)persistencePartInfos:(NSDictionary *)partInfos withUploadId:(NSString *)uploadId;
  54. - (OSSTask *)checkFileSizeWithRequest:(OSSMultipartUploadRequest *)request;
  55. + (NSError *)cancelError;
  56. @end
  57. @implementation OSSClient
  58. static NSObject *lock;
  59. - (instancetype)initWithEndpoint:(NSString *)endpoint credentialProvider:(id<OSSCredentialProvider>)credentialProvider {
  60. return [self initWithEndpoint:endpoint credentialProvider:credentialProvider clientConfiguration:[OSSClientConfiguration new]];
  61. }
  62. - (instancetype)initWithEndpoint:(NSString *)endpoint
  63. credentialProvider:(id<OSSCredentialProvider>)credentialProvider
  64. clientConfiguration:(OSSClientConfiguration *)conf {
  65. if (self = [super init]) {
  66. if (!lock) {
  67. lock = [NSObject new];
  68. }
  69. NSOperationQueue * queue = [NSOperationQueue new];
  70. // using for resumable upload and compat old interface
  71. queue.maxConcurrentOperationCount = 3;
  72. _ossOperationExecutor = [OSSExecutor executorWithOperationQueue:queue];
  73. if (![endpoint oss_isNotEmpty]) {
  74. [NSException raise:NSInvalidArgumentException
  75. format:@"endpoint should not be nil or empty!"];
  76. }
  77. if ([endpoint rangeOfString:@"://"].location == NSNotFound) {
  78. endpoint = [@"https://" stringByAppendingString:endpoint];
  79. }
  80. NSURL *endpointURL = [NSURL URLWithString:endpoint];
  81. if ([endpointURL.scheme.lowercaseString isEqualToString:@"https"]) {
  82. if ([[OSSIPv6Adapter getInstance] isIPv4Address: endpointURL.host] || [[OSSIPv6Adapter getInstance] isIPv6Address: endpointURL.host]) {
  83. [NSException raise:NSInvalidArgumentException
  84. format:@"unsupported format of endpoint, please use right endpoint format!"];
  85. }
  86. }
  87. self.endpoint = [endpoint oss_trim];
  88. self.credentialProvider = credentialProvider;
  89. self.clientConfiguration = conf;
  90. OSSNetworkingConfiguration * netConf = [OSSNetworkingConfiguration new];
  91. if (conf) {
  92. netConf.maxRetryCount = conf.maxRetryCount;
  93. netConf.timeoutIntervalForRequest = conf.timeoutIntervalForRequest;
  94. netConf.timeoutIntervalForResource = conf.timeoutIntervalForResource;
  95. netConf.enableBackgroundTransmitService = conf.enableBackgroundTransmitService;
  96. netConf.backgroundSessionIdentifier = conf.backgroundSesseionIdentifier;
  97. netConf.proxyHost = conf.proxyHost;
  98. netConf.proxyPort = conf.proxyPort;
  99. netConf.maxConcurrentRequestCount = conf.maxConcurrentRequestCount;
  100. netConf.enableFollowRedirects = conf.isFollowRedirectsEnable;
  101. netConf.HTTPMaximumConnectionsPerHost = conf.HTTPMaximumConnectionsPerHost;
  102. }
  103. self.networking = [[OSSNetworking alloc] initWithConfiguration:netConf];
  104. }
  105. return self;
  106. }
  107. - (OSSTask *)invokeRequest:(OSSNetworkingRequestDelegate *)request requireAuthentication:(BOOL)requireAuthentication {
  108. /* if content-type haven't been set, we set one */
  109. if (!request.allNeededMessage.contentType.oss_isNotEmpty
  110. && ([request.allNeededMessage.httpMethod isEqualToString:@"POST"] || [request.allNeededMessage.httpMethod isEqualToString:@"PUT"])) {
  111. request.allNeededMessage.contentType = [OSSUtil detemineMimeTypeForFilePath:request.uploadingFileURL.path uploadName:request.allNeededMessage.objectKey];
  112. }
  113. // Checks if the endpoint is in the excluded CName list.
  114. [self.clientConfiguration.cnameExcludeList enumerateObjectsUsingBlock:^(NSString *exclude, NSUInteger idx, BOOL * _Nonnull stop) {
  115. if ([self.endpoint hasSuffix:exclude]) {
  116. request.allNeededMessage.isHostInCnameExcludeList = YES;
  117. *stop = YES;
  118. }
  119. }];
  120. id<OSSRequestInterceptor> uaSetting = [[OSSUASettingInterceptor alloc] initWithClientConfiguration:self.clientConfiguration];
  121. [request.interceptors addObject:uaSetting];
  122. /* check if the authentication is required */
  123. if (requireAuthentication) {
  124. id<OSSRequestInterceptor> signer = [[OSSSignerInterceptor alloc] initWithCredentialProvider:self.credentialProvider];
  125. [request.interceptors addObject:signer];
  126. }
  127. request.isHttpdnsEnable = self.clientConfiguration.isHttpdnsEnable;
  128. request.isPathStyleAccessEnable = self.clientConfiguration.isPathStyleAccessEnable;
  129. request.isCustomPathPrefixEnable = self.clientConfiguration.isCustomPathPrefixEnable;
  130. return [_networking sendRequest:request];
  131. }
  132. #pragma implement restful apis
  133. - (OSSTask *)getService:(OSSGetServiceRequest *)request {
  134. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  135. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeGetService];
  136. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  137. neededMsg.endpoint = self.endpoint;
  138. neededMsg.httpMethod = OSSHTTPMethodGET;
  139. neededMsg.params = [request requestParams];
  140. requestDelegate.allNeededMessage = neededMsg;
  141. requestDelegate.operType = OSSOperationTypeGetService;
  142. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  143. }
  144. # pragma mark - Private Methods
  145. - (void)enableCRC64WithFlag:(OSSRequestCRCFlag)flag requestDelegate:(OSSNetworkingRequestDelegate *)delegate
  146. {
  147. switch (flag) {
  148. case OSSRequestCRCOpen:
  149. delegate.crc64Verifiable = YES;
  150. break;
  151. case OSSRequestCRCClosed:
  152. delegate.crc64Verifiable = NO;
  153. break;
  154. default:
  155. delegate.crc64Verifiable = self.clientConfiguration.crc64Verifiable;
  156. break;
  157. }
  158. }
  159. - (OSSTask *)preChecksForRequest:(OSSMultipartUploadRequest *)request
  160. {
  161. OSSTask *preTask = [self checkFileSizeWithRequest:request];
  162. if (preTask) {
  163. return preTask;
  164. }
  165. preTask = [self checkNecessaryParamsOfRequest:request];
  166. if (preTask) {
  167. return preTask;
  168. }
  169. preTask = [self checkPartSizeForRequest:request];
  170. if (preTask) {
  171. return preTask;
  172. }
  173. return preTask;
  174. }
  175. - (void)checkRequestCrc64Setting:(OSSRequest *)request
  176. {
  177. if (request.crcFlag == OSSRequestCRCUninitialized)
  178. {
  179. if (self.clientConfiguration.crc64Verifiable)
  180. {
  181. request.crcFlag = OSSRequestCRCOpen;
  182. }else
  183. {
  184. request.crcFlag = OSSRequestCRCClosed;
  185. }
  186. }
  187. }
  188. - (OSSTask *)checkNecessaryParamsOfRequest:(OSSMultipartUploadRequest *)request
  189. {
  190. NSError *error = nil;
  191. if (![request.objectKey oss_isNotEmpty]) {
  192. error = [NSError errorWithDomain:OSSClientErrorDomain
  193. code:OSSClientErrorCodeInvalidArgument
  194. userInfo:@{OSSErrorMessageTOKEN: @"checkNecessaryParamsOfRequest requires nonnull objectKey!"}];
  195. }else if (![request.bucketName oss_isNotEmpty]) {
  196. error = [NSError errorWithDomain:OSSClientErrorDomain
  197. code:OSSClientErrorCodeInvalidArgument
  198. userInfo:@{OSSErrorMessageTOKEN: @"checkNecessaryParamsOfRequest requires nonnull bucketName!"}];
  199. }else if (![request.uploadingFileURL.path oss_isNotEmpty]) {
  200. error = [NSError errorWithDomain:OSSClientErrorDomain
  201. code:OSSClientErrorCodeInvalidArgument
  202. userInfo:@{OSSErrorMessageTOKEN: @"checkNecessaryParamsOfRequest requires nonnull uploadingFileURL!"}];
  203. }
  204. OSSTask *errorTask = nil;
  205. if (error) {
  206. errorTask = [OSSTask taskWithError:error];
  207. }
  208. return errorTask;
  209. }
  210. - (OSSTask *)checkPartSizeForRequest:(OSSMultipartUploadRequest *)request
  211. {
  212. OSSTask *errorTask = nil;
  213. unsigned long long fileSize = [self getSizeWithFilePath:request.uploadingFileURL.path error:nil];
  214. if (request.partSize == 0 || (fileSize > 102400 && request.partSize < 102400)) {
  215. NSError *error = [NSError errorWithDomain:OSSClientErrorDomain
  216. code:OSSClientErrorCodeInvalidArgument
  217. userInfo:@{OSSErrorMessageTOKEN: @"Part size must be greater than equal to 100KB"}];
  218. errorTask = [OSSTask taskWithError:error];
  219. }
  220. return errorTask;
  221. }
  222. - (NSUInteger)judgePartSizeForMultipartRequest:(OSSMultipartUploadRequest *)request fileSize:(unsigned long long)fileSize
  223. {
  224. #pragma clang diagnostic push
  225. #pragma clang diagnostic ignored "-Wshorten-64-to-32"
  226. BOOL divisible = (fileSize % request.partSize == 0);
  227. NSUInteger partCount = (fileSize / request.partSize) + (divisible? 0 : 1);
  228. if(partCount > kClientMaximumOfChunks)
  229. {
  230. NSUInteger partSize = fileSize / (kClientMaximumOfChunks - 1);
  231. request.partSize = [self ceilPartSize:partSize];
  232. partCount = (fileSize / request.partSize) + ((fileSize % request.partSize == 0) ? 0 : 1);
  233. }
  234. return partCount;
  235. #pragma clang diagnostic pop
  236. }
  237. - (NSUInteger)ceilPartSize:(NSUInteger)partSize {
  238. partSize = (((partSize + (kPartSizeAlign - 1)) / kPartSizeAlign) * kPartSizeAlign);
  239. return partSize;
  240. }
  241. - (unsigned long long)getSizeWithFilePath:(nonnull NSString *)filePath error:(NSError **)error
  242. {
  243. NSFileManager *fm = [NSFileManager defaultManager];
  244. NSDictionary *attributes = [fm attributesOfItemAtPath:filePath error:error];
  245. return attributes.fileSize;
  246. }
  247. - (NSString *)readUploadIdForRequest:(OSSResumableUploadRequest *)request recordFilePath:(NSString **)recordFilePath sequential:(BOOL)sequential error:(NSError **)error
  248. {
  249. NSString *uploadId = nil;
  250. NSString *record = [NSString stringWithFormat:@"%@%@%@%lu", request.md5String, request.bucketName, request.objectKey, (unsigned long)request.partSize];
  251. if (sequential) {
  252. record = [record stringByAppendingString:kClientRecordNameWithSequentialSuffix];
  253. }
  254. if (request.crcFlag == OSSRequestCRCOpen) {
  255. record = [record stringByAppendingString:kClientRecordNameWithCRC64Suffix];
  256. }
  257. NSData *data = [record dataUsingEncoding:NSUTF8StringEncoding];
  258. NSString *recordFileName = [OSSUtil dataMD5String:data error:error];
  259. *recordFilePath = [request.recordDirectoryPath stringByAppendingPathComponent: recordFileName];
  260. NSFileManager *fileManager = [NSFileManager defaultManager];
  261. if ([fileManager fileExistsAtPath: *recordFilePath]) {
  262. NSFileHandle * read = [NSFileHandle fileHandleForReadingAtPath:*recordFilePath];
  263. uploadId = [[NSString alloc] initWithData:[read readDataToEndOfFile] encoding:NSUTF8StringEncoding];
  264. [read closeFile];
  265. } else {
  266. [fileManager createFileAtPath:*recordFilePath contents:nil attributes:nil];
  267. }
  268. return uploadId;
  269. }
  270. #pragma mark - sequential multipart upload
  271. - (NSMutableDictionary *)localPartInfosDictoryWithUploadId:(NSString *)uploadId
  272. {
  273. NSMutableDictionary *localPartInfoDict = nil;
  274. NSString *partInfosDirectory = [[NSString oss_documentDirectory] stringByAppendingPathComponent:kClientRecordNameWithCommonPrefix];
  275. NSString *partInfosPath = [partInfosDirectory stringByAppendingPathComponent:uploadId];
  276. BOOL isDirectory;
  277. NSFileManager *defaultFM = [NSFileManager defaultManager];
  278. if (!([defaultFM fileExistsAtPath:partInfosDirectory isDirectory:&isDirectory] && isDirectory))
  279. {
  280. if (![defaultFM createDirectoryAtPath:partInfosDirectory
  281. withIntermediateDirectories:NO
  282. attributes:nil error:nil]) {
  283. OSSLogError(@"create Directory(%@) failed!",partInfosDirectory);
  284. };
  285. }
  286. if (![defaultFM fileExistsAtPath:partInfosPath])
  287. {
  288. if (![defaultFM createFileAtPath:partInfosPath
  289. contents:nil
  290. attributes:nil])
  291. {
  292. OSSLogError(@"create local partInfo file failed!");
  293. }
  294. }
  295. localPartInfoDict = [[NSMutableDictionary alloc] initWithContentsOfURL:[NSURL fileURLWithPath:partInfosPath]];
  296. return localPartInfoDict;
  297. }
  298. - (OSSTask *)persistencePartInfos:(NSDictionary *)partInfos withUploadId:(NSString *)uploadId
  299. {
  300. NSString *filePath = [[[NSString oss_documentDirectory] stringByAppendingPathComponent:kClientRecordNameWithCommonPrefix] stringByAppendingPathComponent:uploadId];
  301. if (![partInfos writeToFile:filePath atomically:YES])
  302. {
  303. NSError *error = [NSError errorWithDomain:OSSClientErrorDomain
  304. code:OSSClientErrorCodeFileCantWrite
  305. userInfo:@{OSSErrorMessageTOKEN: @"uploadId for this task can't be stored persistentially!"}];
  306. OSSLogDebug(@"[Error]: %@", error);
  307. return [OSSTask taskWithError:error];
  308. }
  309. return nil;
  310. }
  311. - (OSSTask *)checkPutObjectFileURL:(OSSPutObjectRequest *)request {
  312. NSError *error = nil;
  313. if (!request.uploadingFileURL || ![request.uploadingFileURL.path oss_isNotEmpty]) {
  314. error = [NSError errorWithDomain:OSSClientErrorDomain
  315. code:OSSClientErrorCodeInvalidArgument
  316. userInfo:@{OSSErrorMessageTOKEN: @"Please check your request's uploadingFileURL!"}];
  317. } else {
  318. NSFileManager *dfm = [NSFileManager defaultManager];
  319. NSDictionary *attributes = [dfm attributesOfItemAtPath:request.uploadingFileURL.path error:&error];
  320. unsigned long long fileSize = [attributes[NSFileSize] unsignedLongLongValue];
  321. if (!error && fileSize == 0) {
  322. error = [NSError errorWithDomain:OSSClientErrorDomain
  323. code:OSSClientErrorCodeInvalidArgument
  324. userInfo:@{OSSErrorMessageTOKEN: kClientErrorMessageForEmptyFile}];
  325. }
  326. }
  327. if (error) {
  328. return [OSSTask taskWithError:error];
  329. } else {
  330. return [OSSTask taskWithResult:nil];
  331. }
  332. }
  333. - (OSSTask *)checkFileSizeWithRequest:(OSSMultipartUploadRequest *)request {
  334. NSError *error = nil;
  335. if (!request.uploadingFileURL || ![request.uploadingFileURL.path oss_isNotEmpty]) {
  336. error = [NSError errorWithDomain:OSSClientErrorDomain
  337. code:OSSClientErrorCodeInvalidArgument
  338. userInfo:@{OSSErrorMessageTOKEN: @"Please check your request's uploadingFileURL!"}];
  339. }
  340. else
  341. {
  342. NSFileManager *dfm = [NSFileManager defaultManager];
  343. NSDictionary *attributes = [dfm attributesOfItemAtPath:request.uploadingFileURL.path error:&error];
  344. unsigned long long fileSize = [attributes[NSFileSize] unsignedLongLongValue];
  345. if (!error && fileSize == 0) {
  346. error = [NSError errorWithDomain:OSSClientErrorDomain
  347. code:OSSClientErrorCodeInvalidArgument
  348. userInfo:@{OSSErrorMessageTOKEN: kClientErrorMessageForEmptyFile}];
  349. }
  350. }
  351. if (error) {
  352. return [OSSTask taskWithError:error];
  353. } else {
  354. return nil;
  355. }
  356. }
  357. + (NSError *)cancelError{
  358. static NSError *error = nil;
  359. static dispatch_once_t onceToken;
  360. dispatch_once(&onceToken, ^{
  361. error = [NSError errorWithDomain:OSSClientErrorDomain
  362. code:OSSClientErrorCodeTaskCancelled
  363. userInfo:@{OSSErrorMessageTOKEN: kClientErrorMessageForCancelledTask}];
  364. });
  365. return error;
  366. }
  367. - (void)dealloc{
  368. [self.networking.session invalidateAndCancel];
  369. }
  370. @end
  371. @implementation OSSClient (Bucket)
  372. - (OSSTask *)createBucket:(OSSCreateBucketRequest *)request {
  373. OSSNetworkingRequestDelegate *requestDelegate = request.requestDelegate;
  374. NSMutableDictionary *headerParams = [NSMutableDictionary dictionary];
  375. [headerParams oss_setObject:request.xOssACL forKey:OSSHttpHeaderBucketACL];
  376. if (request.location) {
  377. requestDelegate.uploadingData = [OSSUtil constructHttpBodyForCreateBucketWithLocation:request.location];
  378. }
  379. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeCreateBucket];
  380. NSString *bodyString = [NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><CreateBucketConfiguration><StorageClass>%@</StorageClass></CreateBucketConfiguration>", request.storageClassAsString];
  381. requestDelegate.uploadingData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
  382. NSString *md5String = [OSSUtil base64Md5ForData:requestDelegate.uploadingData];
  383. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  384. neededMsg.endpoint = self.endpoint;
  385. neededMsg.httpMethod = OSSHTTPMethodPUT;
  386. neededMsg.bucketName = request.bucketName;
  387. neededMsg.headerParams = headerParams;
  388. neededMsg.contentMd5 = md5String;
  389. requestDelegate.allNeededMessage = neededMsg;
  390. requestDelegate.operType = OSSOperationTypeCreateBucket;
  391. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  392. }
  393. - (OSSTask *)deleteBucket:(OSSDeleteObjectRequest *)request {
  394. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  395. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeDeleteBucket];
  396. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  397. neededMsg.endpoint = self.endpoint;
  398. neededMsg.httpMethod = OSSHTTPMethodDELETE;
  399. neededMsg.bucketName = request.bucketName;
  400. requestDelegate.allNeededMessage = neededMsg;
  401. requestDelegate.operType = OSSOperationTypeDeleteBucket;
  402. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  403. }
  404. - (OSSTask *)getBucket:(OSSGetBucketRequest *)request {
  405. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  406. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeGetBucket];
  407. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  408. neededMsg.endpoint = self.endpoint;
  409. neededMsg.httpMethod = OSSHTTPMethodGET;
  410. neededMsg.bucketName = request.bucketName;
  411. neededMsg.params = request.requestParams;
  412. requestDelegate.allNeededMessage = neededMsg;
  413. requestDelegate.operType = OSSOperationTypeGetBucket;
  414. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  415. }
  416. - (OSSTask *)getBucketInfo:(OSSGetBucketInfoRequest *)request {
  417. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  418. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeGetBucketInfo];
  419. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  420. neededMsg.endpoint = self.endpoint;
  421. neededMsg.httpMethod = OSSHTTPMethodGET;
  422. neededMsg.bucketName = request.bucketName;
  423. neededMsg.params = request.requestParams;
  424. requestDelegate.allNeededMessage = neededMsg;
  425. requestDelegate.operType = OSSOperationTypeGetBucketInfo;
  426. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  427. }
  428. - (OSSTask *)getBucketACL:(OSSGetBucketACLRequest *)request {
  429. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  430. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeGetBucketACL];
  431. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  432. neededMsg.endpoint = self.endpoint;
  433. neededMsg.httpMethod = OSSHTTPMethodGET;
  434. neededMsg.bucketName = request.bucketName;
  435. neededMsg.params = request.requestParams;
  436. requestDelegate.allNeededMessage = neededMsg;
  437. requestDelegate.operType = OSSOperationTypeGetBucketACL;
  438. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  439. }
  440. @end
  441. @implementation OSSClient (Object)
  442. - (OSSTask *)headObject:(OSSHeadObjectRequest *)request {
  443. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  444. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeHeadObject];
  445. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  446. neededMsg.endpoint = self.endpoint;
  447. neededMsg.httpMethod = OSSHTTPMethodHEAD;
  448. neededMsg.bucketName = request.bucketName;
  449. neededMsg.objectKey = request.objectKey;
  450. requestDelegate.allNeededMessage = neededMsg;
  451. requestDelegate.operType = OSSOperationTypeHeadObject;
  452. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  453. }
  454. - (OSSTask *)getObject:(OSSGetObjectRequest *)request {
  455. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  456. NSString * rangeString = nil;
  457. if (request.range) {
  458. rangeString = [request.range toHeaderString];
  459. }
  460. if (request.downloadProgress) {
  461. requestDelegate.downloadProgress = request.downloadProgress;
  462. }
  463. if (request.onRecieveData) {
  464. requestDelegate.onRecieveData = request.onRecieveData;
  465. }
  466. NSMutableDictionary * params = [NSMutableDictionary dictionary];
  467. [params oss_setObject:request.xOssProcess forKey:OSSHttpQueryProcess];
  468. [self enableCRC64WithFlag:request.crcFlag requestDelegate:requestDelegate];
  469. OSSHttpResponseParser *responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeGetObject];
  470. responseParser.crc64Verifiable = requestDelegate.crc64Verifiable;
  471. requestDelegate.responseParser = responseParser;
  472. requestDelegate.responseParser.downloadingFileURL = request.downloadToFileURL;
  473. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  474. neededMsg.endpoint = self.endpoint;
  475. neededMsg.httpMethod = OSSHTTPMethodGET;
  476. neededMsg.bucketName = request.bucketName;
  477. neededMsg.objectKey = request.objectKey;
  478. neededMsg.range = rangeString;
  479. neededMsg.params = params;
  480. neededMsg.headerParams = request.headerFields;
  481. requestDelegate.allNeededMessage = neededMsg;
  482. requestDelegate.operType = OSSOperationTypeGetObject;
  483. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  484. }
  485. - (OSSTask *)getObjectACL:(OSSGetObjectACLRequest *)request
  486. {
  487. OSSNetworkingRequestDelegate *requestDelegate = request.requestDelegate;
  488. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeGetObjectACL];
  489. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  490. [params oss_setObject:@"" forKey:@"acl"];
  491. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  492. neededMsg.endpoint = self.endpoint;
  493. neededMsg.httpMethod = OSSHTTPMethodGET;
  494. neededMsg.bucketName = request.bucketName;
  495. neededMsg.objectKey = request.objectName;
  496. neededMsg.params = params;
  497. requestDelegate.allNeededMessage = neededMsg;
  498. requestDelegate.operType = OSSOperationTypeGetObjectACL;
  499. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  500. }
  501. - (OSSTask *)putObject:(OSSPutObjectRequest *)request
  502. {
  503. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  504. NSMutableDictionary * headerParams = [NSMutableDictionary dictionaryWithDictionary:request.objectMeta];
  505. [self enableCRC64WithFlag:request.crcFlag requestDelegate:requestDelegate];
  506. if (request.uploadingData) {
  507. requestDelegate.uploadingData = request.uploadingData;
  508. if (requestDelegate.crc64Verifiable)
  509. {
  510. NSMutableData *mutableData = [NSMutableData dataWithData:request.uploadingData];
  511. requestDelegate.contentCRC = [NSString stringWithFormat:@"%llu",[mutableData oss_crc64]];
  512. }
  513. }
  514. if (request.uploadingFileURL) {
  515. OSSTask *checkIfEmptyTask = [self checkPutObjectFileURL:request];
  516. if (checkIfEmptyTask.error) {
  517. return checkIfEmptyTask;
  518. }
  519. requestDelegate.uploadingFileURL = request.uploadingFileURL;
  520. }
  521. if (request.uploadProgress) {
  522. requestDelegate.uploadProgress = request.uploadProgress;
  523. }
  524. if (request.uploadRetryCallback) {
  525. requestDelegate.retryCallback = request.uploadRetryCallback;
  526. }
  527. [headerParams oss_setObject:[request.callbackParam base64JsonString] forKey:OSSHttpHeaderXOSSCallback];
  528. [headerParams oss_setObject:[request.callbackVar base64JsonString] forKey:OSSHttpHeaderXOSSCallbackVar];
  529. [headerParams oss_setObject:request.contentDisposition forKey:OSSHttpHeaderContentDisposition];
  530. [headerParams oss_setObject:request.contentEncoding forKey:OSSHttpHeaderContentEncoding];
  531. [headerParams oss_setObject:request.expires forKey:OSSHttpHeaderExpires];
  532. [headerParams oss_setObject:request.cacheControl forKey:OSSHttpHeaderCacheControl];
  533. OSSHttpResponseParser *responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypePutObject];
  534. responseParser.crc64Verifiable = requestDelegate.crc64Verifiable;
  535. requestDelegate.responseParser = responseParser;
  536. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  537. neededMsg.endpoint = self.endpoint;
  538. neededMsg.httpMethod = OSSHTTPMethodPUT;
  539. neededMsg.bucketName = request.bucketName;
  540. neededMsg.objectKey = request.objectKey;
  541. neededMsg.contentMd5 = request.contentMd5;
  542. neededMsg.contentType = request.contentType;
  543. neededMsg.headerParams = headerParams;
  544. neededMsg.contentSHA1 = request.contentSHA1;
  545. requestDelegate.allNeededMessage = neededMsg;
  546. requestDelegate.operType = OSSOperationTypePutObject;
  547. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  548. }
  549. - (OSSTask *)putObjectACL:(OSSPutObjectACLRequest *)request {
  550. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  551. if (request.uploadRetryCallback) {
  552. requestDelegate.retryCallback = request.uploadRetryCallback;
  553. }
  554. NSMutableDictionary * headerParams = [NSMutableDictionary dictionary];
  555. [headerParams oss_setObject:request.acl forKey:OSSHttpHeaderObjectACL];
  556. NSMutableDictionary * params = [NSMutableDictionary dictionary];
  557. [params oss_setObject:@"" forKey:@"acl"];
  558. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypePutObjectACL];
  559. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  560. neededMsg.endpoint = self.endpoint;
  561. neededMsg.httpMethod = OSSHTTPMethodPUT;
  562. neededMsg.bucketName = request.bucketName;
  563. neededMsg.objectKey = request.objectKey;
  564. neededMsg.params = params;
  565. neededMsg.headerParams = headerParams;
  566. requestDelegate.allNeededMessage = neededMsg;
  567. requestDelegate.operType = OSSOperationTypePutObjectACL;
  568. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  569. }
  570. - (OSSTask *)appendObject:(OSSAppendObjectRequest *)request
  571. {
  572. return [self appendObject:request withCrc64ecma:nil];
  573. }
  574. - (OSSTask *)appendObject:(OSSAppendObjectRequest *)request withCrc64ecma:(nullable NSString *)crc64ecma
  575. {
  576. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  577. requestDelegate.lastCRC = crc64ecma;
  578. [self enableCRC64WithFlag:request.crcFlag requestDelegate:requestDelegate];
  579. if (request.uploadingData)
  580. {
  581. requestDelegate.uploadingData = request.uploadingData;
  582. if (requestDelegate.crc64Verifiable)
  583. {
  584. NSMutableData *mutableData = [NSMutableData dataWithData:request.uploadingData];
  585. requestDelegate.contentCRC = [NSString stringWithFormat:@"%llu",[mutableData oss_crc64]];
  586. }
  587. }
  588. if (request.uploadingFileURL) {
  589. requestDelegate.uploadingFileURL = request.uploadingFileURL;
  590. }
  591. if (request.uploadProgress) {
  592. requestDelegate.uploadProgress = request.uploadProgress;
  593. }
  594. NSMutableDictionary * headerParams = [NSMutableDictionary dictionaryWithDictionary:request.objectMeta];
  595. [headerParams oss_setObject:request.contentDisposition forKey:OSSHttpHeaderContentDisposition];
  596. [headerParams oss_setObject:request.contentEncoding forKey:OSSHttpHeaderContentEncoding];
  597. [headerParams oss_setObject:request.expires forKey:OSSHttpHeaderExpires];
  598. [headerParams oss_setObject:request.cacheControl forKey:OSSHttpHeaderCacheControl];
  599. NSMutableDictionary* params = [NSMutableDictionary dictionary];
  600. [params oss_setObject:@"" forKey:@"append"];
  601. [params oss_setObject:[@(request.appendPosition) stringValue] forKey:@"position"];
  602. OSSHttpResponseParser *responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeAppendObject];
  603. responseParser.crc64Verifiable = requestDelegate.crc64Verifiable;
  604. requestDelegate.responseParser = responseParser;
  605. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  606. neededMsg.endpoint = self.endpoint;
  607. neededMsg.httpMethod = OSSHTTPMethodPOST;
  608. neededMsg.bucketName = request.bucketName;
  609. neededMsg.objectKey = request.objectKey;
  610. neededMsg.contentType = request.contentType;
  611. neededMsg.contentMd5 = request.contentMd5;
  612. neededMsg.headerParams = headerParams;
  613. neededMsg.params = params;
  614. neededMsg.contentSHA1 = request.contentSHA1;
  615. requestDelegate.allNeededMessage = neededMsg;
  616. requestDelegate.operType = OSSOperationTypeAppendObject;
  617. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  618. }
  619. - (OSSTask *)deleteObject:(OSSDeleteObjectRequest *)request {
  620. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  621. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypePutObject];
  622. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  623. neededMsg.endpoint = self.endpoint;
  624. neededMsg.httpMethod = OSSHTTPMethodDELETE;
  625. neededMsg.bucketName = request.bucketName;
  626. neededMsg.objectKey = request.objectKey;
  627. requestDelegate.allNeededMessage = neededMsg;
  628. requestDelegate.operType = OSSOperationTypeDeleteObject;
  629. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  630. }
  631. - (OSSTask *)deleteMultipleObjects:(OSSDeleteMultipleObjectsRequest *)request
  632. {
  633. if ([request.keys count] == 0) {
  634. NSError *error = [NSError errorWithDomain:OSSClientErrorDomain
  635. code:OSSClientErrorCodeInvalidArgument
  636. userInfo:@{OSSErrorMessageTOKEN: @"keys should not be empty"}];
  637. return [OSSTask taskWithError:error];
  638. }
  639. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  640. requestDelegate.uploadingData = [OSSUtil constructHttpBodyForDeleteMultipleObjects:request.keys quiet:request.quiet];
  641. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeDeleteMultipleObjects];
  642. NSString *md5String = [OSSUtil base64Md5ForData:requestDelegate.uploadingData];
  643. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  644. [params oss_setObject:@"" forKey:@"delete"];
  645. [params oss_setObject:request.encodingType forKey:@"encoding-type"];
  646. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  647. neededMsg.endpoint = self.endpoint;
  648. neededMsg.httpMethod = OSSHTTPMethodPOST;
  649. neededMsg.bucketName = request.bucketName;
  650. neededMsg.contentMd5 = md5String;
  651. neededMsg.params = params;
  652. requestDelegate.allNeededMessage = neededMsg;
  653. requestDelegate.operType = OSSOperationTypeDeleteMultipleObjects;
  654. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  655. }
  656. - (OSSTask *)copyObject:(OSSCopyObjectRequest *)request {
  657. NSString *copySourceHeader = nil;
  658. if (request.sourceCopyFrom) {
  659. copySourceHeader = request.sourceCopyFrom;
  660. } else {
  661. if (![request.sourceBucketName oss_isNotEmpty]) {
  662. NSError *error = [NSError errorWithDomain:OSSClientErrorDomain code:OSSClientErrorCodeInvalidArgument userInfo:@{NSLocalizedDescriptionKey: @"sourceBucketName should not be empty!"}];
  663. return [OSSTask taskWithError:error];
  664. }
  665. if (![request.sourceObjectKey oss_isNotEmpty]) {
  666. NSError *error = [NSError errorWithDomain:OSSClientErrorDomain code:OSSClientErrorCodeInvalidArgument userInfo:@{NSLocalizedDescriptionKey: @"sourceObjectKey should not be empty!"}];
  667. return [OSSTask taskWithError:error];
  668. }
  669. copySourceHeader = [NSString stringWithFormat:@"/%@/%@",request.bucketName, request.sourceObjectKey.oss_urlEncodedString];
  670. }
  671. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  672. NSMutableDictionary * headerParams = [NSMutableDictionary dictionaryWithDictionary:request.objectMeta];
  673. [headerParams oss_setObject:copySourceHeader forKey:OSSHttpHeaderCopySource];
  674. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeCopyObject];
  675. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  676. neededMsg.endpoint = self.endpoint;
  677. neededMsg.httpMethod = OSSHTTPMethodPUT;
  678. neededMsg.bucketName = request.bucketName;
  679. neededMsg.objectKey = request.objectKey;
  680. neededMsg.contentType = request.contentType;
  681. neededMsg.contentMd5 = request.contentMd5;
  682. neededMsg.headerParams = headerParams;
  683. neededMsg.contentSHA1 = request.contentSHA1;
  684. requestDelegate.allNeededMessage = neededMsg;
  685. requestDelegate.operType = OSSOperationTypeCopyObject;
  686. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  687. }
  688. - (OSSTask *)putSymlink:(OSSPutSymlinkRequest *)request {
  689. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  690. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypePutSymlink];
  691. NSMutableDictionary *headerFields = [NSMutableDictionary dictionary];
  692. [headerFields oss_setObject:[request.targetObjectName oss_urlEncodedString] forKey:OSSHttpHeaderSymlinkTarget];
  693. if (request.objectMeta) {
  694. [headerFields addEntriesFromDictionary:request.objectMeta];
  695. }
  696. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  697. neededMsg.endpoint = self.endpoint;
  698. neededMsg.httpMethod = OSSHTTPMethodPUT;
  699. neededMsg.bucketName = request.bucketName;
  700. neededMsg.objectKey = request.objectKey;
  701. neededMsg.params = request.requestParams;
  702. neededMsg.headerParams = headerFields;
  703. requestDelegate.allNeededMessage = neededMsg;
  704. requestDelegate.operType = OSSOperationTypePutSymlink;
  705. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  706. }
  707. - (OSSTask *)getSymlink:(OSSGetSymlinkRequest *)request {
  708. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  709. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeGetSymlink];
  710. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  711. neededMsg.endpoint = self.endpoint;
  712. neededMsg.httpMethod = OSSHTTPMethodGET;
  713. neededMsg.bucketName = request.bucketName;
  714. neededMsg.objectKey = request.objectKey;
  715. neededMsg.params = request.requestParams;
  716. requestDelegate.allNeededMessage = neededMsg;
  717. requestDelegate.operType = OSSOperationTypeGetSymlink;
  718. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  719. }
  720. - (OSSTask *)restoreObject:(OSSRestoreObjectRequest *)request {
  721. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  722. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeRestoreObject];
  723. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  724. neededMsg.endpoint = self.endpoint;
  725. neededMsg.httpMethod = OSSHTTPMethodPOST;
  726. neededMsg.bucketName = request.bucketName;
  727. neededMsg.objectKey = request.objectKey;
  728. neededMsg.params = request.requestParams;
  729. requestDelegate.allNeededMessage = neededMsg;
  730. requestDelegate.operType = OSSOperationTypeRestoreObject;
  731. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  732. }
  733. - (OSSTask *)getObjectTagging:(OSSGetObjectTaggingRequest *)request {
  734. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  735. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeGetObjectTagging];
  736. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  737. neededMsg.endpoint = self.endpoint;
  738. neededMsg.httpMethod = OSSHTTPMethodGET;
  739. neededMsg.bucketName = request.bucketName;
  740. neededMsg.objectKey = request.objectKey;
  741. neededMsg.params = request.requestParams;
  742. requestDelegate.allNeededMessage = neededMsg;
  743. requestDelegate.operType = OSSOperationTypeGetObjectTagging;
  744. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  745. }
  746. - (OSSTask *)putObjectTagging:(OSSPutObjectTaggingRequest *)request {
  747. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  748. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypePutObjectTagging];
  749. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  750. neededMsg.endpoint = self.endpoint;
  751. neededMsg.httpMethod = OSSHTTPMethodPUT;
  752. neededMsg.bucketName = request.bucketName;
  753. neededMsg.objectKey = request.objectKey;
  754. neededMsg.params = request.requestParams;
  755. requestDelegate.allNeededMessage = neededMsg;
  756. NSString *xmlString = [[request entityToDictionary] oss_XMLString];
  757. requestDelegate.uploadingData = [xmlString dataUsingEncoding:NSUTF8StringEncoding];
  758. requestDelegate.operType = OSSOperationTypePutObjectTagging;
  759. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  760. }
  761. - (OSSTask *)deleteObjectTagging:(OSSDeleteObjectTaggingRequest *)request {
  762. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  763. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeDeleteObjectTagging];
  764. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  765. neededMsg.endpoint = self.endpoint;
  766. neededMsg.httpMethod = OSSHTTPMethodDELETE;
  767. neededMsg.bucketName = request.bucketName;
  768. neededMsg.objectKey = request.objectKey;
  769. neededMsg.params = request.requestParams;
  770. requestDelegate.allNeededMessage = neededMsg;
  771. requestDelegate.operType = OSSOperationTypeDeleteObjectTagging;
  772. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  773. }
  774. @end
  775. @implementation OSSClient (MultipartUpload)
  776. - (OSSTask *)listMultipartUploads:(OSSListMultipartUploadsRequest *)request {
  777. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  778. NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:[request requestParams]];
  779. [params oss_setObject:@"" forKey:@"uploads"];
  780. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeListMultipartUploads];
  781. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  782. neededMsg.endpoint = self.endpoint;
  783. neededMsg.httpMethod = OSSHTTPMethodGET;
  784. neededMsg.bucketName = request.bucketName;
  785. neededMsg.params = params;
  786. requestDelegate.allNeededMessage = neededMsg;
  787. requestDelegate.operType = OSSOperationTypeListMultipartUploads;
  788. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  789. }
  790. - (OSSTask *)multipartUploadInit:(OSSInitMultipartUploadRequest *)request {
  791. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  792. NSMutableDictionary * headerParams = [NSMutableDictionary dictionaryWithDictionary:request.objectMeta];
  793. [headerParams oss_setObject:request.contentDisposition forKey:OSSHttpHeaderContentDisposition];
  794. [headerParams oss_setObject:request.contentEncoding forKey:OSSHttpHeaderContentEncoding];
  795. [headerParams oss_setObject:request.expires forKey:OSSHttpHeaderExpires];
  796. [headerParams oss_setObject:request.cacheControl forKey:OSSHttpHeaderCacheControl];
  797. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  798. [params oss_setObject:@"" forKey:@"uploads"];
  799. if (request.sequential) {
  800. [params oss_setObject:@"" forKey:@"sequential"];
  801. }
  802. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeInitMultipartUpload];
  803. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  804. neededMsg.endpoint = self.endpoint;
  805. neededMsg.httpMethod = OSSHTTPMethodPOST;
  806. neededMsg.bucketName = request.bucketName;
  807. neededMsg.objectKey = request.objectKey;
  808. neededMsg.contentType = request.contentType;
  809. neededMsg.params = params;
  810. neededMsg.headerParams = headerParams;
  811. requestDelegate.allNeededMessage = neededMsg;
  812. requestDelegate.operType = OSSOperationTypeInitMultipartUpload;
  813. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  814. }
  815. - (OSSTask *)uploadPart:(OSSUploadPartRequest *)request {
  816. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  817. NSMutableDictionary * params = [NSMutableDictionary dictionary];
  818. [params oss_setObject:[@(request.partNumber) stringValue] forKey:@"partNumber"];
  819. [params oss_setObject:request.uploadId forKey:@"uploadId"];
  820. [self enableCRC64WithFlag:request.crcFlag requestDelegate:requestDelegate];
  821. if (request.uploadPartData) {
  822. requestDelegate.uploadingData = request.uploadPartData;
  823. if (requestDelegate.crc64Verifiable)
  824. {
  825. NSMutableData *mutableData = [NSMutableData dataWithData:request.uploadPartData];
  826. requestDelegate.contentCRC = [NSString stringWithFormat:@"%llu",[mutableData oss_crc64]];
  827. }
  828. }
  829. if (request.uploadPartFileURL) {
  830. requestDelegate.uploadingFileURL = request.uploadPartFileURL;
  831. }
  832. if (request.uploadPartProgress) {
  833. requestDelegate.uploadProgress = request.uploadPartProgress;
  834. }
  835. OSSHttpResponseParser *responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeUploadPart];
  836. responseParser.crc64Verifiable = requestDelegate.crc64Verifiable;
  837. requestDelegate.responseParser = responseParser;
  838. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  839. neededMsg.endpoint = self.endpoint;
  840. neededMsg.httpMethod = OSSHTTPMethodPUT;
  841. neededMsg.bucketName = request.bucketName;
  842. neededMsg.objectKey = request.objectkey;
  843. neededMsg.contentMd5 = request.contentMd5;
  844. neededMsg.params = params;
  845. neededMsg.contentSHA1 = request.contentSHA1;
  846. requestDelegate.allNeededMessage = neededMsg;
  847. requestDelegate.operType = OSSOperationTypeUploadPart;
  848. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  849. }
  850. - (OSSTask *)completeMultipartUpload:(OSSCompleteMultipartUploadRequest *)request
  851. {
  852. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  853. NSMutableDictionary * headerParams = [NSMutableDictionary dictionary];
  854. if (request.partInfos) {
  855. requestDelegate.uploadingData = [OSSUtil constructHttpBodyFromPartInfos:request.partInfos];
  856. }
  857. [headerParams oss_setObject:[request.callbackParam base64JsonString] forKey:OSSHttpHeaderXOSSCallback];
  858. [headerParams oss_setObject:[request.callbackVar base64JsonString] forKey:OSSHttpHeaderXOSSCallbackVar];
  859. if (request.completeMetaHeader) {
  860. [headerParams addEntriesFromDictionary:request.completeMetaHeader];
  861. }
  862. NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:request.uploadId, @"uploadId", nil];
  863. OSSHttpResponseParser *responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeCompleteMultipartUpload];
  864. responseParser.crc64Verifiable = requestDelegate.crc64Verifiable;
  865. requestDelegate.responseParser = responseParser;
  866. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  867. neededMsg.endpoint = self.endpoint;
  868. neededMsg.httpMethod = OSSHTTPMethodPOST;
  869. neededMsg.bucketName = request.bucketName;
  870. neededMsg.objectKey = request.objectKey;
  871. neededMsg.contentMd5 = request.contentMd5;
  872. neededMsg.headerParams = headerParams;
  873. neededMsg.params = params;
  874. neededMsg.contentSHA1 = request.contentSHA1;
  875. requestDelegate.allNeededMessage = neededMsg;
  876. requestDelegate.operType = OSSOperationTypeCompleteMultipartUpload;
  877. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  878. }
  879. - (OSSTask *)listParts:(OSSListPartsRequest *)request {
  880. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  881. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  882. [params oss_setObject: request.uploadId forKey: @"uploadId"];
  883. [params oss_setObject: [NSString stringWithFormat:@"%d",request.partNumberMarker] forKey: @"part-number-marker"];
  884. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeListMultipart];
  885. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  886. neededMsg.endpoint = self.endpoint;
  887. neededMsg.httpMethod = OSSHTTPMethodGET;
  888. neededMsg.bucketName = request.bucketName;
  889. neededMsg.objectKey = request.objectKey;
  890. neededMsg.params = params;
  891. requestDelegate.allNeededMessage = neededMsg;
  892. requestDelegate.operType = OSSOperationTypeListMultipart;
  893. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  894. }
  895. - (OSSTask *)abortMultipartUpload:(OSSAbortMultipartUploadRequest *)request {
  896. OSSNetworkingRequestDelegate * requestDelegate = request.requestDelegate;
  897. NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:request.uploadId, @"uploadId", nil];
  898. requestDelegate.responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeAbortMultipartUpload];
  899. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  900. neededMsg.endpoint = self.endpoint;
  901. neededMsg.httpMethod = OSSHTTPMethodDELETE;
  902. neededMsg.bucketName = request.bucketName;
  903. neededMsg.objectKey = request.objectKey;
  904. neededMsg.params = params;
  905. requestDelegate.allNeededMessage = neededMsg;
  906. requestDelegate.operType = OSSOperationTypeAbortMultipartUpload;
  907. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  908. }
  909. - (OSSTask *)abortResumableMultipartUpload:(OSSResumableUploadRequest *)request
  910. {
  911. return [self abortMultipartUpload:request sequential:NO resumable:YES];
  912. }
  913. - (OSSTask *)abortMultipartUpload:(OSSMultipartUploadRequest *)request sequential:(BOOL)sequential resumable:(BOOL)resumable {
  914. OSSTask *errorTask = nil;
  915. if(resumable) {
  916. OSSResumableUploadRequest *resumableRequest = (OSSResumableUploadRequest *)request;
  917. NSString *nameInfoString = [NSString stringWithFormat:@"%@%@%@%lu",request.md5String, resumableRequest.bucketName, resumableRequest.objectKey, (unsigned long)resumableRequest.partSize];
  918. if (sequential) {
  919. nameInfoString = [nameInfoString stringByAppendingString:kClientRecordNameWithSequentialSuffix];
  920. }
  921. if (request.crcFlag == OSSRequestCRCOpen) {
  922. nameInfoString = [nameInfoString stringByAppendingString:kClientRecordNameWithCRC64Suffix];
  923. }
  924. NSData *data = [nameInfoString dataUsingEncoding:NSUTF8StringEncoding];
  925. NSError *error = nil;
  926. NSString *recordFileName = [OSSUtil dataMD5String:data error:&error];
  927. if (error) {
  928. return [OSSTask taskWithError:error];
  929. }
  930. NSString *recordFilePath = [NSString stringWithFormat:@"%@/%@",resumableRequest.recordDirectoryPath,recordFileName];
  931. NSFileManager *fileManager = [NSFileManager defaultManager];
  932. NSString *partInfosFilePath = [[[NSString oss_documentDirectory] stringByAppendingPathComponent:kClientRecordNameWithCommonPrefix] stringByAppendingPathComponent:resumableRequest.uploadId];
  933. if([fileManager fileExistsAtPath:recordFilePath])
  934. {
  935. NSError *error;
  936. if (![fileManager removeItemAtPath:recordFilePath error:&error])
  937. {
  938. OSSLogDebug(@"[OSSSDKError]: %@", error);
  939. }
  940. }
  941. if ([fileManager fileExistsAtPath:partInfosFilePath]) {
  942. NSError *error;
  943. if (![fileManager removeItemAtPath:partInfosFilePath error:&error])
  944. {
  945. OSSLogDebug(@"[OSSSDKError]: %@", error);
  946. }
  947. }
  948. OSSAbortMultipartUploadRequest * abort = [OSSAbortMultipartUploadRequest new];
  949. abort.bucketName = request.bucketName;
  950. abort.objectKey = request.objectKey;
  951. if (request.uploadId) {
  952. abort.uploadId = request.uploadId;
  953. } else {
  954. abort.uploadId = [[NSString alloc] initWithData:[[NSFileHandle fileHandleForReadingAtPath:recordFilePath] readDataToEndOfFile] encoding:NSUTF8StringEncoding];
  955. }
  956. errorTask = [self abortMultipartUpload:abort];
  957. }else
  958. {
  959. OSSAbortMultipartUploadRequest * abort = [OSSAbortMultipartUploadRequest new];
  960. abort.bucketName = request.bucketName;
  961. abort.objectKey = request.objectKey;
  962. abort.uploadId = request.uploadId;
  963. errorTask = [self abortMultipartUpload:abort];
  964. }
  965. return errorTask;
  966. }
  967. - (OSSTask *)multipartUpload:(OSSMultipartUploadRequest *)request {
  968. return [self multipartUpload: request resumable: NO sequential: NO];
  969. }
  970. - (OSSTask *)processCompleteMultipartUpload:(OSSMultipartUploadRequest *)request partInfos:(NSArray<OSSPartInfo *> *)partInfos clientCrc64:(uint64_t)clientCrc64 recordFilePath:(NSString *)recordFilePath localPartInfosPath:(NSString *)localPartInfosPath
  971. {
  972. OSSCompleteMultipartUploadRequest * complete = [OSSCompleteMultipartUploadRequest new];
  973. complete.bucketName = request.bucketName;
  974. complete.objectKey = request.objectKey;
  975. complete.uploadId = request.uploadId;
  976. complete.partInfos = partInfos;
  977. complete.crcFlag = request.crcFlag;
  978. complete.contentSHA1 = request.contentSHA1;
  979. if (request.completeMetaHeader != nil) {
  980. complete.completeMetaHeader = request.completeMetaHeader;
  981. }
  982. if (request.callbackParam != nil) {
  983. complete.callbackParam = request.callbackParam;
  984. }
  985. if (request.callbackVar != nil) {
  986. complete.callbackVar = request.callbackVar;
  987. }
  988. OSSTask * completeTask = [self completeMultipartUpload:complete];
  989. [completeTask waitUntilFinished];
  990. if (completeTask.error) {
  991. OSSLogVerbose(@"completeTask.error %@: ",completeTask.error);
  992. return completeTask;
  993. } else
  994. {
  995. if(recordFilePath && [[NSFileManager defaultManager] fileExistsAtPath:recordFilePath])
  996. {
  997. NSError *deleteError;
  998. if (![[NSFileManager defaultManager] removeItemAtPath:recordFilePath error:&deleteError])
  999. {
  1000. OSSLogError(@"delete localUploadIdPath failed!Error: %@",deleteError);
  1001. }
  1002. }
  1003. if (localPartInfosPath && [[NSFileManager defaultManager] fileExistsAtPath:localPartInfosPath])
  1004. {
  1005. NSError *deleteError;
  1006. if (![[NSFileManager defaultManager] removeItemAtPath:localPartInfosPath error:&deleteError])
  1007. {
  1008. OSSLogError(@"delete localPartInfosPath failed!Error: %@",deleteError);
  1009. }
  1010. }
  1011. OSSCompleteMultipartUploadResult * completeResult = completeTask.result;
  1012. if (complete.crcFlag == OSSRequestCRCOpen && completeResult.remoteCRC64ecma)
  1013. {
  1014. uint64_t remote_crc64 = 0;
  1015. NSScanner *scanner = [NSScanner scannerWithString:completeResult.remoteCRC64ecma];
  1016. if ([scanner scanUnsignedLongLong:&remote_crc64])
  1017. {
  1018. OSSLogVerbose(@"resumableUpload local_crc64 %llu",clientCrc64);
  1019. OSSLogVerbose(@"resumableUpload remote_crc64 %llu", remote_crc64);
  1020. if (remote_crc64 != clientCrc64)
  1021. {
  1022. NSString *errorMessage = [NSString stringWithFormat:@"local_crc64(%llu) is not equal to remote_crc64(%llu)!",clientCrc64,remote_crc64];
  1023. NSError *error = [NSError errorWithDomain:OSSClientErrorDomain
  1024. code:OSSClientErrorCodeInvalidCRC
  1025. userInfo:@{OSSErrorMessageTOKEN:errorMessage}];
  1026. return [OSSTask taskWithError:error];
  1027. }
  1028. }
  1029. }
  1030. OSSResumableUploadResult * result = [OSSResumableUploadResult new];
  1031. result.requestId = completeResult.requestId;
  1032. result.httpResponseCode = completeResult.httpResponseCode;
  1033. result.httpResponseHeaderFields = completeResult.httpResponseHeaderFields;
  1034. result.serverReturnJsonString = completeResult.serverReturnJsonString;
  1035. result.remoteCRC64ecma = completeResult.remoteCRC64ecma;
  1036. return [OSSTask taskWithResult:result];
  1037. }
  1038. }
  1039. - (OSSTask *)resumableUpload:(OSSResumableUploadRequest *)request
  1040. {
  1041. return [self multipartUpload: request resumable: YES sequential: NO];
  1042. }
  1043. - (OSSTask *)processListPartsWithObjectKey:(nonnull NSString *)objectKey bucket:(nonnull NSString *)bucket uploadId:(NSString * _Nonnull *)uploadId uploadedParts:(nonnull NSMutableArray *)uploadedParts uploadedLength:(NSUInteger *)uploadedLength totalSize:(unsigned long long)totalSize partSize:(NSUInteger)partSize
  1044. {
  1045. BOOL isTruncated = NO;
  1046. int nextPartNumberMarker = 0;
  1047. NSUInteger bUploadedLength = 0;
  1048. do {
  1049. OSSListPartsRequest * listParts = [OSSListPartsRequest new];
  1050. listParts.bucketName = bucket;
  1051. listParts.objectKey = objectKey;
  1052. listParts.uploadId = *uploadId;
  1053. listParts.partNumberMarker = nextPartNumberMarker;
  1054. OSSTask * listPartsTask = [self listParts:listParts];
  1055. [listPartsTask waitUntilFinished];
  1056. if (listPartsTask.error)
  1057. {
  1058. isTruncated = NO;
  1059. [uploadedParts removeAllObjects];
  1060. if ([listPartsTask.error.domain isEqualToString: OSSServerErrorDomain] && labs(listPartsTask.error.code) == 404)
  1061. {
  1062. OSSLogVerbose(@"local record existes but the remote record is deleted");
  1063. *uploadId = nil;
  1064. } else
  1065. {
  1066. return listPartsTask;
  1067. }
  1068. }
  1069. else
  1070. {
  1071. OSSListPartsResult *res = listPartsTask.result;
  1072. isTruncated = res.isTruncated;
  1073. nextPartNumberMarker = res.nextPartNumberMarker;
  1074. OSSLogVerbose(@"resumableUpload listpart ok");
  1075. if (res.parts.count > 0) {
  1076. for (NSDictionary *part in res.parts) {
  1077. unsigned long long iPartSize = 0;
  1078. NSString *partSizeString = [part objectForKey:OSSSizeXMLTOKEN];
  1079. NSScanner *scanner = [NSScanner scannerWithString:partSizeString];
  1080. [scanner scanUnsignedLongLong:&iPartSize];
  1081. if (partSize == iPartSize) {
  1082. [uploadedParts addObject:part];
  1083. bUploadedLength += iPartSize;
  1084. }
  1085. }
  1086. }
  1087. }
  1088. } while (isTruncated);
  1089. *uploadedLength = bUploadedLength;
  1090. if (totalSize < bUploadedLength)
  1091. {
  1092. NSError *error = [NSError errorWithDomain:OSSClientErrorDomain
  1093. code:OSSClientErrorCodeCannotResumeUpload
  1094. userInfo:@{OSSErrorMessageTOKEN: @"The uploading file is inconsistent with before"}];
  1095. return [OSSTask taskWithError: error];
  1096. }
  1097. return nil;
  1098. }
  1099. - (OSSTask *)processResumableInitMultipartUpload:(OSSInitMultipartUploadRequest *)request recordFilePath:(NSString *)recordFilePath
  1100. {
  1101. OSSTask *task = [self multipartUploadInit:request];
  1102. [task waitUntilFinished];
  1103. if(task.result && [recordFilePath oss_isNotEmpty])
  1104. {
  1105. OSSInitMultipartUploadResult *result = task.result;
  1106. if (![result.uploadId oss_isNotEmpty])
  1107. {
  1108. NSString *errorMessage = [NSString stringWithFormat:@"Can not get uploadId!"];
  1109. NSError *error = [NSError errorWithDomain:OSSServerErrorDomain
  1110. code:OSSClientErrorCodeNilUploadid userInfo:@{OSSErrorMessageTOKEN: errorMessage}];
  1111. return [OSSTask taskWithError:error];
  1112. }
  1113. NSFileManager *defaultFM = [NSFileManager defaultManager];
  1114. if (![defaultFM fileExistsAtPath:recordFilePath])
  1115. {
  1116. if (![defaultFM createFileAtPath:recordFilePath contents:nil attributes:nil]) {
  1117. NSError *error = [NSError errorWithDomain:OSSClientErrorDomain
  1118. code:OSSClientErrorCodeFileCantWrite
  1119. userInfo:@{OSSErrorMessageTOKEN: @"uploadId for this task can't be stored persistentially!"}];
  1120. OSSLogDebug(@"[Error]: %@", error);
  1121. return [OSSTask taskWithError:error];
  1122. }
  1123. }
  1124. NSFileHandle * write = [NSFileHandle fileHandleForWritingAtPath:recordFilePath];
  1125. [write writeData:[result.uploadId dataUsingEncoding:NSUTF8StringEncoding]];
  1126. [write closeFile];
  1127. }
  1128. return task;
  1129. }
  1130. - (OSSTask *)upload:(OSSMultipartUploadRequest *)request
  1131. uploadIndex:(NSMutableArray *)alreadyUploadIndex
  1132. uploadPart:(NSMutableArray *)alreadyUploadPart
  1133. count:(NSUInteger)partCout
  1134. uploadedLength:(NSUInteger *)uploadedLength
  1135. fileSize:(unsigned long long)uploadFileSize
  1136. {
  1137. uint32_t threadNum = request.threadNum > 0 ? request.threadNum : OSSDefaultThreadNum;
  1138. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  1139. [queue setMaxConcurrentOperationCount:threadNum];
  1140. NSObject *localLock = [[NSObject alloc] init];
  1141. OSSRequestCRCFlag crcFlag = request.crcFlag;
  1142. __block OSSTask *errorTask;
  1143. __block NSMutableDictionary *localPartInfos = nil;
  1144. if (crcFlag == OSSRequestCRCOpen) {
  1145. localPartInfos = [self localPartInfosDictoryWithUploadId:request.uploadId];
  1146. }
  1147. if (!localPartInfos) {
  1148. localPartInfos = [NSMutableDictionary dictionary];
  1149. }
  1150. NSError *readError;
  1151. NSFileHandle *fileHande = [NSFileHandle fileHandleForReadingFromURL:request.uploadingFileURL error:&readError];
  1152. if (readError) {
  1153. return [OSSTask taskWithError: readError];
  1154. }
  1155. NSData * uploadPartData;
  1156. NSInteger realPartLength = request.partSize;
  1157. __block BOOL hasError = NO;
  1158. for (NSUInteger idx = 1; idx <= partCout; idx++)
  1159. {
  1160. if (request.isCancelled)
  1161. {
  1162. [queue cancelAllOperations];
  1163. break;
  1164. }
  1165. if ([alreadyUploadIndex containsObject:@(idx)])
  1166. {
  1167. continue;
  1168. }
  1169. // while operationCount >= threadNum,the loop will stay here
  1170. while (queue.operationCount >= threadNum) {
  1171. [NSThread sleepForTimeInterval: 0.15f];
  1172. }
  1173. if (idx == partCout) {
  1174. #pragma clang diagnostic push
  1175. #pragma clang diagnostic ignored "-Wshorten-64-to-32"
  1176. realPartLength = uploadFileSize - request.partSize * (idx - 1);
  1177. #pragma clang diagnostic pop
  1178. }
  1179. @autoreleasepool
  1180. {
  1181. if (@available(iOS 13.0, *)) {
  1182. NSError *error = nil;
  1183. [fileHande seekToOffset:request.partSize * (idx - 1) error:&error];
  1184. if (error) {
  1185. hasError = YES;
  1186. errorTask = [OSSTask taskWithError:[NSError errorWithDomain:OSSClientErrorDomain
  1187. code:OSSClientErrorCodeFileCantRead
  1188. userInfo:[error userInfo]]];
  1189. break;
  1190. }
  1191. error = nil;
  1192. uploadPartData = [fileHande readDataUpToLength:realPartLength error:&error];
  1193. if (error) {
  1194. hasError = YES;
  1195. errorTask = [OSSTask taskWithError:[NSError errorWithDomain:OSSClientErrorDomain
  1196. code:OSSClientErrorCodeFileCantRead
  1197. userInfo:[error userInfo]]];
  1198. break;
  1199. }
  1200. } else {
  1201. [fileHande seekToFileOffset: request.partSize * (idx - 1)];
  1202. uploadPartData = [fileHande readDataOfLength:realPartLength];
  1203. }
  1204. NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
  1205. OSSTask *uploadPartErrorTask = nil;
  1206. [self executePartUpload:request
  1207. totalBytesExpectedToSend:uploadFileSize
  1208. totalBytesSent:uploadedLength
  1209. index:idx
  1210. partData:uploadPartData
  1211. alreadyUploadPart:alreadyUploadPart
  1212. localParts:localPartInfos
  1213. errorTask:&uploadPartErrorTask];
  1214. if (uploadPartErrorTask != nil) {
  1215. @synchronized(localLock) {
  1216. if (!hasError) {
  1217. hasError = YES;
  1218. errorTask = uploadPartErrorTask;
  1219. }
  1220. }
  1221. uploadPartErrorTask = nil;
  1222. }
  1223. }];
  1224. [queue addOperation:operation];
  1225. }
  1226. }
  1227. [fileHande closeFile];
  1228. [queue waitUntilAllOperationsAreFinished];
  1229. localLock = nil;
  1230. if (!errorTask && request.isCancelled) {
  1231. errorTask = [OSSTask taskWithError:[OSSClient cancelError]];
  1232. }
  1233. return errorTask;
  1234. }
  1235. - (void)executePartUpload:(OSSMultipartUploadRequest *)request totalBytesExpectedToSend:(unsigned long long)totalBytesExpectedToSend totalBytesSent:(NSUInteger *)totalBytesSent index:(NSUInteger)idx partData:(NSData *)partData alreadyUploadPart:(NSMutableArray *)uploadedParts localParts:(NSMutableDictionary *)localParts errorTask:(OSSTask **)errorTask
  1236. {
  1237. NSUInteger bytesSent = partData.length;
  1238. OSSUploadPartRequest * uploadPart = [OSSUploadPartRequest new];
  1239. uploadPart.bucketName = request.bucketName;
  1240. uploadPart.objectkey = request.objectKey;
  1241. #pragma clang diagnostic push
  1242. #pragma clang diagnostic ignored "-Wshorten-64-to-32"
  1243. uploadPart.partNumber = idx;
  1244. #pragma clang diagnostic pop
  1245. uploadPart.uploadId = request.uploadId;
  1246. uploadPart.uploadPartData = partData;
  1247. uploadPart.contentMd5 = [OSSUtil base64Md5ForData:partData];
  1248. uploadPart.crcFlag = request.crcFlag;
  1249. OSSTask * uploadPartTask = [self uploadPart:uploadPart];
  1250. [uploadPartTask waitUntilFinished];
  1251. if (uploadPartTask.error) {
  1252. if (labs(uploadPartTask.error.code) != 409) {
  1253. *errorTask = uploadPartTask;
  1254. }
  1255. } else {
  1256. OSSUploadPartResult * result = uploadPartTask.result;
  1257. OSSPartInfo * partInfo = [OSSPartInfo new];
  1258. #pragma clang diagnostic push
  1259. #pragma clang diagnostic ignored "-Wshorten-64-to-32"
  1260. partInfo.partNum = idx;
  1261. #pragma clang diagnostic pop
  1262. partInfo.eTag = result.eTag;
  1263. partInfo.size = bytesSent;
  1264. uint64_t crc64OfPart;
  1265. @try {
  1266. NSScanner *scanner = [NSScanner scannerWithString:result.remoteCRC64ecma];
  1267. [scanner scanUnsignedLongLong:&crc64OfPart];
  1268. partInfo.crc64 = crc64OfPart;
  1269. } @catch (NSException *exception) {
  1270. OSSLogError(@"multipart upload error with nil remote crc64!");
  1271. }
  1272. @synchronized(lock){
  1273. [uploadedParts addObject:partInfo];
  1274. if (request.crcFlag == OSSRequestCRCOpen)
  1275. {
  1276. [self processForLocalPartInfos:localParts
  1277. partInfo:partInfo
  1278. uploadId:request.uploadId];
  1279. [self persistencePartInfos:localParts
  1280. withUploadId:request.uploadId];
  1281. }
  1282. *totalBytesSent += bytesSent;
  1283. if (request.uploadProgress)
  1284. {
  1285. request.uploadProgress(bytesSent, *totalBytesSent, totalBytesExpectedToSend);
  1286. }
  1287. }
  1288. }
  1289. }
  1290. - (void)processForLocalPartInfos:(NSMutableDictionary *)localPartInfoDict partInfo:(OSSPartInfo *)partInfo uploadId:(NSString *)uploadId
  1291. {
  1292. NSDictionary *partInfoDict = [partInfo entityToDictionary];
  1293. NSString *keyString = [NSString stringWithFormat:@"%i",partInfo.partNum];
  1294. [localPartInfoDict oss_setObject:partInfoDict forKey:keyString];
  1295. }
  1296. - (OSSTask *)sequentialMultipartUpload:(OSSResumableUploadRequest *)request
  1297. {
  1298. return [self multipartUpload:request resumable:YES sequential:YES];
  1299. }
  1300. - (OSSTask *)multipartUpload:(OSSMultipartUploadRequest *)request resumable:(BOOL)resumable sequential:(BOOL)sequential
  1301. {
  1302. if (resumable) {
  1303. if (![request isKindOfClass:[OSSResumableUploadRequest class]]) {
  1304. NSError *typoError = [NSError errorWithDomain:OSSClientErrorDomain
  1305. code:OSSClientErrorCodeInvalidArgument
  1306. userInfo:@{OSSErrorMessageTOKEN: @"resumable multipart request should use instance of class OSSMultipartUploadRequest!"}];
  1307. return [OSSTask taskWithError: typoError];
  1308. }
  1309. }
  1310. [self checkRequestCrc64Setting:request];
  1311. OSSTask *preTask = [self preChecksForRequest:request];
  1312. if (preTask) {
  1313. return preTask;
  1314. }
  1315. return [[OSSTask taskWithResult:nil] continueWithExecutor:self.ossOperationExecutor withBlock:^id(OSSTask *task) {
  1316. __block NSUInteger uploadedLength = 0;
  1317. uploadedLength = 0;
  1318. __block OSSTask * errorTask;
  1319. __block NSString *uploadId;
  1320. NSError *error;
  1321. unsigned long long uploadFileSize = [self getSizeWithFilePath:request.uploadingFileURL.path error:&error];
  1322. if (error) {
  1323. return [OSSTask taskWithError:error];
  1324. }
  1325. NSUInteger partCount = [self judgePartSizeForMultipartRequest:request fileSize:uploadFileSize];
  1326. if (partCount > 1 && request.partSize < 102400) {
  1327. NSError *checkPartSizeError = [NSError errorWithDomain:OSSClientErrorDomain
  1328. code:OSSClientErrorCodeInvalidArgument
  1329. userInfo:@{OSSErrorMessageTOKEN: @"Part size must be greater than equal to 100KB"}];
  1330. return [OSSTask taskWithError:checkPartSizeError];
  1331. }
  1332. if (request.isCancelled) {
  1333. return [OSSTask taskWithError:[OSSClient cancelError]];
  1334. }
  1335. NSString *recordFilePath = nil;
  1336. NSMutableArray * uploadedPart = [NSMutableArray array];
  1337. NSString *localPartInfosPath = nil;
  1338. NSDictionary *localPartInfos = nil;
  1339. NSMutableArray<OSSPartInfo *> *uploadedPartInfos = [NSMutableArray array];
  1340. NSMutableArray * alreadyUploadIndex = [NSMutableArray array];
  1341. if (resumable) {
  1342. OSSResumableUploadRequest *resumableRequest = (OSSResumableUploadRequest *)request;
  1343. NSString *recordDirectoryPath = resumableRequest.recordDirectoryPath;
  1344. error = nil;
  1345. request.md5String = [OSSUtil fileMD5String:request.uploadingFileURL.path error:&error];
  1346. if (error) {
  1347. return [OSSTask taskWithError:error];
  1348. }
  1349. if ([recordDirectoryPath oss_isNotEmpty])
  1350. {
  1351. error = nil;
  1352. uploadId = [self readUploadIdForRequest:resumableRequest recordFilePath:&recordFilePath sequential:sequential error:&error];
  1353. if (error) {
  1354. return [OSSTask taskWithError:error];
  1355. }
  1356. OSSLogVerbose(@"local uploadId: %@,recordFilePath: %@",uploadId, recordFilePath);
  1357. }
  1358. if([uploadId oss_isNotEmpty])
  1359. {
  1360. localPartInfosPath = [[[NSString oss_documentDirectory] stringByAppendingPathComponent:kClientRecordNameWithCommonPrefix] stringByAppendingPathComponent:uploadId];
  1361. localPartInfos = [[NSDictionary alloc] initWithContentsOfFile:localPartInfosPath];
  1362. OSSTask *listPartTask = [self processListPartsWithObjectKey:request.objectKey
  1363. bucket:request.bucketName
  1364. uploadId:&uploadId
  1365. uploadedParts:uploadedPart
  1366. uploadedLength:&uploadedLength
  1367. totalSize:uploadFileSize
  1368. partSize:request.partSize];
  1369. if (listPartTask.error)
  1370. {
  1371. return listPartTask;
  1372. }
  1373. }
  1374. [uploadedPart enumerateObjectsUsingBlock:^(NSDictionary *partInfo, NSUInteger idx, BOOL * _Nonnull stop) {
  1375. unsigned long long remotePartNumber = 0;
  1376. NSString *partNumberString = [partInfo objectForKey: OSSPartNumberXMLTOKEN];
  1377. NSScanner *scanner = [NSScanner scannerWithString: partNumberString];
  1378. [scanner scanUnsignedLongLong: &remotePartNumber];
  1379. NSString *remotePartEtag = [partInfo objectForKey:OSSETagXMLTOKEN];
  1380. unsigned long long remotePartSize = 0;
  1381. NSString *partSizeString = [partInfo objectForKey:OSSSizeXMLTOKEN];
  1382. scanner = [NSScanner scannerWithString:partSizeString];
  1383. [scanner scanUnsignedLongLong:&remotePartSize];
  1384. #pragma clang diagnostic push
  1385. #pragma clang diagnostic ignored "-Wshorten-64-to-32"
  1386. OSSPartInfo * info = [[OSSPartInfo alloc] init];
  1387. info.partNum = remotePartNumber;
  1388. info.size = remotePartSize;
  1389. info.eTag = remotePartEtag;
  1390. #pragma clang diagnostic pop
  1391. NSDictionary *tPartInfo = [localPartInfos objectForKey: [@(remotePartNumber) stringValue]];
  1392. if (request.crcFlag == OSSRequestCRCOpen) {
  1393. if (tPartInfo == nil) {
  1394. uploadedLength -= remotePartSize;
  1395. return;
  1396. }
  1397. info.crc64 = [tPartInfo[@"crc64"] unsignedLongLongValue];
  1398. }
  1399. [uploadedPartInfos addObject:info];
  1400. [alreadyUploadIndex addObject:@(remotePartNumber)];
  1401. }];
  1402. if ([alreadyUploadIndex count] > 0 && request.uploadProgress && uploadFileSize) {
  1403. request.uploadProgress(0, uploadedLength, uploadFileSize);
  1404. }
  1405. }
  1406. if (![uploadId oss_isNotEmpty]) {
  1407. OSSInitMultipartUploadRequest *initRequest = [OSSInitMultipartUploadRequest new];
  1408. initRequest.bucketName = request.bucketName;
  1409. initRequest.objectKey = request.objectKey;
  1410. initRequest.contentType = request.contentType;
  1411. initRequest.objectMeta = request.completeMetaHeader;
  1412. initRequest.sequential = sequential;
  1413. initRequest.crcFlag = request.crcFlag;
  1414. OSSTask *task = [self processResumableInitMultipartUpload:initRequest
  1415. recordFilePath:recordFilePath];
  1416. if (task.error)
  1417. {
  1418. return task;
  1419. }
  1420. OSSInitMultipartUploadResult *initResult = (OSSInitMultipartUploadResult *)task.result;
  1421. uploadId = initResult.uploadId;
  1422. }
  1423. request.uploadId = uploadId;
  1424. localPartInfosPath = [[[NSString oss_documentDirectory] stringByAppendingPathComponent:kClientRecordNameWithCommonPrefix] stringByAppendingPathComponent:uploadId];
  1425. if (request.isCancelled)
  1426. {
  1427. if(resumable)
  1428. {
  1429. OSSResumableUploadRequest *resumableRequest = (OSSResumableUploadRequest *)request;
  1430. if (resumableRequest.deleteUploadIdOnCancelling) {
  1431. OSSTask *abortTask = [self abortMultipartUpload:request sequential:sequential resumable:resumable];
  1432. [abortTask waitUntilFinished];
  1433. }
  1434. }
  1435. return [OSSTask taskWithError:[OSSClient cancelError]];
  1436. }
  1437. if (sequential) {
  1438. errorTask = [self sequentialUpload:request
  1439. uploadIndex:alreadyUploadIndex
  1440. uploadPart:uploadedPartInfos
  1441. count:partCount
  1442. uploadedLength:&uploadedLength
  1443. fileSize:uploadFileSize];
  1444. } else {
  1445. errorTask = [self upload:request
  1446. uploadIndex:alreadyUploadIndex
  1447. uploadPart:uploadedPartInfos
  1448. count:partCount
  1449. uploadedLength:&uploadedLength
  1450. fileSize:uploadFileSize];
  1451. }
  1452. if(errorTask.error)
  1453. {
  1454. OSSTask *abortTask;
  1455. if(resumable)
  1456. {
  1457. OSSResumableUploadRequest *resumableRequest = (OSSResumableUploadRequest *)request;
  1458. if (resumableRequest.deleteUploadIdOnCancelling || errorTask.error.code == OSSClientErrorCodeFileCantWrite) {
  1459. abortTask = [self abortMultipartUpload:request sequential:sequential resumable:resumable];
  1460. }
  1461. }else
  1462. {
  1463. abortTask =[self abortMultipartUpload:request sequential:sequential resumable:resumable];
  1464. }
  1465. [abortTask waitUntilFinished];
  1466. return errorTask;
  1467. }
  1468. [uploadedPartInfos sortUsingComparator:^NSComparisonResult(OSSPartInfo *part1,OSSPartInfo* part2) {
  1469. if(part1.partNum < part2.partNum){
  1470. return NSOrderedAscending;
  1471. }else if(part1.partNum > part2.partNum){
  1472. return NSOrderedDescending;
  1473. }else{
  1474. return NSOrderedSame;
  1475. }
  1476. }];
  1477. // 如果开启了crc64的校验
  1478. uint64_t local_crc64 = 0;
  1479. if (request.crcFlag == OSSRequestCRCOpen)
  1480. {
  1481. for (NSUInteger index = 0; index< uploadedPartInfos.count; index++)
  1482. {
  1483. uint64_t partCrc64 = uploadedPartInfos[index].crc64;
  1484. int64_t partSize = uploadedPartInfos[index].size;
  1485. #pragma clang diagnostic push
  1486. #pragma clang diagnostic ignored "-Wshorten-64-to-32"
  1487. local_crc64 = [OSSUtil crc64ForCombineCRC1:local_crc64 CRC2:partCrc64 length:partSize];
  1488. #pragma clang diagnostic pop
  1489. }
  1490. }
  1491. return [self processCompleteMultipartUpload:request
  1492. partInfos:uploadedPartInfos
  1493. clientCrc64:local_crc64
  1494. recordFilePath:recordFilePath
  1495. localPartInfosPath:localPartInfosPath];
  1496. }];
  1497. }
  1498. - (OSSTask *)sequentialUpload:(OSSMultipartUploadRequest *)request
  1499. uploadIndex:(NSMutableArray *)alreadyUploadIndex
  1500. uploadPart:(NSMutableArray *)alreadyUploadPart
  1501. count:(NSUInteger)partCout
  1502. uploadedLength:(NSUInteger *)uploadedLength
  1503. fileSize:(unsigned long long)uploadFileSize
  1504. {
  1505. OSSRequestCRCFlag crcFlag = request.crcFlag;
  1506. __block OSSTask *errorTask;
  1507. __block NSMutableDictionary *localPartInfos = nil;
  1508. if (crcFlag == OSSRequestCRCOpen) {
  1509. localPartInfos = [self localPartInfosDictoryWithUploadId:request.uploadId];
  1510. }
  1511. if (!localPartInfos) {
  1512. localPartInfos = [NSMutableDictionary dictionary];
  1513. }
  1514. NSError *readError;
  1515. NSFileHandle *fileHande = [NSFileHandle fileHandleForReadingFromURL:request.uploadingFileURL error:&readError];
  1516. if (readError) {
  1517. return [OSSTask taskWithError: readError];
  1518. }
  1519. NSUInteger realPartLength = request.partSize;
  1520. for (int i = 1; i <= partCout; i++) {
  1521. if (errorTask) {
  1522. break;
  1523. }
  1524. if (request.isCancelled) {
  1525. errorTask = [OSSTask taskWithError:[OSSClient cancelError]];
  1526. break;
  1527. }
  1528. if ([alreadyUploadIndex containsObject:@(i)]) {
  1529. continue;
  1530. }
  1531. realPartLength = request.partSize;
  1532. [fileHande seekToFileOffset:request.partSize * (i - 1)];
  1533. if (i == partCout) {
  1534. #pragma clang diagnostic push
  1535. #pragma clang diagnostic ignored "-Wshorten-64-to-32"
  1536. realPartLength = uploadFileSize - request.partSize * (i - 1);
  1537. #pragma clang diagnostic pop
  1538. }
  1539. @autoreleasepool {
  1540. NSData *uploadPartData = [fileHande readDataOfLength:realPartLength];
  1541. OSSUploadPartRequest * uploadPart = [OSSUploadPartRequest new];
  1542. uploadPart.bucketName = request.bucketName;
  1543. uploadPart.objectkey = request.objectKey;
  1544. uploadPart.partNumber = i;
  1545. uploadPart.uploadId = request.uploadId;
  1546. uploadPart.uploadPartData = uploadPartData;
  1547. uploadPart.contentMd5 = [OSSUtil base64Md5ForData:uploadPartData];
  1548. uploadPart.crcFlag = request.crcFlag;
  1549. OSSTask * uploadPartTask = [self uploadPart:uploadPart];
  1550. [uploadPartTask waitUntilFinished];
  1551. if (uploadPartTask.error) {
  1552. if (labs(uploadPartTask.error.code) != 409) {
  1553. errorTask = uploadPartTask;
  1554. break;
  1555. } else {
  1556. NSDictionary *partDict = uploadPartTask.error.userInfo;
  1557. OSSPartInfo *partInfo = [[OSSPartInfo alloc] init];
  1558. partInfo.eTag = partDict[@"PartEtag"];
  1559. #pragma clang diagnostic push
  1560. #pragma clang diagnostic ignored "-Wshorten-64-to-32"
  1561. partInfo.partNum = [(NSString *)partDict[@"PartNumber"] integerValue];
  1562. partInfo.size = realPartLength;
  1563. #pragma clang diagnostic push
  1564. partInfo.crc64 = [[uploadPartData mutableCopy] oss_crc64];
  1565. [alreadyUploadPart addObject:partInfo];
  1566. }
  1567. } else {
  1568. OSSUploadPartResult * result = uploadPartTask.result;
  1569. OSSPartInfo * partInfo = [OSSPartInfo new];
  1570. partInfo.partNum = i;
  1571. partInfo.eTag = result.eTag;
  1572. partInfo.size = realPartLength;
  1573. uint64_t crc64OfPart;
  1574. @try {
  1575. NSScanner *scanner = [NSScanner scannerWithString:result.remoteCRC64ecma];
  1576. [scanner scanUnsignedLongLong:&crc64OfPart];
  1577. partInfo.crc64 = crc64OfPart;
  1578. } @catch (NSException *exception) {
  1579. OSSLogError(@"multipart upload error with nil remote crc64!");
  1580. }
  1581. [alreadyUploadPart addObject:partInfo];
  1582. if (crcFlag == OSSRequestCRCOpen)
  1583. {
  1584. [self processForLocalPartInfos:localPartInfos
  1585. partInfo:partInfo
  1586. uploadId:request.uploadId];
  1587. [self persistencePartInfos:localPartInfos
  1588. withUploadId:request.uploadId];
  1589. }
  1590. @synchronized(lock) {
  1591. *uploadedLength += realPartLength;
  1592. if (request.uploadProgress)
  1593. {
  1594. request.uploadProgress(realPartLength, *uploadedLength, uploadFileSize);
  1595. }
  1596. }
  1597. }
  1598. }
  1599. }
  1600. [fileHande closeFile];
  1601. return errorTask;
  1602. }
  1603. @end
  1604. @implementation OSSClient (PresignURL)
  1605. - (OSSTask *)presignConstrainURLWithBucketName:(NSString *)bucketName
  1606. withObjectKey:(NSString *)objectKey
  1607. withExpirationInterval:(NSTimeInterval)interval {
  1608. return [self presignConstrainURLWithBucketName:bucketName
  1609. withObjectKey:objectKey
  1610. withExpirationInterval:interval
  1611. withParameters:@{}];
  1612. }
  1613. - (OSSTask *)presignConstrainURLWithBucketName:(NSString *)bucketName
  1614. withObjectKey:(NSString *)objectKey
  1615. withExpirationInterval:(NSTimeInterval)interval
  1616. withParameters:(NSDictionary *)parameters {
  1617. return [self presignConstrainURLWithBucketName: bucketName
  1618. withObjectKey: objectKey
  1619. httpMethod: @"GET"
  1620. withExpirationInterval: interval
  1621. withParameters: parameters];
  1622. }
  1623. - (OSSTask *)presignConstrainURLWithBucketName:(NSString *)bucketName
  1624. withObjectKey:(NSString *)objectKey
  1625. httpMethod:(NSString *)method
  1626. withExpirationInterval:(NSTimeInterval)interval
  1627. withParameters:(NSDictionary *)parameters {
  1628. return [self presignConstrainURLWithBucketName:bucketName
  1629. withObjectKey:objectKey
  1630. httpMethod:method
  1631. withExpirationInterval:interval
  1632. withParameters:parameters
  1633. withHeaders:@{}];
  1634. }
  1635. - (OSSTask *)presignConstrainURLWithBucketName:(NSString *)bucketName
  1636. withObjectKey:(NSString *)objectKey
  1637. httpMethod:(NSString *)method
  1638. withExpirationInterval:(NSTimeInterval)interval
  1639. withParameters:(NSDictionary *)parameters
  1640. contentType:(nullable NSString *)contentType
  1641. contentMd5:(nullable NSString *)contentMd5 {
  1642. NSMutableDictionary *header = [NSMutableDictionary dictionary];
  1643. [header oss_setObject:contentType forKey:OSSHttpHeaderContentType];
  1644. [header oss_setObject:contentMd5 forKey:OSSHttpHeaderContentMD5];
  1645. return [self presignConstrainURLWithBucketName:bucketName
  1646. withObjectKey:objectKey
  1647. httpMethod:method
  1648. withExpirationInterval:interval
  1649. withParameters:parameters
  1650. withHeaders:header];
  1651. }
  1652. - (OSSTask *)presignConstrainURLWithBucketName:(NSString *)bucketName
  1653. withObjectKey:(NSString *)objectKey
  1654. httpMethod:(NSString *)method
  1655. withExpirationInterval:(NSTimeInterval)interval
  1656. withParameters:(NSDictionary *)parameters
  1657. withHeaders:(NSDictionary *)headers
  1658. {
  1659. return [[OSSTask taskWithResult:nil] continueWithBlock:^id(OSSTask *task) {
  1660. NSString * resource = [NSString stringWithFormat:@"/%@/%@", bucketName, objectKey];
  1661. NSString * expires = [@((int64_t)[[NSDate oss_clockSkewFixedDate] timeIntervalSince1970] + interval) stringValue];
  1662. NSString * xossHeader = @"";
  1663. NSString * contentType = headers[OSSHttpHeaderContentType];
  1664. NSString * contentMd5 = headers[OSSHttpHeaderContentMD5];
  1665. NSString * patchContentType = contentType == nil ? @"" : contentType;
  1666. NSString * patchContentMd5 = contentMd5 == nil ? @"" : contentMd5;
  1667. NSMutableDictionary * params = [NSMutableDictionary dictionary];
  1668. if (parameters.count > 0) {
  1669. [params addEntriesFromDictionary:parameters];
  1670. }
  1671. if (headers) {
  1672. NSMutableArray * params = [[NSMutableArray alloc] init];
  1673. NSArray * sortedKey = [[headers allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  1674. return [obj1 compare:obj2];
  1675. }];
  1676. for (NSString * key in sortedKey) {
  1677. if ([key hasPrefix:@"x-oss-"]) {
  1678. [params addObject:[NSString stringWithFormat:@"%@:%@", key, [headers objectForKey:key]]];
  1679. }
  1680. }
  1681. if ([params count]) {
  1682. xossHeader = [NSString stringWithFormat:@"%@\n", [params componentsJoinedByString:@"\n"]];
  1683. }
  1684. }
  1685. NSString * wholeSign = nil;
  1686. OSSFederationToken *token = nil;
  1687. NSError *error = nil;
  1688. if ([self.credentialProvider isKindOfClass:[OSSFederationCredentialProvider class]]) {
  1689. token = [(OSSFederationCredentialProvider *)self.credentialProvider getToken:&error];
  1690. if (error) {
  1691. return [OSSTask taskWithError:error];
  1692. }
  1693. } else if ([self.credentialProvider isKindOfClass:[OSSStsTokenCredentialProvider class]]) {
  1694. token = [(OSSStsTokenCredentialProvider *)self.credentialProvider getToken];
  1695. }
  1696. if ([self.credentialProvider isKindOfClass:[OSSFederationCredentialProvider class]]
  1697. || [self.credentialProvider isKindOfClass:[OSSStsTokenCredentialProvider class]])
  1698. {
  1699. [params oss_setObject:token.tToken forKey:@"security-token"];
  1700. resource = [NSString stringWithFormat:@"%@?%@", resource, [OSSUtil populateSubresourceStringFromParameter:params]];
  1701. NSString * stringToSign = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@%@", method, patchContentMd5, patchContentType, expires, xossHeader, resource];
  1702. wholeSign = [OSSUtil sign:stringToSign withToken:token];
  1703. } else {
  1704. NSString * subresource = [OSSUtil populateSubresourceStringFromParameter:params];
  1705. if ([subresource length] > 0) {
  1706. resource = [NSString stringWithFormat:@"%@?%@", resource, [OSSUtil populateSubresourceStringFromParameter:params]];
  1707. }
  1708. NSString * stringToSign = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@%@", method, patchContentMd5, patchContentType, expires, xossHeader, resource];
  1709. wholeSign = [self.credentialProvider sign:stringToSign error:&error];
  1710. if (error) {
  1711. return [OSSTask taskWithError:error];
  1712. }
  1713. }
  1714. NSArray * splitResult = [wholeSign componentsSeparatedByString:@":"];
  1715. if ([splitResult count] != 2
  1716. || ![((NSString *)[splitResult objectAtIndex:0]) hasPrefix:@"OSS "]) {
  1717. return [OSSTask taskWithError:[NSError errorWithDomain:OSSClientErrorDomain
  1718. code:OSSClientErrorCodeSignFailed
  1719. userInfo:@{OSSErrorMessageTOKEN: @"the returned signature is invalid"}]];
  1720. }
  1721. NSString * accessKey = [(NSString *)[splitResult objectAtIndex:0] substringFromIndex:4];
  1722. NSString * signature = [splitResult objectAtIndex:1];
  1723. BOOL isPathStyle = false;
  1724. NSURL * endpointURL = [NSURL URLWithString:self.endpoint];
  1725. NSString * host = endpointURL.host;
  1726. NSString * port = @"";
  1727. NSString * path = @"";
  1728. NSString * pathStylePath = @"";
  1729. if ([OSSUtil isOssOriginBucketHost:host]) {
  1730. host = [NSString stringWithFormat:@"%@.%@", bucketName, host];
  1731. } else if ([OSSUtil isIncludeCnameExcludeList:self.clientConfiguration.cnameExcludeList host:host]) {
  1732. if (self.clientConfiguration.isPathStyleAccessEnable) {
  1733. isPathStyle = true;
  1734. } else {
  1735. host = [NSString stringWithFormat:@"%@.%@", bucketName, host];
  1736. }
  1737. } else if ([[OSSIPv6Adapter getInstance] isIPv4Address:host] ||
  1738. [[OSSIPv6Adapter getInstance] isIPv6Address:host]) {
  1739. isPathStyle = true;
  1740. }
  1741. if (endpointURL.port) {
  1742. port = [NSString stringWithFormat:@":%@", endpointURL.port];
  1743. }
  1744. if (self.clientConfiguration.isCustomPathPrefixEnable) {
  1745. path = endpointURL.path;
  1746. }
  1747. if (isPathStyle) {
  1748. pathStylePath = [@"/" stringByAppendingString:bucketName];
  1749. }
  1750. [params oss_setObject:signature forKey:@"Signature"];
  1751. [params oss_setObject:accessKey forKey:@"OSSAccessKeyId"];
  1752. [params oss_setObject:expires forKey:@"Expires"];
  1753. NSString * stringURL = [NSString stringWithFormat:@"%@://%@%@%@%@/%@?%@",
  1754. endpointURL.scheme,
  1755. host,
  1756. port,
  1757. path,
  1758. pathStylePath,
  1759. [OSSUtil encodeURL:objectKey],
  1760. [OSSUtil populateQueryStringFromParameter:params]];
  1761. return [OSSTask taskWithResult:stringURL];
  1762. }];
  1763. }
  1764. - (OSSTask *)presignPublicURLWithBucketName:(NSString *)bucketName
  1765. withObjectKey:(NSString *)objectKey {
  1766. return [self presignPublicURLWithBucketName:bucketName
  1767. withObjectKey:objectKey
  1768. withParameters:@{}];
  1769. }
  1770. - (OSSTask *)presignPublicURLWithBucketName:(NSString *)bucketName
  1771. withObjectKey:(NSString *)objectKey
  1772. withParameters:(NSDictionary *)parameters {
  1773. return [[OSSTask taskWithResult:nil] continueWithBlock:^id(OSSTask *task) {
  1774. BOOL isPathStyle = false;
  1775. NSURL * endpointURL = [NSURL URLWithString:self.endpoint];
  1776. NSString * host = endpointURL.host;
  1777. NSString * port = @"";
  1778. NSString * path = @"";
  1779. NSString * pathStylePath = @"";
  1780. if ([OSSUtil isOssOriginBucketHost:host]) {
  1781. host = [NSString stringWithFormat:@"%@.%@", bucketName, host];
  1782. } else if ([OSSUtil isIncludeCnameExcludeList:self.clientConfiguration.cnameExcludeList host:host]) {
  1783. if (self.clientConfiguration.isPathStyleAccessEnable) {
  1784. isPathStyle = true;
  1785. } else {
  1786. host = [NSString stringWithFormat:@"%@.%@", bucketName, host];
  1787. }
  1788. } else if ([[OSSIPv6Adapter getInstance] isIPv4Address:host] ||
  1789. [[OSSIPv6Adapter getInstance] isIPv6Address:host]) {
  1790. isPathStyle = true;
  1791. }
  1792. if (endpointURL.port) {
  1793. port = [NSString stringWithFormat:@":%@", endpointURL.port];
  1794. }
  1795. if (self.clientConfiguration.isCustomPathPrefixEnable) {
  1796. path = endpointURL.path;
  1797. }
  1798. if (isPathStyle) {
  1799. pathStylePath = [@"/" stringByAppendingString:bucketName];
  1800. }
  1801. if ([parameters count] > 0) {
  1802. NSString * stringURL = [NSString stringWithFormat:@"%@://%@%@%@%@/%@?%@",
  1803. endpointURL.scheme,
  1804. host,
  1805. port,
  1806. path,
  1807. pathStylePath,
  1808. [OSSUtil encodeURL:objectKey],
  1809. [OSSUtil populateQueryStringFromParameter:parameters]];
  1810. return [OSSTask taskWithResult:stringURL];
  1811. } else {
  1812. NSString * stringURL = [NSString stringWithFormat:@"%@://%@%@%@%@/%@",
  1813. endpointURL.scheme,
  1814. host,
  1815. port,
  1816. path,
  1817. pathStylePath,
  1818. [OSSUtil encodeURL:objectKey]];
  1819. return [OSSTask taskWithResult:stringURL];
  1820. }
  1821. }];
  1822. }
  1823. @end
  1824. @implementation OSSClient (Utilities)
  1825. - (BOOL)doesObjectExistInBucket:(NSString *)bucketName
  1826. objectKey:(NSString *)objectKey
  1827. error:(const NSError **)error {
  1828. OSSHeadObjectRequest * headRequest = [OSSHeadObjectRequest new];
  1829. headRequest.bucketName = bucketName;
  1830. headRequest.objectKey = objectKey;
  1831. OSSTask * headTask = [self headObject:headRequest];
  1832. [headTask waitUntilFinished];
  1833. NSError *headError = headTask.error;
  1834. if (!headError) {
  1835. return YES;
  1836. } else {
  1837. if ([headError.domain isEqualToString: OSSServerErrorDomain] && labs(headError.code) == 404) {
  1838. return NO;
  1839. } else {
  1840. if (error != nil) {
  1841. *error = headError;
  1842. }
  1843. return NO;
  1844. }
  1845. }
  1846. }
  1847. @end
  1848. @implementation OSSClient (ImageService)
  1849. - (OSSTask *)imageActionPersist:(OSSImagePersistRequest *)request
  1850. {
  1851. if (![request.fromBucket oss_isNotEmpty]
  1852. || ![request.fromObject oss_isNotEmpty]
  1853. || ![request.toBucket oss_isNotEmpty]
  1854. || ![request.toObject oss_isNotEmpty]
  1855. || ![request.action oss_isNotEmpty]) {
  1856. NSError *error = [NSError errorWithDomain:OSSTaskErrorDomain
  1857. code:OSSClientErrorCodeInvalidArgument
  1858. userInfo:@{OSSErrorMessageTOKEN: @"imagePersist parameters not be empty!"}];
  1859. return [OSSTask taskWithError:error];
  1860. }
  1861. OSSNetworkingRequestDelegate *requestDelegate = request.requestDelegate;
  1862. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  1863. [params oss_setObject:@"" forKey:OSSHttpQueryProcess];
  1864. requestDelegate.uploadingData = [OSSUtil constructHttpBodyForImagePersist:request.action toBucket:request.toBucket toObjectKey:request.toObject];
  1865. OSSHttpResponseParser *responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeImagePersist];
  1866. requestDelegate.responseParser = responseParser;
  1867. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  1868. neededMsg.endpoint = self.endpoint;
  1869. neededMsg.httpMethod = OSSHTTPMethodPOST;
  1870. neededMsg.bucketName = request.fromBucket;
  1871. neededMsg.objectKey = request.fromObject;
  1872. neededMsg.params = params;
  1873. requestDelegate.allNeededMessage = neededMsg;
  1874. requestDelegate.operType = OSSOperationTypeImagePersist;
  1875. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  1876. }
  1877. @end
  1878. @implementation OSSClient (Callback)
  1879. - (OSSTask *)triggerCallBack:(OSSCallBackRequest *)request
  1880. {
  1881. OSSNetworkingRequestDelegate *requestDelegate = request.requestDelegate;
  1882. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  1883. [params oss_setObject:@"" forKey:OSSHttpQueryProcess];
  1884. NSString *paramString = [request.callbackParam base64JsonString];
  1885. NSString *variblesString = [request.callbackVar base64JsonString];
  1886. requestDelegate.uploadingData = [OSSUtil constructHttpBodyForTriggerCallback:paramString callbackVaribles:variblesString];
  1887. NSString *md5String = [OSSUtil base64Md5ForData:requestDelegate.uploadingData];
  1888. OSSHttpResponseParser *responseParser = [[OSSHttpResponseParser alloc] initForOperationType:OSSOperationTypeTriggerCallBack];
  1889. requestDelegate.responseParser = responseParser;
  1890. OSSAllRequestNeededMessage *neededMsg = [[OSSAllRequestNeededMessage alloc] init];
  1891. neededMsg.endpoint = self.endpoint;
  1892. neededMsg.httpMethod = OSSHTTPMethodPOST;
  1893. neededMsg.bucketName = request.bucketName;
  1894. neededMsg.objectKey = request.objectName;
  1895. neededMsg.contentMd5 = md5String;
  1896. neededMsg.params = params;
  1897. requestDelegate.allNeededMessage = neededMsg;
  1898. requestDelegate.operType = OSSOperationTypeTriggerCallBack;
  1899. return [self invokeRequest:requestDelegate requireAuthentication:request.isAuthenticationRequired];
  1900. }
  1901. @end