aliyun-oss-react-native

RNAliyunOSS+OBJECT.m 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // RNAliyunOSS+OBJECT.m
  3. // aliyun-oss-rn-sdk
  4. //
  5. // Created by 罗章 on 2018/5/8.
  6. //
  7. #import "RNAliyunOSS+OBJECT.h"
  8. @implementation RNAliyunOSS (OBJECT)
  9. /*
  10. asyncListObjects
  11. */
  12. RCT_REMAP_METHOD(asyncListObjects, bucketName:(NSString*)bucketName options:(NSDictionary*)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
  13. OSSGetBucketRequest * getBucket = [OSSGetBucketRequest new];
  14. getBucket.bucketName = bucketName;
  15. // 可选参数,具体含义参考:https://docs.aliyun.com/#/pub/oss/api-reference/bucket&GetBucket
  16. // getBucket.marker = @"";
  17. // getBucket.delimiter = @"";
  18. if([options objectForKey:@"delimiter"]) {
  19. getBucket.delimiter = [options objectForKey:@"delimiter"];
  20. }
  21. if([options objectForKey:@"marker"]) {
  22. getBucket.delimiter = [options objectForKey:@"marker"];
  23. }
  24. if([options objectForKey:@"prefix"]) {
  25. getBucket.delimiter = [options objectForKey:@"prefix"];
  26. }
  27. if([options objectForKey:@"maxkeys"]) {
  28. getBucket.delimiter = [options objectForKey:@"maxkeys"];
  29. }
  30. OSSTask * getBucketTask = [self.client getBucket:getBucket];
  31. [getBucketTask continueWithBlock:^id(OSSTask *task) {
  32. if (!task.error) {
  33. OSSGetBucketResult * result = task.result;
  34. resolve(result.contents);
  35. } else {
  36. NSLog(@"get bucket failed, error: %@", task.error);
  37. reject(@"Error",@"get bucket failed",task.error);
  38. }
  39. return nil;
  40. }];
  41. }
  42. /*
  43. * doesObjectExist
  44. */
  45. RCT_REMAP_METHOD(doesObjectExist, bucketName:(NSString*)bucketName objectKey:(NSString*)objectKey resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject ) {
  46. NSError * error = nil;
  47. BOOL isExist = [self.client doesObjectExistInBucket:bucketName objectKey:objectKey error:&error];
  48. if (!error) {
  49. if(isExist) {
  50. NSLog(@"File exists.");
  51. resolve(@"File exists.");
  52. } else {
  53. NSLog(@"File not exists.");
  54. resolve(@"File not exists.");
  55. }
  56. } else {
  57. NSLog(@"Error!");
  58. reject(@"error",@"error",error);
  59. }
  60. }
  61. /*
  62. *asyncCopyObject
  63. */
  64. RCT_REMAP_METHOD(asyncCopyObject, srcBucketName:(NSString*)srcBucketName srcObjectKey:(NSString*)srcObjectKey desBucketName:(NSString*)desBucketName desObjectKey:(NSString*)desObjectKey options:(NSDictionary*)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject ) {
  65. OSSCopyObjectRequest * copy = [OSSCopyObjectRequest new];
  66. copy.bucketName = desBucketName;
  67. copy.objectKey = desObjectKey;
  68. copy.sourceCopyFrom = [NSString stringWithFormat:@"/%@/%@", srcBucketName, srcObjectKey];
  69. OSSTask * task = [self.client copyObject:copy];
  70. [task continueWithBlock:^id(OSSTask *task) {
  71. if (!task.error) {
  72. resolve(@"copy success!");
  73. // ...
  74. } else {
  75. NSLog(@"coppy fail error");
  76. reject(@"Error",@"Copy fail",task.error);
  77. }
  78. return nil;
  79. }];
  80. }
  81. /*
  82. asyncHeadObject
  83. */
  84. RCT_REMAP_METHOD(asyncHeadObject, withBucketName:(NSString*)bucketName withObjectKey:(NSString*)objectKey resolver: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
  85. OSSHeadObjectRequest * head = [OSSHeadObjectRequest new];
  86. head.bucketName = bucketName;
  87. head.objectKey = objectKey;
  88. OSSTask * headTask = [self.client headObject:head];
  89. [headTask continueWithBlock:^id(OSSTask *task) {
  90. if (!task.error) {
  91. OSSHeadObjectResult * headResult = task.result;
  92. // NSLog(@"all response header: %@", headResult.httpResponseHeaderFields);
  93. // some object properties include the 'x-oss-meta-*'s
  94. // NSLog(@"head object result: %@", headResult.objectMeta);
  95. resolve(headResult.objectMeta);
  96. } else {
  97. NSLog(@"head object error: %@", task.error);
  98. reject(@"Error",@"head object error",task.error);
  99. }
  100. return nil;
  101. }];
  102. }
  103. /*
  104. astncDeleteObject
  105. */
  106. RCT_REMAP_METHOD(asyncDeleteObject, bucketName:(NSString*)bucketName withObjectKey:(NSString*)objectKey resolver: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
  107. OSSDeleteObjectRequest * delete = [OSSDeleteObjectRequest new];
  108. delete.bucketName = bucketName;
  109. delete.objectKey = objectKey;
  110. OSSTask * deleteTask = [self.client deleteObject:delete];
  111. [deleteTask continueWithBlock:^id(OSSTask *task) {
  112. if (!task.error) {
  113. resolve(@"delete object success");
  114. } else {
  115. reject(@"error",@"delete error",task.error);
  116. }
  117. return nil;
  118. }];
  119. }
  120. @end