管理存储空间

创建Bucket

您可以使用MSSClient.createBucket创建Bucket。如下代码展示如何新建一个Bucket:

public void createBucketIfNotExistExample(String bucketName){
    try{
        //判断待创建的bucket是否存在,如果存在不用重复创建,重复创建同名bucket服务器端会返回错误
        if (s3client.doesBucketExist(bucketName) == false) {
            s3client.createBucket(bucketName);
        }
    }catch (AmazonServiceException ase) {
        //存储服务端处理异常
        System.out.println("Caught an ServiceException.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    }catch (AmazonClientException ace) {
        //客户端处理异常
        System.out.println("Caught an ClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
    }

列举Bucket

您可以使用MSSClient.listBuckets列举指定用户下的Bucket。

public void listBuckets(){
    try{
        // 列举bucket
        List<Bucket> buckets = s3client.listBuckets();
        for (Bucket bucket : buckets) {
            System.out.println(bucket.getName());
        }
    }catch (AmazonServiceException ase) {
        //存储服务端处理异常
        System.out.println("Caught an ServiceException.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    }catch (AmazonClientException ace) {
        //客户端处理异常
        System.out.println("Caught an ClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

删除Bucket

您可以使用MSSClient.deleteBucket删除Bucket。以下代码展示如何删除一个Bucket:

public void deleteBucketExample(String bucketName){
        try{
            if (s3client.doesBucketExist(bucketName)) {
                s3client.deleteBucket(bucketName);
            }
        }catch (AmazonServiceException ase) {
            //存储服务端处理异常
            System.out.println("Caught an ServiceException.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        }catch (AmazonClientException ace) {
            //客户端处理异常
            System.out.println("Caught an ClientException.");
            System.out.println("Error Message: " + ace.getMessage());
        }
    }

判断Bucket是否存在

您可以使用MSSClient.doesBucketExist接口判断该Bucket是否已存在。以下代码展示如何判断指定Bucket是否存在:

public void checkBucketExistExample(String bucketName){
    try{
        boolean exist = s3client.doesBucketExist(bucketName);
    }catch (AmazonServiceException ase) {
        //存储服务端处理异常
        System.out.println("Caught an ServiceException.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    }catch (AmazonClientException ace) {
        //客户端处理异常
        System.out.println("Caught an ClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

设置Bucket ACL

Bucket的ACL包含三类:Private(私有读写), PublicRead(公共读私有写)。您可以通过MSSClient.setBucketAcl设置bucket的权限。

权限 Java SDK对应值
私有读写 CannedAccessControlList.Private
公共读私有写 CannedAccessControlList.PublicRead

以下代码展示如何设置Bucket的权限:

public void setBucketAclExample(String bucketName){
    try{
        // 将桶设置为公共读、私有写
        s3client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
    }catch (AmazonServiceException ase) {
        //存储服务端处理异常
        System.out.println("Caught an ServiceException.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    }catch (AmazonClientException ace) {
        //客户端处理异常
        System.out.println("Caught an ClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

获取Bucket ACL

您可以通过MSSClient.getBucketAcl获取bucket的权限。以下代码展示如何获取Bucket的ACL:

public void getBucketAclExample(String bucketName){
    try{
        AccessControlList bucketacl = s3client.getBucketAcl(bucketName);
        System.out.println("Bucket acl:" + bucketacl.toString() );
    }catch (AmazonServiceException ase) {
        //存储服务端处理异常
        System.out.println("Caught an ServiceException.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
        Assert.assertEquals(true, true);
    }catch (AmazonClientException ace) {
        //客户端处理异常
        System.out.println("Caught an ClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

Bucket Policy(策略)管理

设置policy

public void setBucketPolicy(String bucketName, String policyJsonContent){
    try{
        SetBucketPolicyRequest req = new SetBucketPolicyRequest(bucketName,policyJsonContent);
        s3client.setBucketPolicy(req);
    }catch (AmazonServiceException ase) {
        //存储服务端处理异常
        System.out.println("Caught an ServiceException.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    }catch (AmazonClientException ace) {
        //客户端处理异常
        System.out.println("Caught an ClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

获取policy

public void getBucketPolicy(String bucketName){
    try{
        GetBucketPolicyRequest getReq = new GetBucketPolicyRequest(bucketName);
        BucketPolicy policy = s3client.getBucketPolicy(getReq);
        System.out.println("policy: " + policy.getPolicyText());
    }catch (AmazonServiceException ase) {
        //存储服务端处理异常
        System.out.println("Caught an ServiceException.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    }catch (AmazonClientException ace) {
        //客户端处理异常
        System.out.println("Caught an ClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

删除policy

public void deleteBucketPolicy(String bucketName){
    try{
        DeleteBucketPolicyRequest delReq = new DeleteBucketPolicyRequest(bucketName);
        s3client.deleteBucketPolicy(delReq);
    }catch (AmazonServiceException ase) {
        //存储服务端处理异常
        System.out.println("Caught an ServiceException.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    }catch (AmazonClientException ace) {
        //客户端处理异常
        System.out.println("Caught an ClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

如何基于policy设置Referer

白名单设置(不允许空referer)
配置域名 http://www.example.com http://example.com
{
              "Version":"2012-10-17",
              "Statement":[
              {
                     "Sid":"Allow get requests without referrer",
                     "Effect":"Deny",
                     "Principal":{"AWS": "*"},
                     "Action":["s3:GetObject"],
                     "Resource":["arn:aws:s3:::examplebucket/*"],
                     "Condition":{
                             "StringNotLike":{"aws:Referer":["http://www.example.com/*","http://example.com/*"]}
                     }
              }
              ]
}

白名单设置(允许空referer)
配置域名 http://www.example.com http://example.com
{
              "Version":"2012-10-17",
              "Statement":[
              {
                     "Sid":"Allow get requests without referrer",
                     "Effect":"Deny",
                     "Principal":"*",
                     "Action":["s3:GetObject"],
                     "Resource":"arn:aws:s3:::examplebucket/*",
                     "Condition":{
                             "StringNotLike":{"aws:Referer":["http://www.example.com/*","http://example.com/*",""]}
                     }
              }
              ]
}


黑名单设置(不允许空referer)
{
              "Version":"2012-10-17",
              "Statement":[
              {
                     "Sid":"Allow get requests without referrer",
                     "Effect":"Deny",
                     "Principal":"*",
                     "Action":["s3:GetObject"],
                     "Resource":"arn:aws:s3:::examplebucket/*",
                     "Condition":{
                             "StringLike":{"aws:Referer":["http://www.example.com/*","http://example.com/*",""]}
                     }
              }
              ]
}

黑名单设置(允许空referer)
{
              "Version":"2012-10-17",
              "Id":"http referer policy example",
              "Statement":[
              {
                     "Sid":"Allow get requests without referrer",
                     "Effect":"Deny",
                     "Principal":{"AWS": "*"},
                     "Action":["s3:GetObject"],
                     "Resource":["arn:aws:s3:::examplebucket/*"],
                     "Condition":{
                             "StringLike":{"aws:Referer":["http://www.example.com/*","http://example.com/*"]}
                     }
              }
              ]
}