一 .Amazon S3介绍

Amazon Simple Storage Service (Amazon S3) 是一种对象存储,它具有简单的 Web 服务接口,可用于在 Web 上的任何位置存储和检索任意数量的数据。它能够提供 99.999999999% 的持久性,并且可以在全球大规模传递数万亿对象。

客户使用 S3 作为云原生应用程序的主要存储;作为分析的批量存储库或“数据湖”;作为备份和恢复以及灾难恢复的目标;并将其与无服务器计算配合使用。

使用 Amazon 的云数据迁移选项,客户可以轻松地将大量数据移入或移出 Amazon S3。数据在存储到 S3 中之后,会自动采用成本更低、存储期限更长的云存储类 (如 S3 Standard – Infrequent Access 和 Amazon Glacier) 进行存档。

    <!--亚马逊SDK-->
        <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.11.505</version>
        </dependency>
package com.xf.common.utils.amazonS3;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * @Author: xf
 * @Date: 2019/2/26 14:03
 * @Version 1.0
 */
public class AmazonS3UploadUtil {
    static AmazonS3 s3;
    private static String AWS_ACCESS_KEY = "XXX";
    private static String AWS_SECRET_KEY = "XXX";
    private static final String SUFFIX = "/";

    static {
        // credentials object identifying user for authentication
        // user must have AWSConnector and AmazonS3FullAccess for
        // this example to work
        AWSCredentials credentials = new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY);
        // create a client connection based on credentials
        s3 = new AmazonS3Client(credentials);
        Region usWest2 = Region.getRegion(Regions.US_EAST_1);//美国东部(弗吉尼亚州北部)
        s3.setRegion(usWest2);
    }

    // create bucket - name must be unique for all S3 users
    public static void createBucket() {
        String bucketName = "bucket-xiefei-test";
        s3.createBucket(bucketName);
    }

    //list buckets
    public static void listBuckets() {
        for (Bucket bucket : s3.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
    }

    // create folder into bucket
    public static void createFolder() {
        String bucketName = "bucket-xiefei-test";
        String folderName = "images-test";
        createFolder(bucketName, folderName, s3);
    }

    // upload file to folder and set it to public
    public static void uploadFile() {
        String bucketName = "bucket-xiefei-test";
        String folderName = "images-test";
        String fileName = folderName + SUFFIX + "taylor.jpg";
        s3.putObject(new PutObjectRequest(bucketName, fileName,
                new File("C:\\Users\\xiefei\\Pictures\\taylor.jpg"))
                .withCannedAcl(CannedAccessControlList.PublicRead));
    }

    // deletes bucket
    public static void deleteBucket() {
        String bucketName = "bucket-xiefei-test";
        s3.deleteBucket(bucketName);
    }

    //To delete files use:
    public static void deleteFiles() {
        String bucketName = "bucket-xiefei-test";
        String fileName = "taylor.jpg";
        s3.deleteObject(bucketName, fileName);

    }

    public static void createFolder(String bucketName, String folderName, AmazonS3 client) {
        // create meta-data for your folder and set content-length to 0
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(0);
        // create empty content
        InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
        // create a PutObjectRequest passing the folder name suffixed by /
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, folderName + SUFFIX, emptyContent, metadata);
        // send request to S3 to create folder
        client.putObject(putObjectRequest);
    }

    /**
     * This method first deletes all the files in given folder and than the
     * folder itself
     */
//    public static void deleteFolder(String bucketName, String folderName, AmazonS3 client) {
//        List fileList = client.listObjects(bucketName, folderName).getObjectSummaries();
//        for (S3ObjectSummary file : fileList) {
//            client.deleteObject(bucketName, file.getKey());
//        }
//        client.deleteObject(bucketName, folderName);
//    }
    public static void main(String[] args) {
        deleteFiles();
    }

}

 

参考:

https://javatutorial.net/java-s3-example

https://aws.amazon.com/cn/s3/getting-started/