Baidu AI Cloud
中国站

百度智能云

Object Storage

Bucket Management

Create Bucket

  • Basic procedure

    1.Create an instance of the BOSClient class. 3.To execute BOSClient putbucket method, you need to provide bucket name.

  • Sample Code

      BCETask* task = [client putbucket:@"<bucketname>"]; //Create a bucket and specify the name of the bucket 
  • Complete example

      #import<BaiduBCEBasic/BaiduBCEBasic.h> 
      #import<BaiduBCEBOS/BaiduBCEBOS.h> 
      
      void example(void) { 
      // Initialization 
      BCECredentials* credentials = [[BCECredentials alloc] init]; 
      credentials.accessKey = @"<access key>"; 
      credentials.secretKey = @"<secret key>"; 
      BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init]; 
      configuration.credentials = credentials; 
      
      BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration]; 
      
      BCETask* task = [client putbucket:@"<BucketName>"]; 
      task.then(^(BCEOutput* output) { // Task can be executed asynchronously 
          if (output.response) { 
              // Task executed successfully 
          } 
      
          if (output.error) { 
              // Task execution failed 
          } 
      
          if (output.progress) { 
              // Task execution progress 
          } 
      }); 
      [task waitUtilFinished]; // Synchronous mode can be used, until task is executed completely 
      }  

    Note: The name of bucket must be unique in all regions, so it is needed to guarantee that BucketName is not the same with the BucketName in all other regions.

View Bucket List

  • Basic procedure

    1.Create an instance of the BOSClient class. 2.Execute BOSClient listbuckets method to return instance of BOSListBucketResponse class. 3.For BOSListBucketResponse type operation, buckets/owner operation can be obtained.

  • Sample Code

    The following code can list all the users' buckets:

      __block BOSListBucketResponse* response = nil; 
      BCETask* task = [client listbuckets]; 
      task.then(^(BCEOutput* output) { 
          if (output.response) { 
            response = (BOSListBucketResponse*)output.response; 
        } 
    
        if (output.error) { 
        } 
    
      }); 
      [task waitUtilFinished]; 
    
    The following code can list the Owner of bucket: 
    
      BOSBucketOwner* owner = response.owner; 
    
    The following code can list the Metadata of bucket: 
    
      NSArray<BOSBucketSummary*>* buckets = response.buckets; 
      for (BOSBucketSummary* bucket in buckets) { 
          NSLog(@"bucket name: %@", bucket.name); 
          NSLog(@"bucket location: %@", bucket.location); 
          NSLog(@"bucket create date: %@", bucket.createDate); 
      } 
  • Complete example

      #import<BaiduBCEBasic/BaiduBCEBasic.h> 
      #import<BaiduBCEBOS/BaiduBCEBOS.h> 
      
      void example(void) { 
      // Initialization 
      BCECredentials* credentials = [[BCECredentials alloc] init]; 
      credentials.accessKey = @"<access key>"; 
      credentials.secretKey = @"<secret key>"; 
      BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init]; 
      configuration.credentials = credentials; 
      
      BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration]; 
      
      // List buckets 
      __block BOSListBucketResponse* response = nil; 
      BCETask* task = [client listbuckets]; 
      task.then(^(BCEOutput* output) { 
          if (output.response) { 
              response = (BOSListBucketResponse*)output.response; 
          } 
      
          if (output.error) { 
          } 
      }); 
      [task waitUtilFinished]; 
      
      // Obtain Owner 
      BOSBucketOwner* owner = response.owner; 
      NSLog(@"the buckets owner is %@", owner.ownerID); 
      
      // Obtain bucket information 
      NSArray<BOSBucketSummary*>* buckets = response.buckets; 
      for (BOSBucketSummary* bucket in buckets) { 
          NSLog(@"bucket name: %@", bucket.name); 
          NSLog(@"bucket location: %@", bucket.location); 
          NSLog(@"bucket create date: %@", bucket.createDate); 
      } 
      } 

