AWS S3 JAVA SDK

本文代码为网易云 AWS S3 JAVA SDK 代码,为什么要写到这里,因为开发给的文档有问题,代码也有问题,无奈自己调试。

初始化

/**
 * 初始化
 */

String accessKey = "";
String secretKey = "";
AWSCredentials credentials = new BasicAWSCredentials(accessKey,secretKey);
ClientConfiguration conf = new ClientConfiguration();
// 设置AmazonS3使用的最大连接数
conf.setMaxConnections(200);
// 设置socket超时时间
conf.setSocketTimeout(10000);
// 设置失败请求重试次数
conf.setMaxErrorRetry(2);
// 如果要用https协议,请加上下面语句
conf.setProtocol(Protocol.HTTPS);

//AmazonS3 s3Client = new AmazonS3Client(credentials,clientConfiguration);
//s3Client.setEndpoint(endPoint);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(credentials))
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("nos-eastchina1.126.net","us-east-1"))
            .withClientConfiguration(conf)
            .build();//endpoint,region请指定为NOS支持的

创建桶

        /**
         * 创建桶
         */
        String bucketName = "cloud201411";
        boolean exists = s3Client.doesBucketExist(bucketName);
        if (exists) {
            System.out.println("桶已存在");
        }else {
            CreateBucketRequest request = new CreateBucketRequest(bucketName);
            request.setCannedAcl(CannedAccessControlList.PublicRead);
            s3Client.createBucket(request);

        }

        /**
         * 列举桶
         */

        for (Bucket bucket : s3Client.listBuckets()) {
             System.out.println(" - " + bucket.getName());
        }

        /**
         * 删除桶
         */

//        s3Client.deleteBucket(bucketName);
//        for (Bucket bucket : s3Client.listBuckets()) {
//             System.out.println(" - " + bucket.getName());
//        }

        /**
         * 查看桶的 ACL
         */

        AccessControlList accessControlList = s3Client.getBucketAcl(bucketName);
        System.out.println("owner : " +  accessControlList.getOwner().getId() + " : " + accessControlList.getOwner().getDisplayName());
        for(Grant grant : accessControlList.getGrantsAsList()){//NOS由于权限不能赋值给其他的用户,所以返回值中只有一条记录
              System.out.println(grant.getGrantee().getIdentifier() + " : " +  grant.getPermission() + " : " + grant.getGrantee().getTypeIdentifier());
        }

上传文件

            /**
         * 上传内容
         */

        //要上传文件的路径
//        String content = "Object content";
//        try {
//           s3Client.putObject(bucketName,"a/b/test.txt",content);
//        }catch (Exception e){
//           System.out.println(e.getMessage());
//        }

        /**
         * 上传普通文件
         */

        //要上传文件的路径
//        String filePath = "tomcat.zip";
//        try {
//           s3Client.putObject(bucketName,"tomcat.zip", new File(filePath));
//        }catch (Exception e){
//           System.out.println(e.getMessage());
//        }

        /**
         * 设置元数据
         * 
         */


//        String filePath = "tomcat.zip";
//        ObjectMetadata objectMetadata = new ObjectMetadata();
//        //设置Content-Type
//        objectMetadata.setContentType("application/zip");
//        //设置标准http消息头(元数据)
//        objectMetadata.setHeader("Cache-Control", "no-cache");
//        //设置用户自定义元数据信息
//        Map<String, String> userMeta = new HashMap<String, String>();
//        userMeta.put("ud", "test");
//        objectMetadata.setUserMetadata(userMeta);
//        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,"tomcat1.zip", new File(filePath));
//        putObjectRequest.setMetadata(objectMetadata);
//        s3Client.putObject(putObjectRequest);

//        
//        try {
//               ObjectMetadata objectMetadata = new ObjectMetadata();
//               //设置流的长度,您还可以设置其他文件元数据信息
//               objectMetadata.setContentLength(streamLength);
//               s3Client.putObject("your-bucketname","your-objectname", inputStream, objectMetadata)
//            }catch (Exception e){
//               System.out.println(e.getMessage());
//            }

    }

分块上传

package awsnos.awsnos;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest;
import com.amazonaws.services.s3.model.CompleteMultipartUploadResult;
import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest;
import com.amazonaws.services.s3.model.InitiateMultipartUploadResult;
import com.amazonaws.services.s3.model.ListPartsRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PartETag;
import com.amazonaws.services.s3.model.UploadPartRequest;

public class AwsS3 {


