Keine Beschreibung

OSSClient.m 90KB

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