There is one thing that is currently missing in our recipes:
mouthwatering pictures of pizza with lots of cheese, like this one:
So let’s add pictures to our backend. We’ll use Amazon S3 Storage to upload and store images.
Attention: it requires creating an AWS account and binding in to your credit card, but we’ll only use free features, so no worries about having to pay. If you want to continue without images then just skip this part.
First, we need to register. Go to AWS portal and create an AWS account.
Verifying and binding your account to your credit card may take several days, then you’ll be able to start using AWS services.
When you’re verified, go to Amazon Services -> Storage -> S3
There create your first bucket, where you’ll store pizza pictures.
You can choose an AWS rregion for your bucket. To reduce latency choose the closest to you. Also do not choose “block public access”.
After your create a bucket it should appear in the list like this:
Remember your bucketName and region for future use in the app. In this case they are:
bucketName: kmpizza and
We’ll also need an
accessKey and a
Go to IAM Console.
And add a new user with programmatic access
Add permissions for S3 to this user
In stage 4 it should look like this:
When your user is created, copy your
Now that we have Amazon Storage all set up and ready, let’s get back to our backend module.
First, add AWS SDK to
Then implement it in your backend
Now let’s create a aws package and put it next to exposed in the storage directory:
Inside this package create a
FileStorage interface with a function to save files:
Then create an AmazonFileStorage class that will implement this save function:
 Get the bucket name from environment variables
 Also get other settings to initiate an AWS S3 client: region (optional), access and secret Keys
 Use specified settings to build an S3Client
 Implement the save function from the
 Use the object request to put a file into the AWS S3 bucket and get the source url
We want to use AWS functionality to upload images from the application when saving new recipes, so add
LocalSourceImpl’s constructor like this:
Just like with our local storage we want to use the AWS storage simply by injecting where we need it, so add one line to your
KoinModule.kt and modify the way we create
LocalSourceImpl by injecting the Amazon storage into it:
Let’s add a request to save images.
First, we’ll extend our recipes with a list of images that will show how delicious it is. Add a
RecipeImage data class to your
We can create recipes with images in two steps:
1) use the previously created Recipe entity to post a recipe to the backend and receive the id of newly created Recipe
2) add pictures to it.
But to receive recipes with images directly from the backend whenever we need them we’ll introduce a
Now we need to adjust our database. In a new image package create a
And a corresponding Entity:
Finally, we need to adjust our
RecipeEntity by adding a
Add changing the converter function accordingly:
Let’s use our newly created entities.
LocalSource interface declare this function:
Also change the return type of
And in the
LocalSourceImpl write the following implementations:
 Save the image in S3 storage and get the image url in return
 Get the recipe from the database that the image should be attributed to
 Create a new recipe image entity and save a reference to the recipe
And don’t forget to change the return type of
Also, we have to create a new image recipe table in the
Finally, let’s add the routes to our api. Add this function to
And then implement it:
 We receive the image file from the client side as a multipart form data
 This will help us transform part data to a file type so that we can save the image data in our database. It is unresolved for now and still has to be implemented in the next step
 If an error happens we throw an exception and do not save the image file
As mentioned in  we still need to create the
toFile function that will help us transform the
PartData.FileItem to a
File. Create a utils package in our backend package and put an
Extensions.kt file there with the following function
Now we can test uploading pictures to the recipes. First rebuild and make the project.
In the terminal set local environment variables according to your AWS settings.
then run the backend locally as many times before:
java -jar ./backend/build/libs/Backend.jar -port=9000
Check with postman if you still have any recipes in your local database, otherwise add the pizza dough recipe again.
If your database is fresh you get you
recipeId: 1, otherwise choose the id of the recipe you want to add pictures to.
Now you can add an image to your recipe. Using that id:
We get status
200 OK in response. Let’s check if the recipe has an image now with
getRecipes. Indeed in our recipe we see a list of images now:
We’ve built our very functional backend to store delicious recipes with images. In the next step we’ll see how to publish our backend on Heroku automatically. Soon after that we’ll move on to using the backend we built within the shared code between iOS and Android.