OSSModel.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. //
  2. // OSSModel.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 "OSSDefine.h"
  9. #import "OSSModel.h"
  10. #import "OSSBolts.h"
  11. #import "OSSUtil.h"
  12. #import "OSSNetworking.h"
  13. #import "OSSLog.h"
  14. #import "OSSXMLDictionary.h"
  15. #if TARGET_OS_IOS
  16. #import <UIKit/UIDevice.h>
  17. #endif
  18. #import "OSSAllRequestNeededMessage.h"
  19. @implementation NSDictionary (OSS)
  20. - (NSString *)base64JsonString {
  21. NSError * error;
  22. NSData * jsonData = [NSJSONSerialization dataWithJSONObject:self
  23. options:0
  24. error:&error];
  25. if (!jsonData) {
  26. return @"e30="; // base64("{}");
  27. } else {
  28. NSString * jsonStr = [[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
  29. NSLog(@"callback json - %@", jsonStr);
  30. return [[jsonStr dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
  31. }
  32. }
  33. @end
  34. @implementation OSSSyncMutableDictionary
  35. - (instancetype)init {
  36. if (self = [super init]) {
  37. _dictionary = [NSMutableDictionary dictionary];
  38. _dispatchQueue = dispatch_queue_create("com.aliyun.aliyunsycmutabledictionary", DISPATCH_QUEUE_SERIAL);
  39. }
  40. return self;
  41. }
  42. - (NSArray *)allKeys {
  43. __block NSArray *allKeys = nil;
  44. dispatch_sync(self.dispatchQueue, ^{
  45. allKeys = [self.dictionary allKeys];
  46. });
  47. return allKeys;
  48. }
  49. - (id)objectForKey:(id)aKey {
  50. __block id returnObject = nil;
  51. dispatch_sync(self.dispatchQueue, ^{
  52. returnObject = [self.dictionary objectForKey:aKey];
  53. });
  54. return returnObject;
  55. }
  56. - (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey {
  57. dispatch_sync(self.dispatchQueue, ^{
  58. [self.dictionary oss_setObject:anObject forKey:aKey];
  59. });
  60. }
  61. - (void)removeObjectForKey:(id)aKey {
  62. dispatch_sync(self.dispatchQueue, ^{
  63. [self.dictionary removeObjectForKey:aKey];
  64. });
  65. }
  66. @end
  67. @implementation OSSFederationToken
  68. - (NSString *)description
  69. {
  70. return [NSString stringWithFormat:@"OSSFederationToken<%p>:{AccessKeyId: %@\nAccessKeySecret: %@\nSecurityToken: %@\nExpiration: %@}", self, _tAccessKey, _tSecretKey, _tToken, _expirationTimeInGMTFormat];
  71. }
  72. @end
  73. @implementation OSSPlainTextAKSKPairCredentialProvider
  74. - (instancetype)initWithPlainTextAccessKey:(nonnull NSString *)accessKey secretKey:(nonnull NSString *)secretKey {
  75. if (self = [super init]) {
  76. self.accessKey = [accessKey oss_trim];
  77. self.secretKey = [secretKey oss_trim];
  78. }
  79. return self;
  80. }
  81. - (nullable NSString *)sign:(NSString *)content error:(NSError **)error {
  82. if (![self.accessKey oss_isNotEmpty] || ![self.secretKey oss_isNotEmpty])
  83. {
  84. if (error != nil)
  85. {
  86. *error = [NSError errorWithDomain:OSSClientErrorDomain
  87. code:OSSClientErrorCodeSignFailed
  88. userInfo:@{OSSErrorMessageTOKEN: @"accessKey or secretKey can't be null"}];
  89. }
  90. return nil;
  91. }
  92. NSString * sign = [OSSUtil calBase64Sha1WithData:content withSecret:self.secretKey];
  93. return [NSString stringWithFormat:@"OSS %@:%@", self.accessKey, sign];
  94. }
  95. @end
  96. @implementation OSSCustomSignerCredentialProvider
  97. - (instancetype)initWithImplementedSigner:(OSSCustomSignContentBlock)signContent
  98. {
  99. NSParameterAssert(signContent);
  100. if (self = [super init])
  101. {
  102. _signContent = signContent;
  103. }
  104. return self;
  105. }
  106. - (NSString *)sign:(NSString *)content error:(NSError **)error
  107. {
  108. NSString * signature = @"";
  109. @synchronized(self) {
  110. signature = self.signContent(content, error);
  111. }
  112. if (*error) {
  113. *error = [NSError errorWithDomain:OSSClientErrorDomain
  114. code:OSSClientErrorCodeSignFailed
  115. userInfo:[[NSDictionary alloc] initWithDictionary:[*error userInfo]]];
  116. return nil;
  117. }
  118. return signature;
  119. }
  120. @end
  121. @implementation OSSFederationCredentialProvider
  122. - (instancetype)initWithFederationTokenGetter:(OSSGetFederationTokenBlock)federationTokenGetter {
  123. if (self = [super init]) {
  124. self.federationTokenGetter = federationTokenGetter;
  125. }
  126. return self;
  127. }
  128. - (nullable OSSFederationToken *)getToken:(NSError **)error {
  129. OSSFederationToken * validToken = nil;
  130. @synchronized(self) {
  131. if (self.cachedToken == nil) {
  132. self.cachedToken = self.federationTokenGetter();
  133. } else {
  134. if (self.cachedToken.expirationTimeInGMTFormat) {
  135. NSDateFormatter * fm = [NSDateFormatter new];
  136. [fm setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
  137. self.cachedToken.expirationTimeInMilliSecond = [[fm dateFromString:self.cachedToken.expirationTimeInGMTFormat] timeIntervalSince1970] * 1000;
  138. self.cachedToken.expirationTimeInGMTFormat = nil;
  139. OSSLogVerbose(@"Transform GMT date to expirationTimeInMilliSecond: %lld", self.cachedToken.expirationTimeInMilliSecond);
  140. }
  141. NSDate * expirationDate = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)(self.cachedToken.expirationTimeInMilliSecond / 1000)];
  142. NSTimeInterval interval = [expirationDate timeIntervalSinceDate:[NSDate oss_clockSkewFixedDate]];
  143. /* if this token will be expired after less than 2min, we abort it in case of when request arrived oss server,
  144. it's expired already. */
  145. if (interval < 5 * 60) {
  146. OSSLogDebug(@"get federation token, but after %lf second it would be expired", interval);
  147. self.cachedToken = self.federationTokenGetter();
  148. }
  149. }
  150. validToken = self.cachedToken;
  151. }
  152. if (!validToken)
  153. {
  154. if (error != nil)
  155. {
  156. *error = [NSError errorWithDomain:OSSClientErrorDomain
  157. code:OSSClientErrorCodeSignFailed
  158. userInfo:@{OSSErrorMessageTOKEN: @"Can't get a federation token"}];
  159. }
  160. return nil;
  161. }
  162. return validToken;
  163. }
  164. @end
  165. @implementation OSSStsTokenCredentialProvider
  166. - (OSSFederationToken *)getToken {
  167. OSSFederationToken * token = [OSSFederationToken new];
  168. token.tAccessKey = self.accessKeyId;
  169. token.tSecretKey = self.secretKeyId;
  170. token.tToken = self.securityToken;
  171. token.expirationTimeInMilliSecond = NSIntegerMax;
  172. return token;
  173. }
  174. - (instancetype)initWithAccessKeyId:(NSString *)accessKeyId secretKeyId:(NSString *)secretKeyId securityToken:(NSString *)securityToken {
  175. if (self = [super init]) {
  176. self.accessKeyId = [accessKeyId oss_trim];
  177. self.secretKeyId = [secretKeyId oss_trim];
  178. self.securityToken = [securityToken oss_trim];
  179. }
  180. return self;
  181. }
  182. - (NSString *)sign:(NSString *)content error:(NSError **)error {
  183. NSString * sign = [OSSUtil calBase64Sha1WithData:content withSecret:self.secretKeyId];
  184. return [NSString stringWithFormat:@"OSS %@:%@", self.accessKeyId, sign];
  185. }
  186. @end
  187. @implementation OSSAuthCredentialProvider
  188. - (instancetype)initWithAuthServerUrl:(NSString *)authServerUrl
  189. {
  190. return [self initWithAuthServerUrl:authServerUrl responseDecoder:nil];
  191. }
  192. - (instancetype)initWithAuthServerUrl:(NSString *)authServerUrl responseDecoder:(nullable OSSResponseDecoderBlock)decoder
  193. {
  194. self = [super initWithFederationTokenGetter:^OSSFederationToken * {
  195. NSURL * url = [NSURL URLWithString:self.authServerUrl];
  196. NSURLRequest * request = [NSURLRequest requestWithURL:url];
  197. OSSTaskCompletionSource * tcs = [OSSTaskCompletionSource taskCompletionSource];
  198. NSURLSession * session = [NSURLSession sharedSession];
  199. NSURLSessionTask * sessionTask = [session dataTaskWithRequest:request
  200. completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  201. if (error) {
  202. [tcs setError:error];
  203. return;
  204. }
  205. [tcs setResult:data];
  206. }];
  207. [sessionTask resume];
  208. [tcs.task waitUntilFinished];
  209. if (tcs.task.error) {
  210. return nil;
  211. } else {
  212. NSData* data = tcs.task.result;
  213. if(decoder){
  214. data = decoder(data);
  215. }
  216. NSDictionary * object = [NSJSONSerialization JSONObjectWithData:data
  217. options:kNilOptions
  218. error:nil];
  219. int statusCode = [[object objectForKey:@"StatusCode"] intValue];
  220. if (statusCode == 200) {
  221. OSSFederationToken * token = [OSSFederationToken new];
  222. // All the entries below are mandatory.
  223. token.tAccessKey = [object objectForKey:@"AccessKeyId"];
  224. token.tSecretKey = [object objectForKey:@"AccessKeySecret"];
  225. token.tToken = [object objectForKey:@"SecurityToken"];
  226. token.expirationTimeInGMTFormat = [object objectForKey:@"Expiration"];
  227. OSSLogDebug(@"token: %@ %@ %@ %@", token.tAccessKey, token.tSecretKey, token.tToken, [object objectForKey:@"Expiration"]);
  228. return token;
  229. }else{
  230. return nil;
  231. }
  232. }
  233. }];
  234. if(self){
  235. self.authServerUrl = authServerUrl;
  236. }
  237. return self;
  238. }
  239. @end
  240. NSString * const BACKGROUND_SESSION_IDENTIFIER = @"com.aliyun.oss.backgroundsession";
  241. @implementation OSSClientConfiguration
  242. - (instancetype)init {
  243. if (self = [super init]) {
  244. self.maxRetryCount = OSSDefaultRetryCount;
  245. self.maxConcurrentRequestCount = OSSDefaultMaxConcurrentNum;
  246. self.enableBackgroundTransmitService = NO;
  247. self.isHttpdnsEnable = YES;
  248. self.backgroundSesseionIdentifier = BACKGROUND_SESSION_IDENTIFIER;
  249. self.timeoutIntervalForRequest = OSSDefaultTimeoutForRequestInSecond;
  250. self.timeoutIntervalForResource = OSSDefaultTimeoutForResourceInSecond;
  251. self.isPathStyleAccessEnable = NO;
  252. self.isCustomPathPrefixEnable = NO;
  253. self.cnameExcludeList = @[];
  254. }
  255. return self;
  256. }
  257. - (void)setCnameExcludeList:(NSArray *)cnameExcludeList {
  258. NSMutableArray * array = [NSMutableArray new];
  259. [cnameExcludeList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  260. NSString * host = [(NSString *)obj lowercaseString];
  261. if ([host containsString:@"://"]) {
  262. NSString * trimHost = [host substringFromIndex:[host rangeOfString:@"://"].location + 3];
  263. [array addObject:trimHost];
  264. } else {
  265. [array addObject:host];
  266. }
  267. }];
  268. _cnameExcludeList = array.copy;
  269. }
  270. @end
  271. @implementation OSSSignerInterceptor
  272. - (instancetype)initWithCredentialProvider:(id<OSSCredentialProvider>)credentialProvider {
  273. if (self = [super init]) {
  274. self.credentialProvider = credentialProvider;
  275. }
  276. return self;
  277. }
  278. - (OSSTask *)interceptRequestMessage:(OSSAllRequestNeededMessage *)requestMessage {
  279. OSSLogVerbose(@"signing intercepting - ");
  280. NSError * error = nil;
  281. /****************************************************************
  282. * define a constant array to contain all specified subresource */
  283. static NSArray * OSSSubResourceARRAY = nil;
  284. static dispatch_once_t onceToken;
  285. dispatch_once(&onceToken, ^{
  286. OSSSubResourceARRAY = @[@"acl", @"uploadId", @"partNumber", @"uploads", @"logging", @"website", @"location",
  287. @"lifecycle", @"referer", @"cors", @"delete", @"append", @"position", @"security-token", @"x-oss-process", @"sequential",@"bucketInfo",@"symlink", @"restore"];
  288. });
  289. /****************************************************************/
  290. /* initial each part of content to sign */
  291. NSString * method = requestMessage.httpMethod;
  292. NSString * contentType = @"";
  293. NSString * contentMd5 = @"";
  294. NSString * date = requestMessage.date;
  295. NSString * xossHeader = @"";
  296. NSString * resource = @"";
  297. OSSFederationToken * federationToken = nil;
  298. if (requestMessage.contentType) {
  299. contentType = requestMessage.contentType;
  300. }
  301. if (requestMessage.contentMd5) {
  302. contentMd5 = requestMessage.contentMd5;
  303. }
  304. /* if credential provider is a federation token provider, it need to specially handle */
  305. if ([self.credentialProvider isKindOfClass:[OSSFederationCredentialProvider class]]) {
  306. federationToken = [(OSSFederationCredentialProvider *)self.credentialProvider getToken:&error];
  307. if (error) {
  308. return [OSSTask taskWithError:error];
  309. }
  310. [requestMessage.headerParams oss_setObject:federationToken.tToken forKey:@"x-oss-security-token"];
  311. } else if ([self.credentialProvider isKindOfClass:[OSSStsTokenCredentialProvider class]]) {
  312. federationToken = [(OSSStsTokenCredentialProvider *)self.credentialProvider getToken];
  313. [requestMessage.headerParams oss_setObject:federationToken.tToken forKey:@"x-oss-security-token"];
  314. }
  315. [requestMessage.headerParams oss_setObject:requestMessage.contentSHA1 forKey:OSSHttpHeaderHashSHA1];
  316. /* construct CanonicalizedOSSHeaders */
  317. if (requestMessage.headerParams) {
  318. NSMutableArray * params = [[NSMutableArray alloc] init];
  319. NSArray * sortedKey = [[requestMessage.headerParams allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  320. return [obj1 compare:obj2];
  321. }];
  322. for (NSString * key in sortedKey) {
  323. if ([key hasPrefix:@"x-oss-"]) {
  324. [params addObject:[NSString stringWithFormat:@"%@:%@", key, [requestMessage.headerParams objectForKey:key]]];
  325. }
  326. }
  327. if ([params count]) {
  328. xossHeader = [NSString stringWithFormat:@"%@\n", [params componentsJoinedByString:@"\n"]];
  329. }
  330. }
  331. /* construct CanonicalizedResource */
  332. resource = @"/";
  333. if (requestMessage.bucketName) {
  334. resource = [NSString stringWithFormat:@"/%@/", requestMessage.bucketName];
  335. }
  336. if (requestMessage.objectKey) {
  337. resource = [resource oss_stringByAppendingPathComponentForURL:requestMessage.objectKey];
  338. }
  339. if (requestMessage.params) {
  340. NSMutableArray * querys = [[NSMutableArray alloc] init];
  341. NSArray * sortedKey = [[requestMessage.params allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  342. return [obj1 compare:obj2];
  343. }];
  344. for (NSString * key in sortedKey) {
  345. NSString * value = [requestMessage.params objectForKey:key];
  346. if (![OSSSubResourceARRAY containsObject:key]) { // notice it's based on content compare
  347. continue;
  348. }
  349. if ([value isEqualToString:@""]) {
  350. [querys addObject:[NSString stringWithFormat:@"%@", key]];
  351. } else {
  352. [querys addObject:[NSString stringWithFormat:@"%@=%@", key, value]];
  353. }
  354. }
  355. if ([querys count]) {
  356. resource = [resource stringByAppendingString:[NSString stringWithFormat:@"?%@",[querys componentsJoinedByString:@"&"]]];
  357. }
  358. }
  359. /* now, join every part of content and sign */
  360. NSString * stringToSign = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@%@", method, contentMd5, contentType, date, xossHeader, resource];
  361. OSSLogDebug(@"string to sign: %@", stringToSign);
  362. if ([self.credentialProvider isKindOfClass:[OSSFederationCredentialProvider class]]
  363. || [self.credentialProvider isKindOfClass:[OSSStsTokenCredentialProvider class]])
  364. {
  365. NSString * signature = [OSSUtil sign:stringToSign withToken:federationToken];
  366. [requestMessage.headerParams oss_setObject:signature forKey:@"Authorization"];
  367. }else if ([self.credentialProvider isKindOfClass:[OSSCustomSignerCredentialProvider class]])
  368. {
  369. OSSCustomSignerCredentialProvider *provider = (OSSCustomSignerCredentialProvider *)self.credentialProvider;
  370. NSError *customSignError;
  371. NSString * signature = [provider sign:stringToSign error:&customSignError];
  372. if (customSignError) {
  373. OSSLogError(@"OSSCustomSignerError: %@", customSignError)
  374. return [OSSTask taskWithError: customSignError];
  375. }
  376. [requestMessage.headerParams oss_setObject:signature forKey:@"Authorization"];
  377. }else
  378. {
  379. NSString * signature = [self.credentialProvider sign:stringToSign error:&error];
  380. if (error) {
  381. return [OSSTask taskWithError:error];
  382. }
  383. [requestMessage.headerParams oss_setObject:signature forKey:@"Authorization"];
  384. }
  385. return [OSSTask taskWithResult:nil];
  386. }
  387. @end
  388. @implementation OSSUASettingInterceptor
  389. - (instancetype)initWithClientConfiguration:(OSSClientConfiguration *)clientConfiguration{
  390. if (self = [super init]) {
  391. self.clientConfiguration = clientConfiguration;
  392. }
  393. return self;
  394. }
  395. - (OSSTask *)interceptRequestMessage:(OSSAllRequestNeededMessage *)request {
  396. NSString * userAgent = [self getUserAgent:self.clientConfiguration.userAgentMark];
  397. [request.headerParams oss_setObject:userAgent forKey:@"User-Agent"];
  398. return [OSSTask taskWithResult:nil];
  399. }
  400. - (NSString *)getUserAgent:(NSString *)customUserAgent {
  401. static NSString * userAgent = nil;
  402. static dispatch_once_t once;
  403. NSString * tempUserAgent = nil;
  404. dispatch_once(&once, ^{
  405. NSString *localeIdentifier = [[NSLocale currentLocale] localeIdentifier];
  406. #if TARGET_OS_IOS
  407. NSString *systemName = [[[UIDevice currentDevice] systemName] stringByReplacingOccurrencesOfString:@" " withString:@"-"];
  408. NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
  409. userAgent = [NSString stringWithFormat:@"%@/%@(/%@/%@/%@)", OSSUAPrefix, OSSSDKVersion, systemName, systemVersion, localeIdentifier];
  410. #elif TARGET_OS_OSX
  411. userAgent = [NSString stringWithFormat:@"%@/%@(/%@/%@/%@)", OSSUAPrefix, OSSSDKVersion, @"OSX", [NSProcessInfo processInfo].operatingSystemVersionString, localeIdentifier];
  412. #endif
  413. });
  414. if(customUserAgent){
  415. if(userAgent){
  416. tempUserAgent = [[userAgent stringByAppendingString:@"/"] stringByAppendingString:customUserAgent];
  417. }else{
  418. tempUserAgent = customUserAgent;
  419. }
  420. }else{
  421. tempUserAgent = userAgent;
  422. }
  423. return tempUserAgent;
  424. }
  425. @end
  426. @implementation OSSTimeSkewedFixingInterceptor
  427. - (OSSTask *)interceptRequestMessage:(OSSAllRequestNeededMessage *)request {
  428. request.date = [[NSDate oss_clockSkewFixedDate] oss_asStringValue];
  429. return [OSSTask taskWithResult:nil];
  430. }
  431. @end
  432. @implementation OSSRange
  433. - (instancetype)initWithStart:(int64_t)start withEnd:(int64_t)end {
  434. if (self = [super init]) {
  435. self.startPosition = start;
  436. self.endPosition = end;
  437. }
  438. return self;
  439. }
  440. - (NSString *)toHeaderString {
  441. NSString * rangeString = nil;
  442. if (self.startPosition < 0 && self.endPosition < 0) {
  443. rangeString = [NSString stringWithFormat:@"bytes=%lld-%lld", self.startPosition, self.endPosition];
  444. } else if (self.startPosition < 0) {
  445. rangeString = [NSString stringWithFormat:@"bytes=-%lld", self.endPosition];
  446. } else if (self.endPosition < 0) {
  447. rangeString = [NSString stringWithFormat:@"bytes=%lld-", self.startPosition];
  448. } else {
  449. rangeString = [NSString stringWithFormat:@"bytes=%lld-%lld", self.startPosition, self.endPosition];
  450. }
  451. return rangeString;
  452. }
  453. @end
  454. #pragma mark request and result objects
  455. @implementation OSSGetServiceRequest
  456. - (NSDictionary *)requestParams {
  457. NSMutableDictionary * params = [NSMutableDictionary dictionary];
  458. [params oss_setObject:self.prefix forKey:@"prefix"];
  459. [params oss_setObject:self.marker forKey:@"marker"];
  460. if (self.maxKeys > 0) {
  461. [params oss_setObject:[@(self.maxKeys) stringValue] forKey:@"max-keys"];
  462. }
  463. return [params copy];
  464. }
  465. @end
  466. @implementation OSSGetServiceResult
  467. @end
  468. @implementation OSSCreateBucketRequest
  469. - (instancetype)init
  470. {
  471. self = [super init];
  472. if (self) {
  473. _storageClass = OSSBucketStorageClassStandard;
  474. }
  475. return self;
  476. }
  477. - (NSString *)storageClassAsString {
  478. NSString *storageClassString = nil;
  479. switch (_storageClass) {
  480. case OSSBucketStorageClassStandard:
  481. storageClassString = @"Standard";
  482. break;
  483. case OSSBucketStorageClassIA:
  484. storageClassString = @"IA";
  485. break;
  486. case OSSBucketStorageClassArchive:
  487. storageClassString = @"Archive";
  488. break;
  489. default:
  490. storageClassString = @"Unknown";
  491. break;
  492. }
  493. return storageClassString;
  494. }
  495. @end
  496. @implementation OSSCreateBucketResult
  497. @end
  498. @implementation OSSDeleteBucketRequest
  499. @end
  500. @implementation OSSDeleteBucketResult
  501. @end
  502. @implementation OSSGetBucketRequest
  503. - (NSDictionary *)requestParams {
  504. NSMutableDictionary * params = [NSMutableDictionary dictionary];
  505. [params oss_setObject:self.delimiter forKey:@"delimiter"];
  506. [params oss_setObject:self.prefix forKey:@"prefix"];
  507. [params oss_setObject:self.marker forKey:@"marker"];
  508. if (self.maxKeys > 0) {
  509. [params oss_setObject:[@(self.maxKeys) stringValue] forKey:@"max-keys"];
  510. }
  511. return [params copy];
  512. }
  513. @end
  514. @implementation OSSListMultipartUploadsRequest
  515. - (NSDictionary *)requestParams {
  516. NSMutableDictionary * params = [NSMutableDictionary dictionary];
  517. [params oss_setObject:self.delimiter forKey:@"delimiter"];
  518. [params oss_setObject:self.prefix forKey:@"prefix"];
  519. [params oss_setObject:self.keyMarker forKey:@"key-marker"];
  520. [params oss_setObject:self.uploadIdMarker forKey:@"upload-id-marker"];
  521. [params oss_setObject:self.encodingType forKey:@"encoding-type"];
  522. if (self.maxUploads > 0) {
  523. [params oss_setObject:[@(self.maxUploads) stringValue] forKey:@"max-uploads"];
  524. }
  525. return [params copy];
  526. }
  527. @end
  528. @implementation OSSListMultipartUploadsResult
  529. @end
  530. @implementation OSSGetBucketResult
  531. @end
  532. @implementation OSSGetBucketACLRequest
  533. - (NSDictionary *)requestParams {
  534. return @{@"acl": @""};
  535. }
  536. @end
  537. @implementation OSSGetBucketACLResult
  538. @end
  539. @implementation OSSHeadObjectRequest
  540. @end
  541. @implementation OSSHeadObjectResult
  542. @end
  543. @implementation OSSGetObjectRequest
  544. @end
  545. @implementation OSSGetObjectResult
  546. @end
  547. @implementation OSSPutObjectACLRequest
  548. - (instancetype)init
  549. {
  550. self = [super init];
  551. if (self) {
  552. _acl = @"default";
  553. }
  554. return self;
  555. }
  556. @end
  557. @implementation OSSPutObjectACLResult
  558. @end
  559. @implementation OSSPutObjectRequest
  560. - (instancetype)init {
  561. if (self = [super init]) {
  562. self.objectMeta = [NSDictionary new];
  563. }
  564. return self;
  565. }
  566. @end
  567. @implementation OSSPutObjectResult
  568. @end
  569. @implementation OSSAppendObjectRequest
  570. - (instancetype)init {
  571. if (self = [super init]) {
  572. self.objectMeta = [NSDictionary new];
  573. }
  574. return self;
  575. }
  576. @end
  577. @implementation OSSAppendObjectResult
  578. @end
  579. @implementation OSSDeleteObjectRequest
  580. @end
  581. @implementation OSSDeleteObjectResult
  582. @end
  583. @implementation OSSCopyObjectRequest
  584. - (instancetype)init {
  585. if (self = [super init]) {
  586. self.objectMeta = [NSDictionary new];
  587. }
  588. return self;
  589. }
  590. @end
  591. @implementation OSSCopyObjectResult
  592. @end
  593. @implementation OSSInitMultipartUploadRequest
  594. - (instancetype)init {
  595. if (self = [super init]) {
  596. self.objectMeta = [NSDictionary new];
  597. }
  598. return self;
  599. }
  600. @end
  601. @implementation OSSInitMultipartUploadResult
  602. @end
  603. @implementation OSSUploadPartRequest
  604. @end
  605. @implementation OSSUploadPartResult
  606. @end
  607. @implementation OSSPartInfo
  608. + (instancetype)partInfoWithPartNum:(int32_t)partNum
  609. eTag:(NSString *)eTag
  610. size:(int64_t)size {
  611. return [self partInfoWithPartNum:partNum
  612. eTag:eTag
  613. size:size
  614. crc64:0];
  615. }
  616. + (instancetype)partInfoWithPartNum:(int32_t)partNum eTag:(NSString *)eTag size:(int64_t)size crc64:(uint64_t)crc64
  617. {
  618. OSSPartInfo *parInfo = [OSSPartInfo new];
  619. parInfo.partNum = partNum;
  620. parInfo.eTag = eTag;
  621. parInfo.size = size;
  622. parInfo.crc64 = crc64;
  623. return parInfo;
  624. }
  625. - (nonnull NSDictionary *)entityToDictionary
  626. {
  627. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  628. [dict setValue:@(_partNum) forKey:@"partNum"];
  629. if (_eTag)
  630. {
  631. [dict setValue:_eTag forKey:@"eTag"];
  632. }
  633. [dict setValue:@(_size) forKey:@"size"];
  634. [dict setValue:@(_crc64) forKey:@"crc64"];
  635. return [dict copy];
  636. }
  637. - (NSString *)description
  638. {
  639. return [NSString stringWithFormat:@"OSSPartInfo<%p>:{partNum: %d,eTag: %@,partSize: %lld,crc64: %llu}",self,self.partNum,self.eTag,self.size,self.crc64];
  640. }
  641. #pragma marks - Protocol Methods
  642. - (id)copyWithZone:(nullable NSZone *)zone
  643. {
  644. OSSPartInfo *instance = [[[self class] allocWithZone:zone] init];
  645. instance.partNum = self.partNum;
  646. instance.eTag = self.eTag;
  647. instance.size = self.size;
  648. instance.crc64 = self.crc64;
  649. return instance;
  650. }
  651. @end
  652. @implementation OSSCompleteMultipartUploadRequest
  653. @end
  654. @implementation OSSCompleteMultipartUploadResult
  655. @end
  656. @implementation OSSAbortMultipartUploadRequest
  657. @end
  658. @implementation OSSAbortMultipartUploadResult
  659. @end
  660. @implementation OSSListPartsRequest
  661. @end
  662. @implementation OSSListPartsResult
  663. @end
  664. @implementation OSSMultipartUploadRequest
  665. - (instancetype)init {
  666. if (self = [super init]) {
  667. self.partSize = 256 * 1024;
  668. }
  669. return self;
  670. }
  671. - (void)cancel {
  672. [super cancel];
  673. }
  674. @end
  675. @implementation OSSResumableUploadRequest
  676. - (instancetype)init {
  677. if (self = [super init]) {
  678. self.deleteUploadIdOnCancelling = YES;
  679. self.partSize = 256 * 1024;
  680. }
  681. return self;
  682. }
  683. - (void)cancel {
  684. [super cancel];
  685. if(_runningChildrenRequest){
  686. [_runningChildrenRequest cancel];
  687. }
  688. }
  689. @end
  690. @implementation OSSResumableUploadResult
  691. @end
  692. @implementation OSSCallBackRequest
  693. @end
  694. @implementation OSSCallBackResult
  695. @end
  696. @implementation OSSImagePersistRequest
  697. @end
  698. @implementation OSSImagePersistResult
  699. @end