java对接上传AWS的S3服务

话不多说上干货

首先需要引入aws的maven包


    com.amazonaws
    aws-java-sdk-s3
    1.12.292

证书申请

https://console.aws.amazon.com/iam/

java对接上传AWS的S3服务_第1张图片

 

代码示例(工具类)

    private static String access_key_id = "申请的证书id";
    private static String secret_key_id = "申请的证书id";
    private static String bucket_name = "你S3的桶名称";
    private static AmazonS3 s3Client;
    private static List object_list = new ArrayList<>();
    static {
        //通过sdk创建与S3的链接
        BasicAWSCredentials awsCredentials = new BasicAWSCredentials(access_key_id, secret_key_id);
        //默认地区新加坡
        s3Client = AmazonS3Client.builder()
                .withRegion(Regions.AP_SOUTHEAST_1)//可以根据自己的需求进行选择
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
            .build();

    }

        

//创建桶
public static Bucket createBucket() {
    Bucket b = null;
    if (s3Client.doesBucketExistV2(bucket_name)) {
        System.out.format("Bucket %s already exists.\n", bucket_name);
        b = getBucket(bucket_name);
    } else {
        try {
            b = s3Client.createBucket(bucket_name);
        } catch (AmazonS3Exception e) {
            System.err.println(e.getErrorMessage());
        }
    }
    return b;
}

//获取桶
public static Bucket getBucket(String bucket_name) {
    Bucket named_bucket = null;
    List buckets = s3Client.listBuckets();
    for (Bucket b : buckets) {
        System.err.println(b.getName());
        if (b.getName().equals(bucket_name)) {
            named_bucket = b;
        }
    }
    return named_bucket;
}

//上传文件到桶
public static void PutObject(String file_path){
    String key_name = Paths.get(file_path).getFileName().toString();
    try {
        s3Client.putObject(bucket_name, key_name, new File(file_path));
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
    }
    System.err.println("Done!");
}

//删除对象
public static void DeleteObject(String object_key){
    try {
        s3Client.deleteObject(bucket_name, object_key);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
    }
    System.err.println("Done!");
}

//删除桶下的所有对象
public static void DeleteObjectALL() {
    try {
        String[] object_keys = QueryObjectALL();
        DeleteObjectsRequest dor = new DeleteObjectsRequest(bucket_name)
                .withKeys(object_keys);;
        s3Client.deleteObjects(dor);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
    }
    System.err.println("Done!");
}

//上传json到桶(特性化处理上传到文件夹)
public static void PutJsonString(String file_name,String content){
    try {

        s3Client.putObject(bucket_name, file_name, content);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
    }
    System.err.println("Done!");
}

//查询指定桶下所有的对象
public static String[] QueryObjectALL(){
    ListObjectsV2Result result = s3Client.listObjectsV2(bucket_name);
    List objects = result.getObjectSummaries();
    for (S3ObjectSummary os : objects) {
        object_list.add(os.getKey());
        System.err.println("* " + os.getKey());
    }
    String[] object_keys = object_list.stream().map(el->el.toString()).toArray(String[]::new);
    object_list.clear();
    return object_keys;
}

//下载指定文件(key_name桶名)
public static void GetObject(String key_name){
    try {
        S3Object o = s3Client.getObject(bucket_name, key_name);
        S3ObjectInputStream s3is = o.getObjectContent();
        FileOutputStream fos = new FileOutputStream(new File(key_name));
        byte[] read_buf = new byte[1024];
        int read_len = 0;
        while ((read_len = s3is.read(read_buf)) > 0) {
            fos.write(read_buf, 0, read_len);
        }
        s3is.close();
        fos.close();
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    } catch (FileNotFoundException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}

//删除指定桶下的指定对象
public static void DeleteAssignObject(String appBucket, String appName) {
    appName = "public/" + appName;
    try {
        s3Client.deleteObject(appBucket, appName);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
    }
    System.err.println("Done!");
}

//上传指定json到桶(特性化处理上传到文件夹)
public static void PutAssignJsonString(String appBucket, String file_name,String content){
    try {
        s3Client.putObject(appBucket, file_name, content);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
    }
    System.err.println("Done!");
}
public static void main(String[] args) {
    //创建桶
    createBucket();
    //上传文件
    PutObject("上传文件路径");
    //删除文件
    DeleteObject("111.jpg");
    //上传json文件
    PutJsonString("文件名","{\"result\":{\"status\":false,\"status_code\":010,\"response\":\"Invalid PIN\"}}");
    //删除桶下所有的对象
    DeleteObjectALL();
    // 查询指定桶下所有的对象
    QueryObjectALL();
    //删除指定桶下的指定对象测试
    DeleteAssignObject("桶名","对象名称");
    //指定桶上传json文件
    PutAssignJsonString("桶名","上传json文件名称", "{\"result\":{\"status\":false,\"status_code\":010,\"response\":\"Invalid PIN\"}}");
}

根据我的理解  s3如果要在某个桶的文件夹下上传文件,可以将文件名称前面添加

filename = “文件夹名称/” + filename ;即可

你可能感兴趣的:(java,aws,servlet)