管理文件

在MSS中,用户可以通过一系列的接口管理存储空间(Bucket)中的文件(Object),比如ListObjects,DeleteObject,CopyObject,DoesObjectExist等。Object的名字又称为key或object key。

判断Object是否存在

public boolean doesObjectExistExample(String bucketName, String objectName){
    try {
        s3client.getObjectMetadata(bucketName, objectName);
        return true;
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == 404) {
            return false;
        }
        throw e;
    }
}

获取文件元信息(Object Meta)

文件元信息(Object Meta),是对用户上传到MSS的文件的属性描述,分为两种:HTTP标准属性(HTTP Headers)和User Meta(用户自定义元信息)。 文件元信息可以在各种方式上传或者拷贝文件时进行设置。更多文件元信息的介绍,请参看文件元信息。

public void getObjectMetaExample(String bucketName, String objectName){
    try{
        GetObjectMetadataRequest req = new GetObjectMetadataRequest(bucketName, objectName);
        ObjectMetadata objectMeta = s3client.getObjectMetadata(req);
        System.out.println(objectMeta.getETag());
        System.out.println(objectMeta.getLastModified());
    }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());
    }catch (Exception e) {
        System.out.println("Error Message: " + e.toString());
    }
}

列出存储空间中的文件

列出存储空间中的文件

ObjectListing的参数如下:

参数 含义 方法
Prefix 本次查询结果的开始前缀。 String getPrefix()
Delimiter 是一个用于对Object名字进行分组的字符。 String getDelimiter()
Marker 标明这次List Object的起点。 String getMarker()
MaxKeys 响应请求内返回结果的最大数目。 int getMaxKeys()
NextMarker 下一次List Object的起点。 String getNextMarker()
IsTruncated 指明是否所有的结果都已经返回。 boolean isTruncated()
CommonPrefixes 如果请求中指定了delimiter参数,则返回的包含CommonPrefixes元素。该元素标明以delimiter结尾,并有共同前缀的object的集合。 List getCommonPrefixes()
public void getObjectListExample(String bucketName){
    try{
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                .withBucketName(bucketName)
                //查询bucket下固定的前缀
                .withPrefix("photo");
        ObjectListing objectListing = s3client.listObjects(listObjectsRequest);
        for (S3ObjectSummary objectSummary :
                objectListing.getObjectSummaries()) {
            System.out.println(" - " + objectSummary.getKey() + "  " +
                    "(size = " + objectSummary.getSize() +
                    ")");
        }
    }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());
    }
}

分页所有获取指定前缀的Object

分页所有获取指定前缀的Object,每页maxKeys条Object。

public void getAllObjectsExample(String bucketName){
    try{
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                .withBucketName(bucketName);
        ObjectListing objectListing;
        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary :
                    objectListing.getObjectSummaries()) {
                System.out.println(" - " + objectSummary.getKey() + "  " +
                        "(size = " + objectSummary.getSize() +
                        ")");
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    }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());
    }
}

列出目录下的文件和子目录

public void getSubDirAndObjectsExample(String bucketName){
    try{
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                .withBucketName(bucketName);
        //"/" 为文件夹的分隔符
        listObjectsRequest.setDelimiter("/");
        //列出fun目录下的所有文件和文件夹
        listObjectsRequest.setPrefix("photo/");
        ObjectListing objectListing;
        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary :
                    objectListing.getObjectSummaries()) {
                System.out.println(" - " + objectSummary.getKey() + "  " +
                        "(size = " + objectSummary.getSize() +
                        ")");
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    }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());
    }
}

删除文件

您可以通过MSSClient.deleteObject删除单个文件。

public void deleteObjectExample(String bucketName, String objectName){
    try{
        s3client.deleteObject(new DeleteObjectRequest(bucketName, objectName));
    }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());
    }
}

拷贝文件

目前子支持同一账号下的文件之间的copy

public void copyObjectExample(String sourceBucketName, String sourceObjectName, String destBucketName, String destObjectName){
    try{
        CopyObjectRequest copyObjRequest = new CopyObjectRequest(
                sourceBucketName, sourceObjectName, destBucketName, destObjectName);
        s3client.copyObject(copyObjRequest);
    }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());
    }
}

更新文件元信息

public void updateObjectExample(String bucketName,String objectName) {
    try{
        //get origin objectmeta
        GetObjectMetadataRequest req = new GetObjectMetadataRequest(bucketName, objectName);
        ObjectMetadata objectMeta = s3client.getObjectMetadata(req);
        //update meta date
        objectMeta.setHeader("Content-Type","image/png");

        //create update request
        CopyObjectRequest copyObjRequest = new CopyObjectRequest(
                bucketName, objectName, bucketName, objectName);
        copyObjRequest.setNewObjectMetadata(objectMeta);
        s3client.copyObject(copyObjRequest);

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