Bucket

新建Bucket

  • 基本流程

    1. 创建BOSClient类的实例。
    2. 执行BOSClient putBucket方法,您需要提供Bucket的名字。
  • 示例代码

    BCETask* task = [client putBucket:@"<bucketname>"]; //新建一个Bucket,指定Bucket名称
    
  • 完整示例

    #import <BaiduBCEBasic/BaiduBCEBasic.h>
    #import <BaiduBCEBOS/BaiduBCEBOS.h>
    
    void example(void) {
    // 初始化
    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) { // 任务可以异步执行。
        if (output.response) {
            // 任务执行成功。
        }
    
        if (output.error) {
            // 任务执行失败。
        }
    
        if (output.progress) {
            // 任务执行进度。
        }
    });
    [task waitUtilFinished]; // 可以同步方式,等待任务执行完毕。
    }
    

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

查看Bucket列表

  • 基本流程

    1. 创建BOSClient类的实例。
    2. 执行BOSClient listBuckets方法,会返回BOSListBucketResponse类的实例。
    3. 对BOSListBucketResponse类型实例可以进行获取buckets/owner操作。
  • 示例代码

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

    __block BOSListBucketResponse* response = nil;
    BCETask* task = [client listBuckets];
    task.then(^(BCEOutput* output) {
        if (output.response) {
          response = (BOSListBucketResponse*)output.response;
      }
    
      if (output.error) {
      }
    });
    [task waitUtilFinished];
    

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

    BOSBucketOwner* owner = response.owner;
    

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

    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);
    }
    
  • 完整示例

    #import <BaiduBCEBasic/BaiduBCEBasic.h>
    #import <BaiduBCEBOS/BaiduBCEBOS.h>
    
    void example(void) {
    // 初始化
    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];
    
    // 列举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];
    
    // 获取Owner
    BOSBucketOwner* owner = response.owner;
    NSLog(@"the buckets owner is %@", owner.ownerID);
    
    // 获取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);
    }
    }
    

判断Bucket是否存在,以及是否有权限访问

  • 基本流程

    1. 创建BOSClient类的实例;
    2. 执行BOSClient headBucket方法;
    3. 当请求无错误发生时,说明Bucket存在且请求者有权限访问。
  • 示例代码

    __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 permission!");
      }
    });
    [task waitUtilFinished];
    
  • 完整示例

    #import <BaiduBCEBasic/BaiduBCEBasic.h>
    #import <BaiduBCEBOS/BaiduBCEBOS.h>
    
    void example(void) {
    // 初始化
    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 permission!");
        }
    });
    [task waitUtilFinished];
    }
    

删除Bucket

  • 基本流程

    1. 创建BOSClient类的实例;
    2. 执行BOSClient deleteBucket方法;
    3. 删除失败时会产生错误。
  • 示例代码

    __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不为空(即Bucket中有Object和未完成的三步上传Part存在),则Bucket无法被删除,必须清空Bucket后才能成功删除。

  • 完整示例

    #import <BaiduBCEBasic/BaiduBCEBasic.h>
    #import <BaiduBCEBOS/BaiduBCEBOS.h>
    
    void example(void) {
    // 初始化
    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权限控制

设置Bucket的访问权限

  • 基本流程

    1. 创建BOSClient类的实例;
    2. 执行BOSClient putBucketACL方法;
    3. 设置失败时会产生错误。
  • 示例代码

    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];
    

    说明:cannedAcl 字段可取3个值:PrivatePublicReadPublicReadWrite,它们分别对应相关权限,具体内容可以参考《BOS API文档 使用CannedAcl方式的权限控制》。

  • 完整示例

    #import <BaiduBCEBasic/BaiduBCEBasic.h>
    #import <BaiduBCEBOS/BaiduBCEBOS.h>
    
    void example(void) {
    // 初始化
    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];
    }
    

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

  • 基本流程

    1. 创建BOSClient类的实例。
    2. 执行putBucketACL方法,您需要创建一个BOSPutBucketAclRequest的实例来提供授权用户信息。
    3. 设置失败时会抛出异常。
  • 示例代码

    #import <BaiduBCEBasic/BaiduBCEBasic.h>
    #import <BaiduBCEBOS/BaiduBCEBOS.h>
    
    void example(void) {
        // 初始化
        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*>* permission = @[
                                               [BOSGrant permissionToString:BOSBucketGranteePermissionRead],
                                               [BOSGrant permissionToString:BOSBucketGranteePermissionList],
                                               ];
    
        BOSGrant* grant = [[BOSGrant alloc] init];
        grant.granteeIDArray = grantee;
        grant.permission = permission;
        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];
    }
    

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

  • 完整示例

    #import <BaiduBCEBasic/BaiduBCEBasic.h>
    #import <BaiduBCEBOS/BaiduBCEBOS.h>
    
    void example(void) {
    // 初始化
    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*>* permission = @[
        [BOSGrant permissionToString:BOSBucketGranteePermissionRead],
        [BOSGrant permissionToString:BOSBucketGranteePermissionList],
    ];
    
    BOSGrant* grant = [[BOSGrant alloc] init];
    grant.granteeIDArray = grantee;
    grant.permission = permission;
    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];
    }