Amazon S3 File Upload Api Codes
The following C# example uploads a file to an S3 bucket using the low-level multipart upload API. For information about the example's compatibility with a specific version of the AWS SDK for.NET and instructions for creating and testing a working sample, see Running the Amazon S3.NET Code Examples. Provides code samples of uploading objects using POST through your browser and using the signature calculation in AWS Signature Version 4. The following POST policy supports uploads to Amazon S3 with specific conditions. You can then upload image files to the specific bucket using the exampleform.html.
This post gives a solution to a very rare use case where you want to use cURL from Java code to upload a file to Amazon S3 bucket.
Amazon S3
Amazon Simple Storage Service is storage for the Internet. It is designed to make web-scale computing easier for developers. Amazon S3 has a simple web services interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web.
Upload to Amazon S3 with Java
Amazon S3 has very good documentation how to use their Java client in order to upload or retrieve files. Ways described in the manual are:
Upload File To Amazon S3
- Upload in a single operation – few lines of code to instantiate AmazonS3Client object and to upload the file in one chunk.
- Upload using multipart upload API – provides the ability to upload a file into several chinks. It is possible to use low and high-level operations on the upload.
- Upload using pre-signed URLs – with this approach you can upload to some else’s bucket with having access key and shared key.
More on each of the approaches can be found in Amazon S3 upload object manual.
Upload with cURL
cURL is widely used and powerful tool for data transfer. It supports various protocols and functionalities. In Windows world, it is not widely used, but there are cURL implementations for Windows which can be used. Uploading to Amazon S3 bucket with cURL is pretty easy. There are bash scripts that can do it. One such is described in File Upload on Amazon S3 server using CURL request post. It is the basis I took to create the Java code for upload.
Upload with cURL and Java
Upload with Java and cURL is a pretty rare case. Benefits of using this approach are memory and CPU optimization. If an upload is done through Java code file to be uploaded is read and stored in heap. This reading is optimized and parts already uploaded part is removed from the heap. Anyway reading and removing the not needed file parts requires memory to keep it and CPU for garbage collection, especially when a huge amount of data is to be transferred. In some cases where resources and performance are absolutely important this memory and CPU usage can be critical. cURL also uses memory to upload the file, but this becomes no longer problem of the JVM, rather than a problem of the OS. Upload Java code is:
Conclusion
Upload to Amazon S3 with cURL from Java code is a rare case, which could be beneficial in the case where memory and CPU usage by JVM is crucial. Delegating file upload to cURL does not disturb JVM heap and Garbage Collection process.

Introduction
As a developer, i am pretty sure that you may have come across with scenarios where you need to store images (either user uploaded or application itself) of your application. There are several possibilities to store file(s) as follows.
- Store the file(s) somewhere in the hosting server where the application is deployed (if it is a web application).
- Store the file(s) in the database as binary files.
- Store the file using cloud storage services.
Here we are going to evaluate the third option (given above) which is “Store the file using cloud storage services“.
Amazon Simple Storage Service (S3)is an AWS object storage platform which helps you to store the files in form of objects, and, store and retrieve any amount of data from anywhere. Each file stored in Amazon S3 (as an object) is represented using a key.
Spring Boot Application and Amazon S3 Cloud
AWS Java SDK supports various APIs related to Amazon S3 service for working with files stored in S3 bucket.
Amazon S3 Account Configuration
Please follow the instructions given in the Amazon S3 official documentation for creating and configuring the S3 account and bucket.
I will list down the steps in below for your convenience.
Sign up with Amazon
First you need to go to https://aws.amazon.com/s3/ for sign up with Amazon S3 service. You have to follow up the instructions given in the sign up document and need to complete the registration.
AWS will notify you by email when your account is active and available for you to use. Once your account is active and ready to use, you can
Creating S3 bucket
login to https://console.aws.amazon.com/s3 and click on “Create bucket” from below screen.
Then provide the required details (e.g;- bucket name , region) and click on “Create“.

Then it will list down the newly created bucket as follows.
Create an IAM User
Go to https://console.aws.amazon.com/iam/ . In the navigation pane, choose Users and then choose Add user.
Input User name, choose Programmatic access for Access type:
Press Next: Permissions button -> go to Set permissions for peopleshrm-app-user screen.
Justin Timberlake - Discography - Albums 9 Singles 5. Free and Fast Torrent Downloads. Justin Timberlake. Missy Elliott and Timbaland)Sexy Ladies (Remix) (feat. Charlie Wilson and Snoop Dogg). Charlie Wilson and Snoop Dogg). Timbaland and Nelly Furtado). Beyonce) (Club Remixes). Justin - Love Sex Magic. The clipse discography rapidshare downloader. May 31, 2017. So Clean,” and Clipse's “When the Last Time.” There are a couple stylistic outliers, like Ini Kamoze and Zhane, but it still offers a useful, if somewhat random, assortment of rap singles. Kelis - Discography (1999-2014) download links are externally hosted on Extabit, Rapidshare, Rapidgator, Netload,.
Now, choose Attach existing policies directly -> filter policy type s3, then check AmazonS3FullAccess. Then click on “Next Review“.
Once you have reviewed the details, press “Create user“. Now you have successfully created the user.
Amazon S3 File Upload Api Codes Free
Click on Download .csv for downloading the credentials. {Access key ID, Secret access key}.

Spring Boot Amazon S3 client application.
Final Project Structure
Your final project structure should looks like below.
Add Amazon SDK for the project
First we need to create a simple spring boot project and add the following AWS SDK dependency for the project.
AWS Java SDK supports various APIs related to Amazon S3 service for working with files stored in S3 bucket.
application.properties
Add the AWS S3 bucket related configurations in the application.properties as follows. You will find those configurations details in the CSV file (Download.csv) that you have already downloaded. Change the properties
AmazonS3Config.java
AmazonS3 is a class from amazon dependency. All other fields are just a representation of variables from our application.propertiesfile. The @Value annotation will bind application properties directly to class fields during application initialization.
AmazonS3ClientService.java
AmazonS3ClientServiceImpl.java
The two methods responsible for uploading and deleting files are annotated with @Async to make them asynchronous. That means those methods will be executed in some other background thread except the main thread (the thread responsible for serving the request)
When uploading files, the below code segment is responsible for adding PublicRead permissions to the file being uploaded. It means that anyone who has the file url can access the file.
FileHandlerController.java
Testing Application
Lets upload a file.
Let’s test our application by making requests using Postman. We need to choose POST method, in the Body we should select ‘form-data’. As a key we should enter ‘file’ and choose value type ‘File’. Then choose any file from your local drive as a value.
As you can see that, we are trying to send a POST request to /files endpoint with file called “zuul-api-gateway.jpg“. Once the request is submitted, the file will be uploaded to the S3 bucket in the background thread (@Async method).
Once the file is uploaded (after few seconds), it should be listed under your S3 bucket as follows.
Lets remove a file
In order to delete the file, the following HTTP DELETE request to the following endpoint should be sent along with name of the file to be deleted.
DELETE http://localhost:8080/files?file_name=zuul-api-gateway.jpg
Then again go back to the Amazon S3 bucket and check whether the file exists there. You can see that file is successfully removed and no longer listed there.
Upload To Amazon S3
In this article, we have discussed how to upload files to the Amazon S3 bucket and how to remove the stored files. If you have any query or clarification, please feel free to drop a comment or message.
The Source Code
The complete source code of this article can be found at GitHub. Click here to download.