AWS S3工具类

Posted gamedev˚

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
    
  

 

以上是关于AWS S3工具类的主要内容,如果未能解决你的问题,请参考以下文章

无法在 go 中使用 AWS 开发工具包获取 s3.Object ACL

如何使用 AWS 开发工具包从 S3 下载对象 - RESTful

AWS S3 同步与 JS/节点开发工具包

使用 AWS 开发工具包从 Amazon S3 下载文件后如何删除文件

AWS iOS 开发工具包:在两个不同区域同时使用 S3 和移动分析

使用 AWS Java 开发工具包为现有 S3 对象设置 Expires 标头