Judges whether Bucket Exists, and whether There Is an Access Privilege

  • Basic procedure

    1.Create an instance of the BOSClient class. 2.Execute BOSClient headbucket method; 3.If there is no error of request, it indicates that bucket exists, and the requester has an access privilege.

  • Sample Code

      __block BOSHeadBucketResponse* response = nil; 
      BCETask* task = [client headbucket:@"<bucketname>"]; 
       task.then(^(BCEOutput* output) { 
          if (output.response) { 
              response = (BOSHeadBucketResponse*)output.response; 
              NSLog(@"head bucket success!"); 
          } 
      
        if (output.error) { 
            NSLog(@"bucket not exist or no privilege!"); 
        } 
      }); 
      [task waitUtilFinished]; 
  • Complete example

      #import<BaiduBCEBasic/BaiduBCEBasic.h> 
      #import<BaiduBCEBOS/BaiduBCEBOS.h> 
      
      void example(void) { 
      // Initialization 
      BCECredentials* credentials = [[BCECredentials alloc] init]; 
      credentials.accessKey = @"<access key>"; 
      credentials.secretKey = @"<secret key>"; 
      BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init]; 
      configuration.credentials = credentials; 
      
      BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration]; 
      
      __block BOSHeadBucketResponse* response = nil; 
      BCETask* task = [client headbucket:@"<bucketname>"]; 
      task.then(^(BCEOutput* output) { 
          if (output.response) { 
              response = (BOSHeadBucketResponse*)output.response; 
              NSLog(@"head bucket success!"); 
          } 
      
          if (output.error) { 
              NSLog(@"bucket not exist or no privilege!"); 
          } 
      }); 
      [task waitUtilFinished]; 
      } 

Delete Bucket

  • Basic procedure

    1.Create an instance of the BOSClient class. 2.Execute BOSClient deletebucket method; 3.An error is generated when deletion fails.

  • Sample Code

      __block BOSDeleteBucketResponse* response = nil; 
      BCETask* task = [client deletebucket:@"<bucketname>"]; 
      task.then(^(BCEOutput* output) { 
        if (output.response) { 
            response = (BOSDeleteBucketResponse*)output.response; 
            NSLog(@"delete bucket success!"); 
        } 
      
        if (output.error) { 
            NSLog(@"delete bucket failure"); 
        } 
      }); 
      [task waitUtilFinished]; 

    Note: If the bucket is not null (i.e. bucket has object and unfinished three-step upload Parts), the bucket cannot be deleted and must be emptied to be deleted successfully.

  • Complete example

      #import<BaiduBCEBasic/BaiduBCEBasic.h> 
      #import<BaiduBCEBOS/BaiduBCEBOS.h> 
      
      void example(void) { 
      // Initialization 
      BCECredentials* credentials = [[BCECredentials alloc] init]; 
      credentials.accessKey = @"<access key>"; 
      credentials.secretKey = @"<secret key>"; 
      BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init]; 
      configuration.credentials = credentials; 
      
      BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration]; 
      
      __block BOSDeleteBucketResponse* response = nil; 
      BCETask* task = [client deletebucket:@"<bucketname>"]; 
      task.then(^(BCEOutput* output) { 
          if (output.response) { 
              response = (BOSDeleteBucketResponse*)output.response; 
              NSLog(@"delete bucket success!"); 
          } 
      
          if (output.error) { 
              NSLog(@"delete bucket failure"); 
          } 
      }); 
      [task waitUtilFinished]; 
      } 

Bucket Privilege Control

Set Access Privileges of Bucket

  • Basic procedure

    1.Create an instance of the BOSClient class. 2.Execute BOSClient putbucketACL method. 3.An error is generated when setting fails.

  • Sample Code

      BOSPutBucketAclRequest* request = [[BOSPutBucketAclRequest alloc] init]; 
      request.cannedAcl = BOS_ACL_PUBLIC_READ; 
      request.bucket = @"<bucketname>"; 
      
      __block BOSPutBucketAclResponse* response = nil; 
      BCETask* task = [client putbucketACL:request]; 
      task.then(^(BCEOutput* output) { 
        if (output.response) { 
            response = (BOSPutBucketAclResponse*)output.response; 
            NSLog(@"pub bucket acl success!"); 
        } 
      
        if (output.error) { 
            NSLog(@"pub bucket acl failure with %@", output.error); 
        } 
      }); 
      [task waitUtilFinished]; 

    Note: 3 values are available for cannedAcl field: Private, PublicRead and PublicReadWrite, which respectively correspond to relevant privileges; for the specific contents, please see "BOS API Document privilege Control Through CannedAcl".

  • Complete example

      #import<BaiduBCEBasic/BaiduBCEBasic.h> 
      #import<BaiduBCEBOS/BaiduBCEBOS.h> 
      
      void example(void) { 
      // Initialization 
      BCECredentials* credentials = [[BCECredentials alloc] init]; 
      credentials.accessKey = @"<access key>"; 
      credentials.secretKey = @"<secret key>"; 
      BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init]; 
      configuration.credentials = credentials; 
      
      BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration]; 
      
      BOSPutBucketAclRequest* request = [[BOSPutBucketAclRequest alloc] init]; 
      request.cannedAcl = BOS_ACL_PUBLIC_READ; 
      request.bucket = @"<bucketname>"; 
      
      __block BOSPutBucketAclResponse* response = nil; 
      BCETask* task = [client putbucketACL:request]; 
      task.then(^(BCEOutput* output) { 
          if (output.response) { 
              response = (BOSPutBucketAclResponse*)output.response; 
              NSLog(@"pub bucket acl success!"); 
          } 
      
          if (output.error) { 
              NSLog(@"pub bucket acl failure with %@", output.error); 
          } 
      }); 
      [task waitUtilFinished]; 
      } 

