
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
In this series of labs, you take a demo microservices Java application built with the Spring framework and modify it to use an external database server. You adopt some of the best practices for tracing, configuration management, and integration with other services using integration patterns.
Google Cloud has a bucket-based object storage solution called Cloud Storage. Cloud Storage is designed to store a large number of files and a large volume of binary data. You don't need to manage your own file systems or file sharing services. Cloud Storage can be used directly by many other Google Cloud products. For example, you can store data files on Cloud Storage and process those data files in a managed Hadoop (Dataproc) cluster. You can also import structured data stored on Cloud Storage directly into BigQuery for ad hoc data analytics using standard SQL.
Cloud Storage provides fast, low-cost, highly durable, global object storage for developers and enterprises that need to manage unstructured file data. In Cloud Storage, the consistent API, low latency, and speed across storage classes simplify development integration and reduce code complexity.
In this lab, you add the ability to upload an image associated with a message. You store the image in Cloud Storage.
In this lab, you learn how to perform the following tasks:
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is a panel populated with the temporary credentials that you must use for this lab.
Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Choose an account page.
On the Choose an account page, click Use Another Account. The Sign in page opens.
Paste the username that you copied from the Connection Details panel. Then copy and paste the password.
After a few moments, the Cloud console opens in this tab.
After you complete the initial sign-in steps, the project dashboard appears.
In this task, you clone the source repository files that are used throughout this lab.
To begin the lab, click the Activate Cloud Shell button at the top of the Google Cloud Console.
To activate the code editor, click the Open Editor
button on the toolbar of the Cloud Shell window. Code editor opens in a new tab with continued access to Cloud Shell.
Now you're ready to go!
In this task, you add the Cloud Storage starter to the guestbook frontend application.
~/guestbook-frontend/pom.xml
.<dependencies>
section, just before the closing </dependencies>
tag:In this task, you update the home page to enable files to be uploaded with a message.
You edit the main.container
class input form action to handle multi-part data encoding.
~/guestbook-frontend/src/main/resources/templates/index.html
.To these lines:
<input type="submit" value="Post"/>
tag:You update the FrontendController.java file to accept file data when you choose to upload an image with a message.
~/guestbook-frontend/src/main/java/com/example/frontend/FrontendController.java
.import
directives immediately below the existing import
directives:FrontendController
class definition, immediately before @GetMapping("/")
:You need to get Applicationcontext
in order to create a new resource. The project ID is required in order to access Cloud Storage because this demo application uses the project ID as the Cloud Storage bucket name.
The first part of FrontendController
should look like the screenshot:
You modify the post method to save uploaded images to Cloud Storage.
to these lines:
model.addAttribute("name", name);
:client.add(payload);
line:The complete post
method definition should look like the screenshot:
In this task, you run the application in Cloud Shell to test the new image upload functionality.
guestbook-service
directory:The backend service application launches on port 8081.This takes a minute or two to complete. Wait until you see that the GuestbookApplication is running.
guestbook-frontend
directory:cloud
profile:Open the Cloud Shell web preview on port 8080 and post a message with a small JPEG image.
From the Google Cloud console, navigate to Cloud Storage > Buckets.
Navigate to your bucket.
The uploaded file should appear as in the screenshot. There will also be two folders that were created during the lab startup that contain the demo application source files.
In this task, you update the frontend application to retrieve and display images associated with guestbook messages.
You edit FrontendController.java
so that it fetches image files associated with messages if they are found.
~/guestbook-frontend/src/main/java/com/example/frontend/FrontendController.java
.import
directive immediately below the existing import
directives:FrontEndController
class definition. Place the code after the closing brace for the post
method definition and immediately before the final closing brace:The end of the FrontendController.java
file should look like the screenshot:
You edit the main.container
class to load and display images for messages if they are present.
~/guestbook-frontend/src/main/resources/templates/index.html
.In the next step, you insert an <image>
tag in the messages
class.
<img>
tag after the second <span>
tag:The main container
div class should look like the screenshot:
In this task, you restart the frontend application to activate the new image retrieval and display functionality.
In this lab, you added the Spring starter for Cloud Storage and created a Cloud Storage bucket. Next, you modified the HTML template of the frontend application to enable file uploads. You then modified the frontend application to process and store images on Cloud Storage. Finally, you modified the frontend application to display uploaded message images.
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one