aliyun-oss-react-native

RNAliyunOSS+OBJECT.m 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 prefix:(NSString*)prefix 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.prefix = prefix;
  18. // getBucket.delimiter = @"";
  19. OSSTask * getBucketTask = [self.client getBucket:getBucket];
  20. [getBucketTask continueWithBlock:^id(OSSTask *task) {
  21. if (!task.error) {
  22. OSSGetBucketResult * result = task.result;
  23. // NSLog(@"get bucket success!");
  24. // for (NSDictionary * objectInfo in result.contents) {
  25. // NSLog(@"list object: %@", objectInfo);
  26. // }
  27. resolve(result.contents);
  28. } else {
  29. NSLog(@"get bucket failed, error: %@", task.error);
  30. reject(@"Error",@"get bucket failed",task.error);
  31. }
  32. return nil;
  33. }];
  34. }
  35. /*
  36. * doesObjectExist
  37. */
  38. RCT_REMAP_METHOD(doesObjectExist, bucketName:(NSString*)bucketName objectKey:(NSString*)objectKey resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject ) {
  39. NSError * error = nil;
  40. BOOL isExist = [self.client doesObjectExistInBucket:bucketName objectKey:objectKey error:&error];
  41. if (!error) {
  42. if(isExist) {
  43. NSLog(@"File exists.");
  44. resolve(@"File exists.");
  45. } else {
  46. NSLog(@"File not exists.");
  47. resolve(@"File not exists.");
  48. }
  49. } else {
  50. NSLog(@"Error!");
  51. reject(@"error",@"error",error);
  52. }
  53. }
  54. /*
  55. *asyncCopyObject
  56. */
  57. 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 ) {
  58. OSSCopyObjectRequest * copy = [OSSCopyObjectRequest new];
  59. copy.bucketName = desBucketName;
  60. copy.objectKey = desObjectKey;
  61. copy.sourceCopyFrom = [NSString stringWithFormat:@"/%@/%@", srcBucketName, srcObjectKey];
  62. OSSTask * task = [self.client copyObject:copy];
  63. [task continueWithBlock:^id(OSSTask *task) {
  64. if (!task.error) {
  65. resolve(@"copy success!");
  66. // ...
  67. } else {
  68. NSLog(@"coppy fail error");
  69. reject(@"Error",@"Copy fail",task.error);
  70. }
  71. return nil;
  72. }];
  73. }
  74. /*
  75. asyncHeadObject
  76. */
  77. RCT_REMAP_METHOD(asyncHeadObject, withBucketName:(NSString*)bucketName withObjectKey:(NSString*)objectKey resolver: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
  78. OSSHeadObjectRequest * head = [OSSHeadObjectRequest new];
  79. head.bucketName = bucketName;
  80. head.objectKey = objectKey;
  81. OSSTask * headTask = [self.client headObject:head];
  82. [headTask continueWithBlock:^id(OSSTask *task) {
  83. if (!task.error) {
  84. OSSHeadObjectResult * headResult = task.result;
  85. // NSLog(@"all response header: %@", headResult.httpResponseHeaderFields);
  86. // some object properties include the 'x-oss-meta-*'s
  87. // NSLog(@"head object result: %@", headResult.objectMeta);
  88. resolve(headResult.objectMeta);
  89. } else {
  90. NSLog(@"head object error: %@", task.error);
  91. reject(@"Error",@"head object error",task.error);
  92. }
  93. return nil;
  94. }];
  95. }
  96. /*
  97. astncDeleteObject
  98. */
  99. RCT_REMAP_METHOD(asyncDeleteObject, bucketName:(NSString*)bucketName withObjectKey:(NSString*)objectKey resolver: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
  100. OSSDeleteObjectRequest * delete = [OSSDeleteObjectRequest new];
  101. delete.bucketName = bucketName;
  102. delete.objectKey = objectKey;
  103. OSSTask * deleteTask = [self.client deleteObject:delete];
  104. [deleteTask continueWithBlock:^id(OSSTask *task) {
  105. if (!task.error) {
  106. resolve(@"delete object success");
  107. } else {
  108. reject(@"error",@"delete error",task.error);
  109. }
  110. return nil;
  111. }];
  112. }
  113. @end