【AWS S3工具类】

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);
    }
  }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值