Java and Amazon S3

本文介绍使用Java实现Amazon S3文件上传与下载的功能,并提供了一个简单的接口定义与具体实现。此外,还通过JUnit进行了集成测试确保功能正确性。
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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值