百度智能云

All Product Document

          Object Storage

          Bucket Management

          Create a Bucket

          • Basic procedure

            1.Create an instance of the BOSClient class. 2.Execute the BOSClient.createbucket () method, you need to provide the name of the bucket. 3.On the returned object, you can call to execute getLocation ()/getName () to get the bucket's region and bucket's name.

          • Sample Code

              BosClient client = new BosClient(config);    //Create the instance of BOSClient. 
              client.createBucket(<BucketName>);     //Create a bucket and specify the name of the bucket 
          • Complete example

              import android.app.Activity;
              import android.os.Bundle;
              
              import com.baidubce.BceClientException;
              import com.baidubce.BceServiceException;
              import com.baidubce.auth.DefaultBceCredentials;
              import com.baidubce.development.R;
              import com.baidubce.services.bos.BosClient;
              import com.baidubce.services.bos.BosClientConfiguration;
              import com.baidubce.services.bos.model.CreateBucketResponse;
              
              public class ExampleActivity extends Activity {
              
                private static String bucketName = <BucketName>;
              
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
              
                    new Thread(new Runnable() {
                        @Override-9
                        public void run() {
                            try {
                                BosClientConfiguration config = new BosClientConfiguration();
                                config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>));  //Your original AK/SK
                                config.setEndpoint(<EndPoint>);   //Region where Bucket is located
                                BosClient client = new BosClient(config);
                                
                                CreateBucketResponse response = client.createBucket(<BucketName>);   //Create a Bucket and specify the name of the bucket 
              
                                System.out.println(response.getLocation());
                                System.out.println(response.getName());
              
                            } catch (BceServiceException e) {
                                System.out.println("Error ErrorCode: " + e.getErrorCode());
                                System.out.println("Error RequestId: " + e.getRequestId());
                                System.out.println("Error StatusCode: " + e.getStatusCode());
                                System.out.println("Error Message: " + e.getMessage());
                                System.out.println("Error ErrorType: " + e.getErrorType());
                            } catch (BceClientException e) {
                                System.out.println("Error Message: " + e.getMessage());
                            }
                        }
                    }).start();
                    }
                }

            Note: Since the name of the bucket is unique in all regions, you need to make sure that the BucketName is different from that on all other regions.

          View Bucket List

          • Basic procedure

            1.Create an instance of the BOSClient class. 2.An instance of the ListBucketsResponse class will be returned if you execute BOSClient.listbuckets() method. 3.The getbuckets ()/getOwner ()/getMetadata () operation can be performed on the ListBucketsResponse type instance.

          • Sample Code

            The following code can list all the users' buckets: 
            
              List<BucketSummary> buckets = client.listbuckets().getbuckets(); 
              // Traverse all buckets. 
              for (BucketSummary bucket : buckets) { 
                 System.out.println(bucket.getName()); 
                } 

            The following code can list the Owner of bucket:

            User usr = client.listBuckets().getOwner();
            System.out.println(usr.getDisplayName());
            System.out.println(usr.getId());

            The following code can list the Metadata of bucket:

            BosResponseMetadata metaData = client.listBuckets().getMetadata();
            System.out.println(metaData.getBceRequestId());
            System.out.println(metaData.getBceContentSha256());
            System.out.println(metaData.getContentLength());
            System.out.println(metaData.getEtag());
            System.out.println(metaData.getExpires());
            System.out.println(metaData.getContentMd5());
            System.out.println(metaData.getContentDisposition());
            System.out.println(metaData.getLastModified());

            Note: bucket may return empty if these parameters are not set.

          • Complete example

              import java.util.List;
              import android.app.Activity;
              import android.os.Bundle;
              import com.baidubce.BceClientException;
              import com.baidubce.BceServiceException;
              import com.baidubce.auth.DefaultBceCredentials;
              import com.baidubce.development.R;
              import com.baidubce.model.User;
              import com.baidubce.services.bos.BosClient;
              import com.baidubce.services.bos.BosClientConfiguration;
              import com.baidubce.services.bos.model.BosResponseMetadata;
              import com.baidubce.services.bos.model.BucketSummary;
              import com.baidubce.services.bos.model.CreateBucketResponse;
              import com.baidubce.services.bos.model.ListBucketsResponse;
              ​    
              public class ExampleActivity extends Activity {
              
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  new Thread(new Runnable() {
                      @Override
                      public void run() {
                          try {
                              BosClientConfiguration config = new BosClientConfiguration();
                              config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>));
                              config.setEndpoint(<EndPoint>);    
                              BosClient client = new BosClient(config);
                              
                              ListBucketsResponse bucketsResponse = client.listBuckets() ;
                              // Obtain bucket List
                              List<BucketSummary> buckets = bucketsResponse.getBuckets();
                              for (BucketSummary bucket : buckets) {
                                  System.out.println(bucket.getName());
                              }
                              
                              // Obtain the Owner of Bucket
                              User user = bucketsResponse.getOwner();
                              System.out.println(user.getDisplayName());
                              System.out.println(user.getId());
                              
                          } catch (BceServiceException e) {
                              System.out.println("Error ErrorCode: " + e.getErrorCode());
                              System.out.println("Error RequestId: " + e.getRequestId());
                              System.out.println("Error StatusCode: " + e.getStatusCode());
                              System.out.println("Error Message: " + e.getMessage());
                              System.out.println("Error ErrorType: " + e.getErrorType());
                          } catch (BceClientException e) {
                              System.out.println("Error Message: " + e.getMessage());
                          }
                      }
                  }).start();
              }
              }

          Judge whether a Bucket Exists or Not

          • Basic procedure

            1.Create an instance of the BOSClient class. 2.Execute BOSClient.doesBucketExist() method. 3.The doesBucketExist () method returns the Boolean type to judge if a bucket exists.

          • Sample Code

              // Obtain the presence information of the bucket 
              boolean exists = client.doesBucketExist(<BucketName>); //Designate the name of the bucket: 
              
              // Output result 
              if (exists) { 
                  System.out.println("Bucket exists"); 
              } else { 
                  System.out.println("Bucket not exists"); 
              } 
          • Complete example

              import android.app.Activity;
              import android.os.Bundle;
              import com.baidubce.BceClientException;
              import com.baidubce.BceServiceException;
              import com.baidubce.auth.DefaultBceCredentials;
              import com.baidubce.development.R;
              import com.baidubce.services.bos.BosClient;
              import com.baidubce.services.bos.BosClientConfiguration;
              ​    
              public class ExampleActivity extends Activity {
              
              private String bucketName = <BucketName>;  //Your bucket name 
              ​    
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  new Thread(new Runnable() {
                      @Override
                      public void run() {
                          try {
                              BosClientConfiguration config = new BosClientConfiguration();
                              config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>));
                              config.setEndpoint(<EndPoint>);
                              BosClient client = new BosClient(config);
                              
                              //  Obtain the presence information of the bucket 
                              boolean exists = client.doesBucketExist(<BucketName>); //指定Bucket名称
                              //  Output result 
                              if (exists) {
                                  System.out.println("Bucket exists");
                              } else {
                                  System.out.println("Bucket not exists");
                              }
              
                          } catch (BceServiceException e) {
                              System.out.println("Error ErrorCode: " + e.getErrorCode());
                              System.out.println("Error RequestId: " + e.getRequestId());
                              System.out.println("Error StatusCode: " + e.getStatusCode());
                              System.out.println("Error Message: " + e.getMessage());
                              System.out.println("Error ErrorType: " + e.getErrorType());
                          } catch (BceClientException e) {
                              System.out.println("Error Message: " + e.getMessage());
                          }
                      }
                  }).start();
              
              }
              }

          Delete Bucket

          • Basic procedure

            1.Create an instance of the BOSClient class. 2.Execute BOSClient.deletebucket() method. 3.deletebucket () has no return value, and an exception will be thrown when the deletion fails.

          • Sample Code

              client.deleteBucket(<BucketName>);     //Designate the name of the Bucket: 

            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 android.app.Activity;
              import android.os.Bundle;
              import com.baidubce.BceClientException;
              import com.baidubce.BceServiceException;
              import com.baidubce.auth.DefaultBceCredentials;
              import com.baidubce.development.R;
              import com.baidubce.services.bos.BosClient;
              import com.baidubce.services.bos.BosClientConfiguration;
              ​    
              public class ExampleActivity extends Activity {
              
                private String bucketName = <BucketName>;
              
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  new Thread(new Runnable() {
                      @Override
                      public void run() {
                          try {
                              BosClientConfiguration config = new BosClientConfiguration();
                              config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>));
                              config.setEndpoint(<EndPoint>);
                              BosClient client = new BosClient(config);
                              
                              // Delete Bucket
                              client.deleteBucket(<BucketName>); //Designate the name of the Bucket
              
                          } catch (BceServiceException e) {
                              System.out.println("Error ErrorCode: " + e.getErrorCode());
                              System.out.println("Error RequestId: " + e.getRequestId());
                              System.out.println("Error StatusCode: " + e.getStatusCode());
                              System.out.println("Error Message: " + e.getMessage());
                              System.out.println("Error ErrorType: " + e.getErrorType());
                          } catch (BceClientException e) {
                              System.out.println("Error Message: " + e.getMessage());
                          }
                      }
                  }).start();
              }
              }

          Bucket Privilege Control

          Set access Privileges of bucket

          • Basic procedure

            1.Create an instance of the BOSClient class. 2.Execute BOSClient.setBucketAcl() method. 3.setBucketAcl () has no return value, and an exception will be thrown when the setting fails.

          • Sample Code

              // Set access Privileges of bucket 
              client.setBucketAcl(<BucketName>, CannedAccessControlList.Private); 

            Note: CannedAccessControlList is an enumeration type that contains three values: Private, PublicRead, PublicReadWrite . They correspond to the respective privileges. For the specific contents, please see BOS API Document [Privilege Control through CannedAcl](BOS/API Reference/Access Control.md# Privilege Control through ACL File Uploading).

          • Complete example

              import org.json.JSONException;
              import android.app.Activity;
              import android.os.Bundle;
              import com.baidubce.BceClientException;
              import com.baidubce.BceServiceException;
              import com.baidubce.auth.DefaultBceCredentials;
              import com.baidubce.development.R;
              import com.baidubce.services.bos.BosClient;
              import com.baidubce.services.bos.BosClientConfiguration;
              import com.baidubce.services.bos.model.CannedAccessControlList;
              ​    
              public class ExampleActivity extends Activity {
              
                private String bucketName = <BucketName>;
              
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  new Thread(new Runnable() {
                      @Override
                      public void run() {
                          try {
                              BosClientConfiguration config = new BosClientConfiguration();
                              config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>));
                              config.setEndpoint(<EndPoint>);
                              BosClient client = new BosClient(config);
                              
                              // Set access Privileges of bucket
                              client.setBucketAcl(<BucketName>, CannedAccessControlList.Private);   //Specify the bucket name and set the bucket Privilege to Private 
              
                          } catch (BceServiceException e) {
                              System.out.println("Error ErrorCode: " + e.getErrorCode());
                              System.out.println("Error RequestId: " + e.getRequestId());
                              System.out.println("Error StatusCode: " + e.getStatusCode());
                              System.out.println("Error Message: " + e.getMessage());
                              System.out.println("Error ErrorType: " + e.getErrorType());
                          } catch (BceClientException e) {
                              System.out.println("Error Message: " + e.getMessage());
                          } catch (JSONException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                      }
                  }).start();
              
              }
              }

          Set the specified user's access to the bucket

          • Basic procedure

            1.Create an instance of the BOSClient class. 2.Create an example of SetBucketAclRequest to provide the above information for executing BOSClient.setBucketAcl(). 3.setBucketAcl () has no return value, and an exception will be thrown when the setting fails.

          • Sample Code

              List<Grant> grants = new ArrayList<Grant>(); 
              List<Grantee> grantee = new ArrayList<Grantee>(); 
              List<Privilege> Privilege = new ArrayList<Privilege>(); 
              
              //Privilege to users 
              grantee.add(new Grantee(<UserID>); 
              
              //Privilege setting 
              Privilege.add(Privilege.READ); 
              grants.add(new Grant().withGrantee(grantee).withPrivilege(Privilege)); 
              
              //Request packaging 	 
              SetBucketAclRequest request = new SetBucketAclRequest(<BucketName>, grants); 
              
              //Set access Privileges of bucket 
              client.setBucketAcl(request); 

            Note: The privilege setting in privilege has three values: READ, WRITE and FULL_CONTROL, corresponding to respective privileges. For the specific contents, please see BOS API Document [Privilege Control Through ACL File Uploading]( BOS/API Reference/Access Control.md#Privilege Control Through ACL File Uploading).

          • Complete example

              import java.util.ArrayList; 
              import java.util.List;
              import org.json.JSONException;
              import android.app.Activity;
              import android.os.Bundle;
              import com.baidubce.BceClientException;
              import com.baidubce.BceServiceException;
              import com.baidubce.auth.DefaultBceCredentials;
              import com.baidubce.development.R;
              import com.baidubce.services.bos.BosClient;
              import com.baidubce.services.bos.BosClientConfiguration;
              import com.baidubce.services.bos.model.Grant;
              import com.baidubce.services.bos.model.Grantee;
              import com.baidubce.services.bos.model.Privilege;
              import com.baidubce.services.bos.model.SetBucketAclRequest;
              ​    
              public class ExampleActivity extends Activity {
              
                private String bucketName = <BucektName>;
                List<Grant> grants = new ArrayList<Grant>();
                List<Grantee> grantee = new ArrayList<Grantee>();
                List<Privilege> Privilege = new ArrayList<Privilege>();
              
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
                  new Thread(new Runnable() {
                      @Override
                      public void run() {
                          try {
                              BosClientConfiguration config = new BosClientConfiguration();
                              config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>));
                              config.setEndpoint(<EndPoint>);
                              BosClient client = new BosClient(config);
                              
                              //Privilege to specific users
                              grantee.add(new Grantee("UserId_1"));
                              grantee.add(new Grantee("UserId_2"));
                              
                              //Privilege to Everyone
                              grantee.add(new Grantee("*"));
              
                              //Privilege setting
                              Privilege.add(Privilege.READ);
                              Privilege.add(Privilege.WRITE);
              
                              grants.add(new Grant().withGrantee(grantee).withPrivilege(Privilege));
                              
                              //Request packaging 
                              SetBucketAclRequest request = new SetBucketAclRequest(<BucketName>, grants);
                              
                              //Set access Privileges of bucket 
                              client.setBucketAcl(request);
                              
                          } catch (BceServiceException e) {
                              System.out.println("Error ErrorCode: " + e.getErrorCode());
                              System.out.println("Error RequestId: " + e.getRequestId());
                              System.out.println("Error StatusCode: " + e.getStatusCode());
                              System.out.println("Error Message: " + e.getMessage());
                              System.out.println("Error ErrorType: " + e.getErrorType());
                          } catch (BceClientException e) {
                              System.out.println("Error Message: " + e.getMessage());
                          } catch (JSONException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          } 
                      }
                  }).start();
              
              }
              }
          Previous
          Initialization
          Next
          Object Management