AWS s3云服务存储的介绍和上传和下载

我们项目中如果涉及到海外项目,不可避免会用到AWS s3文件的使用。Amazon Simple Storage Service (Amazon S3) 是一种面向 Internet 的存储服务。下面介绍一下AWS s3的一些主要概念。

存储桶:
存储桶是 Amazon S3 中用于存储对象的容器。每个对象都储存在一个存储桶中。例如,如果名为 photo/test.jpg 的对象存储在存储桶中,申请endPoint为:https://johnsmith.s3.amazonaws.com则可使用 URL https://johnsmith.s3.amazonaws.com/photo/test.jpg 对该对象进行寻址,
在存储桶中存储无限量的数据,每个对象可包含最多 5 TB 的数据.

键:
密钥是指存储桶中对象的唯一标识符。存储桶内的每个对象都只能有一个密钥。
在URL http://doc.s3.amazonaws.com/2019-12-27/test.xml 中,“doc”是存储桶的名称,而“2019-12-27/test.xml”是密钥,秘钥是文件唯一标签。

下面就通过一个demo来展示AWS s3 上传、下载、删除等功能。

1、先引入maven依赖。

 
        
            com.amazonaws
            aws-java-sdk-s3
            1.11.688
        

2、下面是具体例子。

public class AmazonCloudStorageService  {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

   
    private static String accessKey="你的ak值";

  
    private static String secretKey="申请获取的秘钥";

    
    private static String bucketName="申请的桶名";

  

    private static final String SUFFIX = "/";

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    public static void main(String[] args) {
        upload(new File("C:\\Users\\Administrator\\Desktop\\test.xml"));
    }
    public static void upload(File file) {

        long begin = System.currentTimeMillis();

        AmazonS3Client connection = null;
        TransferManager tm = null;

        try {
            // 文件对象路径
          
           String effectKey = "effect" + SUFFIX + "test.xml";


            /*
             * Constructs a client instance
             */
            AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
            ClientConfiguration clientConfiguration = new ClientConfiguration();
            clientConfiguration.setMaxConnections(100);
            clientConfiguration.setSocketTimeout(7200000);
            clientConfiguration.setProtocol(Protocol.HTTPS);
            connection = new AmazonS3Client(credentials, clientConfiguration);

            /*
             * Determine whether the bucket exists
             */
            if (!connection.doesBucketExist(bucketName)) {
                connection.createBucket(bucketName);
            }

            /*
             * Upload an object to your bucket
             */
            tm = new TransferManager(connection);
            // TransferManager processes all transfers asynchronously,
            // so this call returns immediately.



            //如果文件存在获取最新文件,这里需要先判断桶内的键是否存在,如果存在才能下载
            if(connection.doesObjectExist(bucketName,effectKey)){
                String fileName = file.getName();
                String suffixName= fileName.substring(fileName.lastIndexOf(".")+1);
                String unSuffix  = fileName.substring(0, fileName.lastIndexOf("."));
                String slaveFileName=unSuffix+formatter.format(LocalDateTime.now())+"."+suffixName;
                File slaveFile = new File(slaveFileName);
                Download download = tm.download(bucketName, effectKey,slaveFile);
                download.waitForCompletion();

                //上传到备份文件
                String archiveContentsKey = "archive" + SUFFIX + slaveFileName;
                System.out.println("上传到云服务id---------------------------------------:{}"+archiveContentsKey);
                Upload upload = tm.upload(bucketName, archiveContentsKey, slaveFile);
                upload.waitForCompletion();

            }


            //上传到正式文件覆盖原有文件
            Upload upload = tm.upload(bucketName, effectKey, file);
            // Optionally, wait for the upload to finish before continuing
            upload.waitForCompletion();

            /**
             * set acl设置上传文件的权限
             */
            connection.setObjectAcl(bucketName, effectKey, CannedAccessControlList.PublicRead);

            long duration = (System.currentTimeMillis() - begin) / 1000;
         

        } catch (InterruptedException e) {
          
            throw new RuntimeException("upload to s3 failed.");
        } finally {
            if (tm != null) {
                tm.shutdownNow();
            }
            if (connection != null) {
                connection.shutdown();
            }
            
        }
    }
}

上传成功之后可以通过给到endPoint,例如:endPoint为:https://johnsmith.s3.amazonaws.com ,那么就可以endPoint为:https://johnsmith.s3.amazonaws.com/ effect/test.xml访问到文件。
https://johnsmith.s3.amazonaws.com/ archive/test20191228001023.xml访问到备份文件

你可能感兴趣的:(云服务)