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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326156867&siteId=291194637