Automate File Upload Test case to AWS S3 storage using RestAssured


Nowadays, cloud computing is trending in the market. Most of the new products are getting shaped using cloud services as the underlying architecture. When we talk about the cloud, AWS (Amazon Web Service) is one of the leading cloud providers. Coming back to the apps, many of the apps use the media (images/video/pdf/doc), e.g. Social media apps like Twitter, Facebook, etc. use images/videos in the post, Ecommerce Apps use products’ images and the list is growing. Let’s gear our focus on AWS cloud services and first understand how the media upload to AWS S3 bucket works. Later, you’ll see how you can automate media upload test case using Rest Assured + Java.

Overview of serverless — media uploading to S3

When you upload any file directly to an S3 bucket, you must first request a signed URL from the Amazon S3 service. You can then upload the file directly using the signed URL. This is the two-step process for your application front end:

Let’s try to upload the jpeg image file to the AWS S3 bucket using postman.

  1. Call an Amazon API Gateway endpoint, which invokes the getSignedURL Lambda function. This gets a signed URL from the S3 bucket.
Get the signed URL from AWS S3 bucket.

As you can see the “uploadURL” field has the signed url in the response.

2. Hit this signed url along with the file to be uploaded from the application to the S3 bucket.

Upload Image file using the signed URL.

Yeah…The file is uploaded successfully to AWS S3 storage. Isn’t it simple????

Let’s see how you can automate above 2 steps procedure using Rest Assured and java.

Automating Media Upload Testcase using RestAssured + Java

Knowing the underlying AWS serverless architecture and upload file to AWS s3 bucket using postman (manually), the next step is to automate these 2 steps sequence which even simpler yet a bit tricky :)

Let’s automate the image/video upload to AWS S3 bucket using RestAssured (RestAPI) with Java. As the journey kick-off, the challenges start coming in the way. No worry at all, if you want to automate upload media API test cases. Let’s discuss the complete solution in detail.

Step 1: Login using AWS Cognito user credentials and getToken.

import com.amazonaws.auth.SystemPropertiesCredentialsProvider;

import com.amazonaws.regions.Regions;






import com.api.setup.RequestBuilder;

import com.core.Config;

import org.json.simple.parser.ParseException;


import java.util.HashMap;

import java.util.Map;

import static com.amazonaws.SDKGlobalConfiguration.ACCESS_KEY_SYSTEM_PROPERTY;

import static com.amazonaws.SDKGlobalConfiguration.SECRET_KEY_SYSTEM_PROPERTY;

public class AWSCognitoAccess {

public static String getIdToken(String username, String password) throws IOException, ParseException {

Regions regions = Regions.EU_WEST_1;

String accessKey = “<Replace your accessKey>”;

String secretKey = “<Replace your secretKey>”;

String clientId = “<Replace your clientId>”;

String poolId = “<Replace your poolId>”;

System.setProperty(ACCESS_KEY_SYSTEM_PROPERTY, accessKey); System.setProperty(SECRET_KEY_SYSTEM_PROPERTY, secretKey);

AWSCognitoIdentityProvider provider = AWSCognitoIdentityProviderClientBuilder.standard()

.withRegion(regions).withCredentials(new SystemPropertiesCredentialsProvider()).build();

Map<String, String> authParams = new HashMap<>();

authParams.put(“USERNAME”, username);

authParams.put(“PASSWORD”, password);

AdminInitiateAuthRequest adminInitiateAuthRequest = new AdminInitiateAuthRequest().withClientId(clientId)


AdminInitiateAuthResult result = provider.adminInitiateAuth(adminInitiateAuthRequest);

return result.getAuthenticationResult().getIdToken();



Security Notes: I strongly recommend keeping your AWS accessKey/secretKey/clientId/poolId secured. You may keep it in the separate config file and add that config file in the .gitignore file to skip it pushing to the git repository. If you are using any CI/CD pipeline, please use the best practices provided to keep the above-secured information.

Step 2: Call AWS API gateway to get a signed URL from S3.

============Create Media Model Class=============

package Models;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import com.fasterxml.jackson.annotation.JsonInclude;

import lombok.AllArgsConstructor;

import lombok.Builder;

import lombok.Data;

import lombok.NoArgsConstructor;





@JsonIgnoreProperties(ignoreUnknown = true)


public class Media {

private String mediaId;

private String filePath;

private String mimeType;

private String contentType;

private String uploadURL;

private String imageId;


==============Call AWS API gateway===========

public Media callAWSAPIGateway(String contentType){

String apiEndPoint = “https://<replace your domainName>/uploadImage”;

public Media buildMediaRequest(String contentType){

Media media = Media.builder().build();


return media;


response = RestAssured.given().body(buildMediaRequest(contentType)).post(apiEndPoint);


======Get signed url from response and save it into map========

public static void get_media_url(String mediaUrlKey, String imageIdKey){

Media media =;




Step 3: Finally, Call uploadMedia method from your calling function / main method.

public void uploadMedia(String username, String password, String contentType, String mediaName, String mediaUrlKey, String imageIdKey) throws IOException, ParseException {

//This is the media file location.

File mediaFile = new File(System.getProperty(“user.dir”)+”/src/test/resources/testResource/”+mediaName);

//Below highlighted lines do the magic. You may get an error if you don’t set urlEncodingEnable to false and set config for CharSet.

RestAssured.urlEncodingEnabled = false;



URL mediaUrl = new URL(map.get(mediaUrlKey));


.header(“Content-Type”, contentType)



RestAssured.urlEncodingEnabled = true;


Let’s call uploadMedia from main class.

public class TestFileUpload {
public static void main(String[] args) {

String username = “maulikpatel”;

String password = “HappyTest@123”;
String contentType = “video/mp4”;
String mediaName = “testVideo.mp4”;

String mediaUrlKey = “videoUploadURLKey”;

String imageIdKey = “videoImageIdKey”;

uploadMedia(username, String password, String contentType, mediaName, mediaUrlKey, imageIdKey)

That’s it. Your file is now uploaded to the S3 bucket successfully.

Happy Testing…. Very Happy Automated Testing…. :)



Maulikkumar Patel
An Automation and Functional QA Evangelist




Automation and Functional QA Evangelist

Love podcasts or audiobooks? Learn on the go with our new app.

What is the OSI Model? 7 layers explained in detail

Please become a member of Binance through my referral link. Thank you

Containers And Docker In Plain English For App Developers (1 of 3)

Scrum: 19 Sprint Planning Anti-Patterns

Raise your hand if you want to quit drinking coffee. Why, though???

How We Built a QA Team in a Fast-Growing Startup — Slido Blog

Day 24 of July LeetCode Challenge/

How to optimize your software development co-op / internship program

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Maulikkumar Patel

Maulikkumar Patel

Automation and Functional QA Evangelist

More from Medium

7 Benefits of DevOps for Database

What is OpenAPI ? — OpenAPI [1]

DataBase 101

Deploy Web app from Azure DevOps to AWS EC2