Bucket

新建Bucket

  • 基本流程

    1. 创建BOSClient 类的实例。
    2. 执行BOSClient.createBucket()方法,你需要提供Bucket的名字。
    3. 对返回的对象可以调用执行getLocation()/getName()获取Bucket所在区域和Bucket名字。
  • 示例代码

    BosClient client = new BosClient(config);    //创建BOSClient实例
    client.createBucket(<BucketName>);     //新建一个Bucket,指定Bucket名称
    
  • 完整示例

    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>));  //您的原始AK/SK
                      config.setEndpoint(<EndPoint>);   //Bucket所在区域
                      BosClient client = new BosClient(config);
    
                      CreateBucketResponse response = client.createBucket(<BucketName>);   //新建一个Bucket并指定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();
          }
      }
    

    注意:由于Bucket的名称在所有区域中是唯一的,所以需要保证BucketName不与其他所有区域上的BucketName相同。

查看Bucket列表

  • 基本流程

    1. 创建BOSClient类的实例。
    2. 执行BOSClient.listBuckets()方法,会返回ListBucketsResponse类的实例。
    3. 对ListBucketsResponse类型实例可以进行getBuckets()/getOwner()/getMetadata()操作。
  • 示例代码

    如下代码可以列出用户所有的Bucket:

    List<BucketSummary> buckets = client.listBuckets().getBuckets();
    // 遍历Bucket
    for (BucketSummary bucket : buckets) {
       System.out.println(bucket.getName());
      }
    

    如下代码可以列出Bucket的Owner:

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

    如下代码可以列出Bucket的Metadata:

    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());
    

    注意:Bucket如果没有设置这些参数,返回时可能为空。

  • 完整示例

    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() ;
                    // 获得Bucket列表
                    List<BucketSummary> buckets = bucketsResponse.getBuckets();
                    for (BucketSummary bucket : buckets) {
                        System.out.println(bucket.getName());
                    }
    
                    // 获得Bucket的Owner
                    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();
    }
    }
    

判断Bucket是否存在

  • 基本流程

    1. 创建BOSClient类的实例。
    2. 执行BOSClient.doesBucketExist()方法。
    3. doesBucketExist()方法会返回布尔类型,判断bucket是否存在。
  • 示例代码

    // 获取Bucket的存在信息
    boolean exists = client.doesBucketExist(<BucketName>); //指定Bucket名称
    
    // 输出结果
    if (exists) {
        System.out.println("Bucket exists");
    } else {
        System.out.println("Bucket not exists");
    }
    
  • 完整示例

    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>;  //您的Bucket名称
    ​    
    @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);
    
                    // 获取Bucket的存在信息
                    boolean exists = client.doesBucketExist(<BucketName>); //指定Bucket名称
                    // 输出结果
                    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();
    
    }
    }
    

删除Bucket

  • 基本流程

    1. 创建BOSClient类的实例。
    2. 执行BOSClient.deleteBucket()方法。
    3. deleteBucket()无返回值,删除失败时会抛出异常。
  • 示例代码

    client.deleteBucket(<BucketName>);     //指定Bucket名称
    

    注意:如果Bucket不为空(即Bucket中有Object和未完成的三步上传Part存在),则Bucket无法被删除,必须清空Bucket后才能成功删除。

  • 完整示例

    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);
    
                    // 删除Bucket
                    client.deleteBucket(<BucketName>); //指定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权限控制

设置Bucket的访问权限

  • 基本流程

    1. 创建BOSClient类的实例。
    2. 执行BOSClient.setBucketAcl()方法。
    3. setBucketAcl()无返回值,设置失败时会抛出异常。
  • 示例代码

    // 设置Bucket访问权限
    client.setBucketAcl(<BucketName>, CannedAccessControlList.Private);
    

    说明:CannedAccessControlList是枚举类型,包含三个值: PrivatePublicReadPublicReadWrite ,它们分别对应相关权限。具体内容可以参考《BOS API文档 使用CannedAcl方式的权限控制》。

  • 完整示例

    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);
    
                    // 设置Bucket访问权限
                    client.setBucketAcl(<BucketName>, CannedAccessControlList.Private);   //指定Bucket名称,并设置Bucket权限为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();
    
    }
    }
    

设置指定用户对Bucket的访问权限

  • 基本流程
1. 创建BOSClient类的实例。
2. 执行BOSClient.setBucketAcl()方法,您需要创建一个SetBucketAclRequest的实例来提供上述信息。
3. setBucketAcl()无返回值,设置失败时会抛出异常。
  • 示例代码

    List<Grant> grants = new ArrayList<Grant>();
    List<Grantee> grantee = new ArrayList<Grantee>();
    List<Permission> permission = new ArrayList<Permission>();
    
    //授权给用户
    grantee.add(new Grantee(<UserID>);
    
    //设置权限
    permission.add(Permission.READ);
    grants.add(new Grant().withGrantee(grantee).withPermission(permission));
    
    //封装成request    
    SetBucketAclRequest request = new SetBucketAclRequest(<BucketName>, grants);
    
    //设置bucket的访问权限
    client.setBucketAcl(request);
    

    注意:Permission中的权限设置包含三个值:READWRITEFULL_CONTROL,它们分别对应相关权限。具体内容可以参考《BOS API文档 上传ACL文件方式的权限控制》。

  • 完整示例

    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.Permission;
    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<Permission> permission = new ArrayList<Permission>();
    
      @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);
    
                    //授权给特定用户
                    grantee.add(new Grantee("UserId_1"));
                    grantee.add(new Grantee("UserId_2"));
    
                    //授权给Everyone
                    grantee.add(new Grantee("*"));
    
                    //设置权限
                    permission.add(Permission.READ);
                    permission.add(Permission.WRITE);
    
                    grants.add(new Grant().withGrantee(grantee).withPermission(permission));
    
                    //封装request
                    SetBucketAclRequest request = new SetBucketAclRequest(<BucketName>, grants);
    
                    //设置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();
    
    }
    }