Set the Specified User's Access to the Bucket

  • Basic procedure

    1.Create an instance of the BOSClient class. 2.To execute putbucketACL method, you need to create an instance of BOSPutBucketAclRequest to provide authorized user information. 3.An exception is thrown when setting fails.

  • Sample Code

      #import<BaiduBCEBasic/BaiduBCEBasic.h> 
      	 	 #import<BaiduBCEBOS/BaiduBCEBOS.h> 
      
      	 	 void example(void) { 
      	  // Initialization 
      	 BCECredentials* credentials = [[BCECredentials alloc] init]; 
      	 credentials.accessKey = @"<access key>"; 
      	 	 credentials.secretKey = @"<secret key>"; 
      	 BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init]; 
      	 configuration.credentials = credentials; 
      
      	 BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration]; 
      	 NSArray<NSString*>* grantee = @[ 
                                     	 	 @"<grantee1>", 
                                      	 @"<grantee2>" 
                                      	 ]; 
      
      	 NSArray<NSString*>* privilege = @[ 
                                         	 	 [BOSGrant privilegeToString:BOSbucketGranteeprivilegeRead], 
                                         	 	 [BOSGrant privilegeToString:BOSbucketGranteeprivilegeList], 
                                         	 	 ]; 
      
      	 BOSGrant* grant = [[BOSGrant alloc] init]; 
      	 grant.granteeIDArray = grantee; 
      	 grant.privilege = privilege; 
      	 BOSPutBucketAclRequest* request = [[BOSPutBucketAclRequest alloc] init]; 
      	 request.acl = [[BOSACL alloc] init]; 
      	 request.acl.grantees = @[grant]; 
      	 request.bucket = @"<bucketname>"; 
      
      	 __block BOSPutBucketAclResponse* response = nil; 
      	 BCETask* task = [client putbucketACL:request]; 
      	 task.then(^(BCEOutput* output) { 
          	 if (output.response) { 
              	 response = (BOSPutBucketAclResponse*)output.response; 
              	 NSLog(@"pub bucket acl success!"); 
          	 } 
          
          	 if (output.error) { 
              	 NSLog(@"pub bucket acl failure with %@", output.error); 
          	 } 	 
      	 }); 
      	 [task waitUtilFinished]; 
      	 	 } 

    Note: The privilege setting in privilege has three values: READ, WRITE, LIST, Getobject and FULL_CONTROL, which respectively correspond to relevant privileges; for the specific content, please see "BOS API File privilege Control Through Uploading ACL File"

  • Complete example

      #import<BaiduBCEBasic/BaiduBCEBasic.h> 
      #import<BaiduBCEBOS/BaiduBCEBOS.h> 
      
      void example(void) { 
      // Initialization 
      BCECredentials* credentials = [[BCECredentials alloc] init]; 
      credentials.accessKey = @"<access key>"; 
      credentials.secretKey = @"<secret key>"; 
      BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init]; 
      configuration.credentials = credentials; 
      
      BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration]; 
          NSArray<NSString*>* grantee = @[ 
          @"<grantee1>", 
          @"<grantee2>" 
      ]; 
      
      NSArray<NSString*>* privilege = @[ 
          [BOSGrant privilegeToString:BOSbucketGranteeprivilegeRead], 
          [BOSGrant privilegeToString:BOSbucketGranteeprivilegeList], 
      ]; 
      
      BOSGrant* grant = [[BOSGrant alloc] init]; 
      grant.granteeIDArray = grantee; 
      grant.privilege = privilege; 
      BOSPutBucketAclRequest* request = [[BOSPutBucketAclRequest alloc] init]; 
      request.acl = @[grant]; 
      request.bucket = @"<bucketname>"; 
      
      __block BOSPutBucketAclResponse* response = nil; 
      BCETask* task = [client putbucketACL:request]; 
      task.then(^(BCEOutput* output) { 
          if (output.response) { 
              response = (BOSPutBucketAclResponse*)output.response; 
              NSLog(@"pub bucket acl success!"); 
          } 
      
          if (output.error) { 
              NSLog(@"pub bucket acl failure with %@", output.error); 
          } 
      }); 
      [task waitUtilFinished]; 
      }
Previous
Initialization
Next
Object Management