This is going to be a piece of code really useful for many purposes for uploading anything you want to your S3 Bucket, as MySql or PostgreSql backups, or anything else.
the first thing we need to do after created the bucket you are going to set the data, go to AWS Identity and Access Management (IAM) is a web service that helps you securely control access to AWS resources.
Let’s create such user. Go to Services -> IAM. In the navigation pane, choose Users and then choose Add user.
you can download the whole project from GitHub at from https://github.com/juandavidmarin368/Upload-AwsS3Bucket-Java
the first thing we need to do after created the bucket you are going to set the data, go to AWS Identity and Access Management (IAM) is a web service that helps you securely control access to AWS resources.
Let’s create such user. Go to Services -> IAM. In the navigation pane, choose Users and then choose Add user.
Enter user’s name and check Access type ‘Programatic access’. Press next button. We need to add permissions to this user. Press ‘Attach existing policy directly’, in the search field enter ‘s3’ and among found permissions choose AmazonS3FullAccess.
Then press next and ‘Create User’. If you did everything right then you should see an Access key ID and a Secret access key for your user. There is also ‘Download .csv’ button for downloading these keys, so please click on it in order not to loose keys.
Our S3 Bucket configuration is done so let’s proceed to our Maven Java application.
AWS S3 PutObject – In this tutorial, we will learn about how to upload an object to Amazon S3 bucket using java language.
Project Setup
Create a simple maven project in your favorite IDE and add below mentioned dependency in your pom.xml file.
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.533</version>
</dependency>
For latest version of aws library, check this page.
S3 Connection
Create an object of AmazonS3 ( com.amazonaws.services.s3.AmazonS3 ) class for sending a client request to S3. To get instance of this class, we will use AmazonS3ClientBuilder builder class. It requires three important parameters :-
- Region :- It is a region where S3 table will be stored.
- ACCESS_KEY :- It is a access key for using S3. You can generate this key, using aws management console.
- SECRET_KEY :- It is a secret key of above mentioned access key.
Here is a code example :-
AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withRegion(Regions.AP_SOUTH_1)
.withCredentials(new AWSStaticCredentialsProvider
(new BasicAWSCredentials("ACCESS_KEY","SECRET_KEY")))
.build();
Put Object
An AmazonS3.putObject method uploads a new Object to the specified Amazon S3 bucket. The specified bucket must be present in the Amazon S3 and the caller must have Permission.Write permission on the bucket.
The PutObjectRequest object can be used to create and send the client request to Amazon S3. A bucket name, object key, and file or input stream are only information required for uploading the object. This object can optionally upload an object metadata and can also apply a Canned ACL to the new object. Depending on whether a file or input stream is being uploaded, this request has slightly different behavior:-
Uploading File
- The AmazonS3 client automatically computes and validate a checksum of the file.
- AmazonS3 determines the correct content type and content disposition to use for the new object by using the file extension.
Uploading Input Stream
- The content length must be specified before data is uploaded to Amazon S3. If not provided, the library will have to buffer the contents of the input stream in order to calculate it.
Amazon S3 never stores partial objects, if an exception is thrown during an upload operation, the partial objects will get deleted.
Do not forget to set the config.properties close to the final jar AwsS3-Upload-0.0.1-SNAPSHOT-jar-with-dependencies.jar remember to replace inside the config.properties your credentials: accessKey= secretKey= those are the credentiales which were created before in order to have permision over the bucket to tun this, just run mvn clean compile package go to the target folder and paste the config.properties, or move it to the folder you want to run it, but do not forget that config.properties file now the way to use it: 1: first, execute the the jar, as java -jar AwsS3-Upload-0.0.1-SNAPSHOT-jar-with-dependencies.jar 2: give it the first parameter as the bucket name, it does not matter if you have more subfolders inside the bucket just keep in mind that the way to especify more subfolders in is with the character / 3: give it the path of the file you want to upload, if you are using windwos that's the way to do it D:\\filename or C:\\ if you are using linux systems it would be like /yourpath/yourfile example: java -jar AwsS3-Upload-0.0.1-SNAPSHOT-jar-with-dependencies.jar backupsdbmedics/db3 D:\\zpl-zbi2-pm-en-PROGRAMING_GUIDE_ZM400.pdf
you can download the whole project from GitHub at from https://github.com/juandavidmarin368/Upload-AwsS3Bucket-Java
Comments
Post a Comment