aliyun-oss-react-native

RNAliyunOSS+BUCKET.m 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // RNAliyunOSS+BUCKET.m
  3. // aliyun-oss-rn-sdk
  4. //
  5. // Created by 罗章 on 2018/5/8.
  6. //
  7. #import "RNAliyunOSS+BUCKET.h"
  8. @implementation RNAliyunOSS (BUCKET)
  9. /**
  10. *异步创建bucket
  11. */
  12. RCT_REMAP_METHOD(asyncCreateBucket,bucketName:(NSString*)bucketName acl:(NSString*)acl region:(NSString*)region resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
  13. OSSCreateBucketRequest * create = [OSSCreateBucketRequest new];
  14. create.bucketName = bucketName ;
  15. create.xOssACL = acl;
  16. create.location = region;
  17. OSSTask * createTask = [self.client createBucket:create];
  18. [createTask continueWithBlock:^id(OSSTask *task) {
  19. if (!task.error) {
  20. NSLog(@"create bucket success!");
  21. resolve(@"create bucket success!");
  22. } else {
  23. NSLog(@"create bucket failed, error: %@", task.error);
  24. reject(@"Error", @"Upload failed", task.error);
  25. }
  26. return nil;
  27. }];
  28. }
  29. /*
  30. 罗列出所有的bucket
  31. */
  32. RCT_REMAP_METHOD(asyncListBuckets, resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
  33. OSSGetServiceRequest * getService = [OSSGetServiceRequest new];
  34. OSSTask * getServiceTask = [self.client getService:getService];
  35. [getServiceTask continueWithBlock:^id(OSSTask *task) {
  36. if (!task.error) {
  37. OSSGetServiceResult * result = task.result;
  38. NSLog(@"buckets: %@", result.buckets);
  39. resolve(result.buckets);
  40. NSLog(@"owner: %@, %@", result.ownerId, result.ownerDispName);
  41. [result.buckets enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  42. NSDictionary * bucketInfo = obj;
  43. NSLog(@"BucketName: %@", [bucketInfo objectForKey:@"Name"]);
  44. NSLog(@"CreationDate: %@", [bucketInfo objectForKey:@"CreationDate"]);
  45. NSLog(@"Location: %@", [bucketInfo objectForKey:@"Location"]);
  46. }];
  47. }
  48. return nil;
  49. }];
  50. }
  51. /**
  52. asyncGetBucketACL
  53. */
  54. RCT_REMAP_METHOD(asyncGetBucketACL, bucketName:(NSString*)bucketName resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
  55. OSSGetBucketACLRequest *getBucketACL = [OSSGetBucketACLRequest new];
  56. getBucketACL.bucketName = bucketName;
  57. OSSTask * osstask = [self.client getBucketACL:getBucketACL];
  58. [osstask continueWithBlock:^id(OSSTask *task) {
  59. if (!task.error) {
  60. OSSGetBucketACLResult * result = task.result;
  61. resolve(result.aclGranted);
  62. }else {
  63. reject(@"Error",@"getBucketACL fail",task.error);
  64. }
  65. return nil;
  66. }];
  67. }
  68. /*
  69. asyncDeleteBucket
  70. */
  71. RCT_REMAP_METHOD(asyncDeleteBucket,withBucketName:(NSString*)bucketName resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
  72. OSSDeleteBucketRequest * delete = [OSSDeleteBucketRequest new];
  73. delete.bucketName = bucketName;
  74. OSSTask * deleteTask = [self.client deleteBucket:delete];
  75. [deleteTask continueWithBlock:^id(OSSTask *task) {
  76. if (!task.error) {
  77. NSLog(@"delete bucket success!");
  78. resolve(@"delete bucket success");
  79. } else {
  80. NSLog(@"delete bucket failed, error: %@", task.error);
  81. reject(@"Error", @"delete bucket failed", task.error);
  82. }
  83. return nil;
  84. }];
  85. }
  86. @end