    public static void main(String[] args) {

        /**
         * 初始化
         */

        String accessKey = "";
        String secretKey = "";
        AWSCredentials credentials = new BasicAWSCredentials(accessKey,secretKey);
        ClientConfiguration conf = new ClientConfiguration();
        // 设置AmazonS3使用的最大连接数
        conf.setMaxConnections(200);
        // 设置socket超时时间
        conf.setSocketTimeout(10000);
        // 设置失败请求重试次数
        conf.setMaxErrorRetry(2);
        // 如果要用https协议,请加上下面语句
        conf.setProtocol(Protocol.HTTPS);

        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                        .withCredentials(new AWSStaticCredentialsProvider(credentials))
                        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("nos-eastchina1.126.net","us-east-1"))
                        .withClientConfiguration(conf)
                        .build();

        String bucketName = "cloud201411";
        String key = "tomcat111.zip"; //对象名称
        String filePath = "tomcat.zip"; // 文件路径

         File file = new File(filePath);
        long contentLength = file.length();
        long partSize = 5 * 1024 * 1024; // Set part size to 5 MB.
        List<PartETag> partETags = new ArrayList<>();

         try {
            FileInputStream is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         InitiateMultipartUploadRequest  initRequest = new InitiateMultipartUploadRequest(bucketName,key);
         //您还可以在初始化分块上传时,设置文件的Content-Type
         ObjectMetadata objectMetadata = new ObjectMetadata();
         objectMetadata.setContentType("application/zip");
         initRequest.setObjectMetadata(objectMetadata);
         InitiateMultipartUploadResult initResult = s3Client.initiateMultipartUpload(initRequest);
         String uploadId = initResult.getUploadId();


        /**
         * 上传文件
         */

            long filePosition = 0;
            for (int i = 1; filePosition < contentLength; i++) {
                // Last part can be less than 5 MB. Adjust part size.
                partSize = Math.min(partSize, (contentLength - filePosition));
                System.out.println("partNum : " + i + " , partSize : " + partSize);

                // Create request to upload a part.
                UploadPartRequest uploadRequest = new UploadPartRequest()
                        .withBucketName(bucketName).withKey(key)
                        .withUploadId(uploadId).withPartNumber(i)
                        .withFileOffset(filePosition)
                        .withFile(file) //要上传的文件对象
                        .withPartSize(partSize);
                partETags.add(s3Client.uploadPart(uploadRequest).getPartETag());
                filePosition += partSize;
            }

            /**
             * 完成上传
             */

            CompleteMultipartUploadRequest completeRequest =  new CompleteMultipartUploadRequest(
                    bucketName,key, uploadId, partETags);
            CompleteMultipartUploadResult completeResult = s3Client.completeMultipartUpload(completeRequest);

    }
}

操作 AWS S3

看了下 aws s3 的开发文档,发现这个代码完全平移过去改下 ak,sk 和 origin 以endpoint 就可以操作 aws 的对象存储,或者说这个 SDK 就是亚马逊提供的,只不过网易云兼容了 aws 的接口。

package awsnos.awsnos;



import java.security.NoSuchAlgorithmException;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.AmazonS3Encryption;
import com.amazonaws.services.s3.AmazonS3EncryptionClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.CreateBucketRequest;
import com.amazonaws.services.s3.model.CryptoConfiguration;
import com.amazonaws.services.s3.model.CryptoMode;
import com.amazonaws.services.s3.model.CryptoStorageMode;
import com.amazonaws.services.s3.model.EncryptionMaterials;
import com.amazonaws.services.s3.model.EncryptionMaterialsProvider;
import com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider;

public class Aws3 {

    public static void main(String[] args) throws NoSuchAlgorithmException {

        String accessKey = "AKIA*****TGD3B***MDA";
        String secretKey = "GL*******ZFfH5H***gVb/yUO4YA***iBUy8X6U";
        String bucketName = "cloud201411";
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        ClientConfiguration conf = new ClientConfiguration();
        // 设置AmazonS3使用的最大连接数
        conf.setMaxConnections(200);
        // 设置socket超时时间
        conf.setSocketTimeout(10000);
        // 设置失败请求重试次数
        conf.setMaxErrorRetry(2);
        // 如果要用https协议,请加上下面语句
        conf.setProtocol(Protocol.HTTPS);

        // AmazonS3 s3Client = new AmazonS3Client(credentials,clientConfiguration);
        // s3Client.setEndpoint(endPoint);
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withEndpointConfiguration(
                        new AwsClientBuilder.EndpointConfiguration("s3.ap-northeast-1.amazonaws.com", "ap-northeast-1"))
                .withClientConfiguration(conf).build();// endpoint,region请指定为NOS支持的

        /**
         * 创建桶
         */
        boolean exists = s3Client.doesBucketExist(bucketName);
        if (exists) {
            System.out.println("桶已存在");
        } else {
            CreateBucketRequest request = new CreateBucketRequest(bucketName);
            request.setCannedAcl(CannedAccessControlList.PublicRead);
            s3Client.createBucket(request);

        }

        /**
         * 列举桶
         */

        for (Bucket bucket : s3Client.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }

    }

}

AWS S3的 endpoint 以及 origin 参考AWS文档