Upload and download notes in java on S3

Introduction

It is a memo of uploading and downloading from java to S3. In the case of proxy environment, if proxy is not set, it will time out. Other than that, it was made with the official sample.

I would like to investigate the details and add them.

Introducing jar (maven)

pom.xml


    <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.11.112</version>
    </dependency>

Declaration of constants

java


	static final String S3_ACCESS_KEY			= "";
	static final String S3_SECRET_KEY			= "";
	static final String S3_SERVICE_END_POINT	= "";
	static final String S3_REGION				= "";
	static final String S3_BUCKET_NAME			= "";

Authentication process

java


    //Authentication process
    private static AmazonS3 auth(){
    	System.out.println("auth start");

    	//AWS credentials
    	AWSCredentials credentials = new BasicAWSCredentials(S3_ACCESS_KEY, S3_SECRET_KEY);

    	//Client settings
    	ClientConfiguration clientConfig = new ClientConfiguration();
    	clientConfig.setProxyHost("[proxyHost]");
    	clientConfig.setProxyPort([portNo]);

    	//Endpoint setting
    	EndpointConfiguration endpointConfiguration = new EndpointConfiguration(S3_SERVICE_END_POINT,  S3_REGION);

    	//Generate S3 access client
    	AmazonS3 client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials))
    			.withClientConfiguration(clientConfig)
    			.withEndpointConfiguration(endpointConfiguration).build();

    	System.out.println("auth end");
    	return client;
    }

Upload process

java


    //Upload processing
    private static void upload() throws Exception{
    	System.out.println("upload start");

        //Authentication process
    	AmazonS3 client = auth();

    	File file = new File("[Upload file path]");
    	FileInputStream fis = new FileInputStream(file);


    	ObjectMetadata om = new ObjectMetadata();
    	om.setContentLength(file.length());

    	final PutObjectRequest putRequest = new PutObjectRequest(S3_BUCKET_NAME, file.getName(), fis, om);

    	//Permission settings
    	putRequest.setCannedAcl(CannedAccessControlList.PublicRead);

    	//upload
    	client.putObject(putRequest);

    	fis.close();

    	System.out.println("upload end");
    }

Download process

java


    //Download processing
    private static void download() throws Exception{
    	System.out.println("download start");

        //Authentication process
    	AmazonS3 client = auth();

    	final GetObjectRequest getRequest = new GetObjectRequest(S3_BUCKET_NAME, "[Download file name");

    	S3Object object = client.getObject(getRequest);

    	FileOutputStream fos = new FileOutputStream(new File("[Output destination path]"));
    	IOUtils.copy(object.getObjectContent(), fos);

    	fos.close();

    	System.out.println("download end");
    }

Recommended Posts

Upload and download notes in java on S3
Java upload and download notes to Azure storage
Notes on signal control in Java
Notes on Java path and Package
[Java] Test S3 upload / download using "S3 ninja"
Notes on how to use regular expressions in Java
[Java] Upload images and base64
[Note] Download from S3, upload to S3
[Java] Basic types and instruction notes
StringBuffer and StringBuilder Class in Java
Deleting AWS S3 Objects in Java
Renamed folders in AWS S3 (Java)
Understanding equals and hashCode in Java
Enable Java 8 and Java 11 SDKs on Ubuntu
Notes on Android (java) thread processing
Install Java 9 on windows 10 and CentOS 7
Hello world in Java and Gradle
Notes on building Kotlin development environment and migrating from Java to Kotlin
Difference between final and Immutable in Java
A note on the differences between interfaces and abstract classes in Java
Notes on operators using Java ~ String type ~
[Java] for Each and sorted in Lambda
Notes on expand () and collapse () of Expandablerecyclerview
Arrylist and linked list difference in java
Notes on Java's Stream API and SQL
Notes on JSP extension tags in SpringFrameWork
Learn Flyweight patterns and ConcurrentHashMap in Java
Java Direction in C ++ Design and Evolution
Java to C and C to Java in Android Studio
Reading and writing gzip files in Java
Difference between int and Integer in Java
Discrimination of Enums in Java 7 and above
Java: Download the file and save it in the location selected in the dialog [Use HttpClient]
Regarding the transient modifier and serialization in Java
Create barcodes and QR codes in Java PDF
Put Java 11 and spring tool suite on mac
Detect similar videos in Java and OpenCV rev.2
Review notes for Java 1.7 and later file copies
Publish MySQL externally and log in on Ubuntu
Parallel and parallel processing in various languages (Java edition)
Deserialize CSV in Java based on header name
Difference between next () and nextLine () in Java Scanner
Differences in writing Java, C # and Javascript classes
Capture and save from selenium installation in Java
Add, read, and delete Excel comments in Java
Check static and public behavior in Java methods
[Java] Understand in 10 minutes! Associative array and HashMap
Basics of threads and Callable in Java [Beginner]
Compile and run Java on the command line
Distinguish between positive and negative numbers in Java
Java adds and removes watermarks in word documents
Detect similar videos in Java and OpenCV rev.1
Represents "next day" and "previous day" in Java / Android
Questions in java exception handling throw and try-catch
Encrypt / decrypt with AES256 in PHP and Java
Generate OffsetDateTime from Clock and LocalDateTime in Java
Notes on how to write comments in English
Partization in Java
Java Generics (Notes)
Changes in Java 11
[Java] Array notes