【AWS S3工具类】

干照
2023-12-01

AWS S3 SDK接入

参考资料:

根据我目前的业务,这里有一个需求,一个是在指定S3存储桶中查看文件目录,另外一个是列出S3中所有的存储桶资源。这里的代码片段中,比较重要的是第一个需求,如何查看存储桶中指定目录层级下的所有文件目录。

package com.nkm.deploy.service;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.nkm.deploy.common.enumPojo.RegionEnum;
import com.nkm.deploy.common.utils.AWSUtil;

/**
 * 
 * @ClassName: S3Service
 * @Description: Describes S3 Object and Some methods of operation associated with an AWS account
 * @author <a href="https://longshilin.com">LongShiLin</a>
 * @date 2019年1月7日
 *
 */
public class S3Service extends BaseService {

  private Logger logger = LoggerFactory.getLogger(S3Service.class.getName());
  private AmazonS3 s3Client = null;

  public void initS3() {
    try {
      AWSCredentials awsCredentials = AWSUtil.getAWSCredentials(RegionEnum.NA.getType());
      if (awsCredentials != null) {
        s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).withRegion(Regions.US_EAST_1).build();
      } else {
        return;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  // List the buckets in your account
  public void listAllBuckets() {
    System.out.println("Listing buckets");
    for (Bucket bucket : s3Client.listBuckets()) {
      System.out.println(" - " + bucket.getName());
    }
    System.out.println();
  }

  // 获取存储桶中的文件目录,可以指定为任意子目录级
  /**
   */
  public List<String> listKeysInBucket(String bucketName, String prefix) {
    Boolean isTopLevel = false;
    String delimiter = "/";
    if (prefix == "" || prefix == "/") {
      isTopLevel = true;
    }
    if (!prefix.endsWith(delimiter)) {
      prefix += delimiter;
    }

    ListObjectsRequest listObjectsRequest = null;
    if (isTopLevel) {
      listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withDelimiter(delimiter);
    } else {
      listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(prefix).withDelimiter(delimiter);
    }
    ObjectListing objects = s3Client.listObjects(listObjectsRequest);
    return objects.getCommonPrefixes();
  }

}

对于上面的工具代码,这里写了一个测试类,可以对其进行测试:

public class AWSTest {
  @Resource
  S3Service s3Service;

  /**
   * 测试单元:AWS S3服务
   */
  @Test
  public void s3Test() {
    s3Service.listAllBuckets();

    List<String> listKeysInDirectory = s3Service.listKeysInBucket("bucket1", "middle-east");
    String regEx = "^201[0-9]{7}$";
    Pattern pat = Pattern.compile(regEx);
    String temp = "";
    System.out.println("Result:");
    for (String key : listKeysInDirectory) {
      // System.out.println(key);
      temp = key.replace("middle-east", "").replace("/", "");
      if (pat.matcher(temp).find())
        System.out.println(temp);
    }
    List<String> listKeysInDirectory1 = s3Service.listKeysInBucket("bucket1", "amazon");
    System.out.println("Result:");
    for (String key : listKeysInDirectory1) {
      // System.out.println(key);
      temp = key.replace("amazon", "").replace("/", "");
      if (pat.matcher(temp).find())
        System.out.println(temp);
    }
    List<String> listKeysInDirectory2 = s3Service.listKeysInBucket("bucket1", "/");
    System.out.println("Result:");
    for (String key : listKeysInDirectory2) {
      // System.out.println(key);
      temp = key.replace("/", "").replace("/", "");
      if (pat.matcher(temp).find())
        System.out.println(temp);
    }
  }

 

 类似资料: