Skip to content

Commit

Permalink
create file and test with snippets to use on this page of the Java De…
Browse files Browse the repository at this point in the history
…v Guide: https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/transfer-manager.html

checkstyle test added, which required modifying other existing files

dependencies updated
  • Loading branch information
tkhill-AWS committed Oct 28, 2022
1 parent 9eb886e commit 4e56eb4
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 48 deletions.
108 changes: 80 additions & 28 deletions javav2/example_code/s3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,29 @@
<artifactId>S3J2Project</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<java.version>1.8</java.version>
<aws.java.sdk.version>2.17.272</aws.java.sdk.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.17.230</version>
<version>${aws.java.sdk.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.19.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -42,29 +39,26 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4.1</version>
<version>2.13.4.2</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.8.2</version>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -74,7 +68,7 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3-transfer-manager</artifactId>
<version>2.17.103-PREVIEW</version>
<version>${aws.java.sdk.version}-PREVIEW</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
Expand All @@ -84,10 +78,68 @@
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3control</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
</dependency>

<!-- The following is needed for Apache HttpClient wire logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
</dependency>
</dependencies>
</project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<excludes>
<exclude>**/AmazonS3Test.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<configLocation>src/main/resources/checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
package com.example.s3;

// snippet-start:[s3.java2.s3_object_check_integrity.import]
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;

import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;
import software.amazon.awssdk.services.s3.model.DeleteBucketRequest;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.S3Object;
// snippet-end:[s3.java2.s3_bucket_ops.delete_bucket.import]
// snippet-end:[s3.java2.bucket_deletion.import]
// snippet-start:[s3.java2.bucket_deletion.main]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//snippet-sourcedescription:[FileUpDownOps.java demonstrates how to upload and download an object to an Amazon Simple Storage Service (Amazon S3) bucket.]
//snippet-keyword:[AWS SDK for Java v2]
//snippet-service:[Amazon S3]
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package com.example.s3.transfermanager;

// snippet-start:[s3.tm.java2.file_up_down_ops.import]

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.transfer.s3.CompletedFileDownload;
import software.amazon.awssdk.transfer.s3.CompletedFileUpload;
import software.amazon.awssdk.transfer.s3.FileDownload;
import software.amazon.awssdk.transfer.s3.FileUpload;
import software.amazon.awssdk.transfer.s3.S3ClientConfiguration;
import software.amazon.awssdk.transfer.s3.S3TransferManager;

import java.nio.file.Paths;

import static software.amazon.awssdk.transfer.s3.SizeConstant.MB;

public class FileUpDownOps {

private String bucketName;
private String key;
private String filePath;
private String downloadedFileName;

public FileUpDownOps(String bucketName, String key, String filePath, String downloadedFileName) {
this.bucketName = bucketName;
this.key = key;
this.filePath = filePath;
this.downloadedFileName = downloadedFileName;
}

public String fileUploadDownload() {

// snippet-start:[s3.tm.java2.file_up_down_ops.createclients]
S3ClientConfiguration s3ClientConfiguration =
S3ClientConfiguration.builder()
.region(Region.US_EAST_1)
.minimumPartSizeInBytes(10 * MB)
.targetThroughputInGbps(20.0)
.build();

S3TransferManager s3TransferManager = S3TransferManager.builder()
.s3ClientConfiguration(s3ClientConfiguration)
.build();
// snippet-end:[s3.tm.java2.file_up_down_ops.createclients]

// snippet-start:[s3.tm.java2.file_up_down_ops.fileupload]
FileUpload fileUpload = s3TransferManager.uploadFile(uploadFileRequestBuilder ->
uploadFileRequestBuilder
.putObjectRequest(
putObjectRequestBuilder -> putObjectRequestBuilder.bucket(bucketName).key(key))
.source(Paths.get(filePath)));
// snippet-end:[s3.tm.java2.file_up_down_ops.fileupload]

// snippet-start:[s3.tm.java2.file_up_down_ops.fileuploadresult]
CompletedFileUpload uploadResult = fileUpload.completionFuture().join();
System.out.println("Etag: " + uploadResult.response().eTag());
// snippet-end:[s3.tm.java2.file_up_down_ops.fileuploadresult]

// snippet-start:[s3.tm.java2.file_up_down_ops.filedownload]
FileDownload downloadFile = s3TransferManager.downloadFile(
downloadFileRequestBuilder -> downloadFileRequestBuilder
.getObjectRequest(getObjectRequestBuilder ->
getObjectRequestBuilder.bucket(bucketName).key(key))
.destination(Paths.get(downloadedFileName)));
// snippet-end:[s3.tm.java2.file_up_down_ops.filedownload]

// snippet-start:[s3.tm.java2.file_up_down_ops.filedownloadresult]
CompletedFileDownload downloadResult = downloadFile.completionFuture().join();
System.out.println("Content length: " + downloadResult.response().contentLength());
// snippet-end:[s3.tm.java2.file_up_down_ops.filedownloadresult]

return uploadResult.response().eTag() + "|" + downloadResult.response().contentType();
}
}
25 changes: 25 additions & 0 deletions javav2/example_code/s3/src/main/resources/checkstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" ?>

<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.2//EN"
"https://checkstyle.org/dtds/configuration_1_2.dtd">

<module name="Checker">
<module name="TreeWalker">

<module name="IllegalCatch"/>
<module name="EmptyStatement"/>
<module name="AvoidStarImport"/>
<module name="UnusedImports"/>
<module name="ParameterAssignment"/>
<module name="LocalVariableName"/>
<module name="NoLineWrap"/>
<module name="SingleSpaceSeparator"/>
<module name="MethodName"/>
<module name="MemberName"/>
<module name="LocalVariableName"/>
<module name="LeftCurly"/>
<module name="RightCurly"/>
<module name="DeclarationOrder"/>
</module>
</module>
Empty file.
Loading

0 comments on commit 4e56eb4

Please sign in to comment.