快速入门

初始化MSSClient

向MSS发送任一HTTP请求之前,必须先创建一个MSSClient实例:

//accessKey:用户的Access Key ID
//secretKey:用户的Access Key Secret
//hostname:MSS的endpoint服务地址
public static AmazonS3 CreateAmazonS3Conn (String accessKey, String secretKey, String hostname){
    AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

    //生成云存储api client
    mssClient = new AmazonS3Client(credentials);

    //配置云存储服务地址
    mssClient.setEndpoint(hostname);

    //设置客户端生成的http请求hos格式,目前只支持path type的格式,不支持bucket域名的格式
    S3ClientOptions s3ClientOptions = new S3ClientOptions();
    s3ClientOptions.setPathStyleAccess(true);
    mssClient.setS3ClientOptions(s3ClientOptions);
    return mssClient;
}

创建Bucket

存储空间(Bucket)是用户账号下的一个命名空间,相当于数据的容器,可以存储若干文件(Object),在上传文件前,必须先创建好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());
        Assert.assertEquals(true, true);
    }catch (AmazonClientException ace) {
        //客户端处理异常
        System.out.println("Caught an ClientException.");
        System.out.println("Error Message: " + ace.getMessage());
    }
    }

上传Object

以下代码展示如何上传文件(object)至MSS:

public void putObjectExample(String bucketName, String objectName, String content){
    try{
        //bucketName指定上传文件所在的桶名
        //objectName指定上传的文件名
        //content指定上传的文件内容
        s3client.putObject(bucketName,objectName,new ByteArrayInputStream(content.getBytes()),null);
    }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());
    }
}

下载Object

以下代码展示如何获取Object的文本内容:

public void getObjectExample(String bucketName, String objectName) throws IOException{
    try{
        //bucketName是桶名
        //objectName是文件名
        S3Object s3object = s3client.getObject(new GetObjectRequest(
                bucketName, objectName));
        InputStream content = s3object.getObjectContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(content));
        if (content != null) {
                while (true) {
                    String line = reader.readLine();
                    if (line == null) break;
                    System.out.println("\n" + line);
                }
            //获取object后需要close(),释放连接
            s3object.close();
        }
    }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());
    }
}

列举Object

当完成一系列上传Object操作后,可能需要查看Bucket下包含哪些Object。以下代码展示如何列举指定Bucket下的Object:

//列举object
public void objectListExample(String bucketName){
    try{
        List<S3ObjectSummary> objSum = s3client.listObjects(bucketName).getObjectSummaries();
        for(int i = 0; i < objSum.size(); i++) {
            System.out.println(objSum.get(i).getKey().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());
    }
}

删除Object

以下代码展示如何删除指定Object:

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