`
sillycat
  • 浏览: 2552392 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Java and Amazon S3

 
阅读更多
Java and Amazon S3

For S3 client, it should be straightforward, just need to pay attention to 5G file size limitation.
pom.xml as follow:
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-s3</artifactId>
      <version>${aws.java.sdk.s3.version}</version>
    </dependency>
<aws.java.sdk.s3.version>1.11.148</aws.java.sdk.s3.version>
is that the latest version? I do not know, but it is already in my POM.xml

I have a interface which will be shared with S3FileOperation and FtpFileOperation
package com.sillycat.transmission;

public interface Transmission
{
  public boolean uploadFile( String bucketName, String key, String fileToUpload );
  public boolean downloadFile( String bucketName, String key, String destFolder );
}

The implementation code class is as follow:
package com.sillycat.transmission;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.transfer.Download;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import com.amazonaws.services.s3.transfer.Upload;

public class TransmissionS3Impl implements Transmission
{

  protected final Logger logger = LoggerFactory.getLogger( this.getClass() );

  private TransferManager transferManager;

  public TransmissionS3Impl()
  {
    logger.info( "TransmissionS3 start to init the AWS S3 parameters--------" );
    String key = System.getProperty( "aws.s3.key" );
    String secret = System.getProperty( "aws.s3.secret" );
    logger.info( "get aws.s3.key=" + key );
    BasicAWSCredentials awsCreds = new BasicAWSCredentials( key, secret );
    AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials( new AWSStaticCredentialsProvider( awsCreds ) )
      .withRegion( Regions.US_EAST_1 ).build();
    TransferManager transferManager = TransferManagerBuilder.standard().withS3Client( s3Client ).build();
    this.transferManager = transferManager;
    logger.info( "TransmissionS3 success init ------------------------------" );
  }

  @Override
  public boolean uploadFile( String bucketName, String key, String fileToUpload )
  {
    boolean result = false;
    logger.debug( "TransmissionS3 upload file bucketName = " + bucketName + " key = " + key + " fileToUpload = "
      + fileToUpload );
    try
    {
      Upload upload = transferManager.upload( bucketName, key, new File( fileToUpload ) );
      upload.waitForCompletion();
      logger.debug( "TransmissionS3 upload file done." );
      result = true;
    }
    catch ( AmazonClientException | InterruptedException e )
    {
      logger.error( "AmazonClientException | InterruptedException:", e );
    }
    return result;
  }

  @Override
  public boolean downloadFile( String bucketName, String key, String destFile )
  {
    boolean result = false;
    logger.debug(
      "TransmissionS3 download file bucketName = " + bucketName + " key = " + key + " destFile = " + destFile );
    try
    {
      Download download = transferManager.download( bucketName, key, new File( destFile ) );
      download.waitForCompletion();
      logger.debug( "TransmissionS3 download file done." );
      result = true;
    }
    catch ( AmazonClientException | InterruptedException e )
    {
      logger.error( "AmazonClientException | InterruptedException:", e );
    }
    return result;
  }

}

I agree with my colleagues, it will connect to S3, so it should be integration testing, so here is the test class
package com.sillycat.transmission;

import java.io.File;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
* how to run: mvn -Dtest=TransmissionS3JUnitIntegrationTest test
* @author carl
*
*/
public class TransmissionS3JUnitIntegrationTest
{

  private Transmission transmission;

  String bucketName = “sillycat-test";

  String key = "developer/test/mock.txt";

  @Before
  public void setUp() throws Exception
  {
    System.setProperty( "aws.s3.key", “xxxxxxx" );
    System.setProperty( "aws.s3.secret", “xxxxxxxxxxx" );
    transmission = new TransmissionS3Impl();
  }

  @Test
  public void uploadFile()
  {
    String localFilePath = this.getClass().getResource( "mock.txt" ).getPath();
    boolean result = transmission.uploadFile( bucketName, key, localFilePath );
    Assert.assertTrue( result );
  }

  @Test
  public void downloadFile()
  {
    String localFilePath = this.getClass().getResource( "mock.txt" ).getPath();
    localFilePath = localFilePath.replace( "mock.txt", "mock1.txt" );
    boolean result = transmission.downloadFile( bucketName, key, localFilePath );
    Assert.assertTrue( result );
    Assert.assertTrue( "Download is failing, where is your file.", ( new File( localFilePath ) ).exists() );
  }

}



References:
https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/java/example_code/s3/src/main/java/aws/example/s3/XferMgrDownload.java
https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-s3-transfermanager.html#tranfermanager-download-directory
https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/java/example_code/s3/src/main/java/aws/example/s3/XferMgrUpload.java

分享到:
评论

相关推荐

    springboot集成amazon aws s3对象存储sdk(javav2)

    而Amazon S3(Simple Storage Service)是AWS提供的一个云存储服务,用于存储和检索任何数量的数据,无论何时何地。本篇文章将深入探讨如何在SpringBoot项目中集成AWS S3 SDK(Java V2版本),实现对象的分页列表、...

    aws-java-sdk-s3-1.12.384.jar中文-英文对照文档.zip

    aws-java-sdk-s3-***.jar中文-英文对照文档.zip,java,aws-java-sdk-s3-***.jar,com.amazonaws,aws-java-sdk-s3,***,com.amazonaws.auth,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,amazonaws,aws,sdk...

    Amazon S3创建存储公私桶步骤!

    Amazon Simple Storage Service (S3) 是由亚马逊网络服务 (AWS) 提供的一种面向互联网的对象存储服务,它提供了几乎无限的存储空间。S3 的设计目的是为了支持大规模的数据存储需求,适用于各种场景,包括备份与存档...

    S3-jets3t交互入门

    Jets3t(Java Toolkit for Amazon S3 and CloudFront)是一个开源的Java库,它为开发者提供了一种简单的方式来与S3进行交互,包括上传、下载、管理对象以及处理权限等操作。本教程旨在帮助新手快速入门S3-jets3t的...

    MinIO Client SDK for Java

    MinIO Java SDK is Simple Storage Service (aka S3) client to perform bucket and object operations to any Amazon S3 compatible object storage service. MinIO Java SDK是简单存储服务(又称S3)的客户端,...

    aws-java-sdk-1.2.12下载

    AWS Java SDK 1.2.12 是一个用于与亚马逊Web服务(Amazon Web Services, AWS)进行交互的软件开发工具包,特别适用于Java开发者。这个版本的SDK包含了多个示例项目,可以帮助开发者深入理解如何利用AWS的各项服务。...

    Practical Amazon EC2, SQS, Kinesis, and S3_ A Hands-On Approach to AWS-2017

    《Practical Amazon EC2, SQS, Kinesis, and S3: A Hands-On Approach to AWS-2017》是一本面向实践的指南,旨在帮助读者深入理解和掌握Amazon Web Services (AWS) 中的核心服务,包括Amazon Elastic Compute Cloud ...

    blobs-s3:使用 Amazon S3 实现 blob API

    在IT行业中,Amazon S3(Simple Storage Service)是亚马逊提供的一种云存储服务,它允许开发者和企业存储和检索大量数据。Blob(Binary Large Object)通常指的是在数据库中存储的二进制大对象,如图像、视频或文档...

    S3修改

    - **AWS SDKs**:Amazon提供了多种编程语言的SDK,如Python的boto3、Java的AWS SDK for Java等,这些SDK允许开发者通过源码直接与S3交互,进行对象的修改操作。 - **AWS CLI**:命令行界面工具,可以方便地执行S3...

    Java_亚马逊网络服务AWS指南了解有关亚马逊网络服务工具、服务和认证的所有信息.zip

    亚马逊网络服务(Amazon Web Services,简称AWS)是全球领先的云服务提供商,为开发者和企业提供了一系列广泛的服务,包括计算、存储、数据库、分析、机器学习、物联网等。Java作为一门广泛应用的编程语言,与AWS的...

    s3_signed_url:生成签名的URL以获取或放置S3对象

    s3_signed_url s3_signed_url生成签名的URL,以获取或放置S3对象。 我们的后端在有描述。 安装 将此行添加到您的应用程序的Gemfile中: gem 's3_signed_url' 然后执行: ...https://s3.amazonaws.co

    AWS SDK for Java 开发人员指南1

    这个SDK提供了对多种AWS服务的访问,如Amazon S3(简单存储服务)、Amazon EC2(弹性计算云)、DynamoDB(数据库服务)等,通过简洁的API调用来实现这些服务的功能。 **开发工具包版本 2** AWS SDK for Java 版本2...

    amazon 开源的API

    最后,Amazon IAM(Identity and Access Management)API是管理用户、组和权限的关键工具。通过IAM,开发者可以精细控制谁可以访问哪些AWS资源,以及他们可以执行的操作。这有助于实施安全策略,确保合规性。 综上...

    登录亚马逊账户apk

    - AWS Mobile SDK:这是一个综合的SDK,包含了多个亚马逊Web服务,如S3(存储服务)、DynamoDB(数据库服务)等,可能用于存储用户偏好或者账户相关数据。 - Login with Amazon:这是亚马逊提供的一种身份服务,允许...

    Amazonapi

    描述中的“Send query to Amazon Web service and returns XML”进一步解释了该工具的主要功能。它执行的是API调用,这在AWS中很常见,因为AWS提供了大量的服务,如S3(Simple Storage Service)、EC2(Elastic ...

    Amazon Web Services for Mobile

    亚马逊网络服务(AWS)为移动开发者提供了一套丰富的云服务,以支持iOS和Android等移动平台的应用程序开发。AWS Mobile是AWS中专门为移动应用优化的服务集合,让开发者能利用云基础设施、数据库、消息推送、身份认证...

    Big+Data+Analytics+with+Java-Packt+Publishing(2017).pdf

    Apache Spark or Tez or distributed filesystems such as HDFS and Amazon S3 or real-time SQL on underlying data using Impala or Spark SQL. This book provides a lot of information on big data ...

    AWS开发SDK

    AWS开发SDK是指亚马逊网络服务(Amazon Web Services,简称AWS)推出的软件开发工具包,特别是针对Java语言的版本。AWS是目前全球最大的云服务提供商,提供众多的云服务模块,包括计算、存储、数据库、分析、机器...

Global site tag (gtag.js) - Google Analytics