In this article, we will learn how to create a storage bucket in the Google Cloud Platform and access the files stored in the Storage Bucket in a web application hosted in a VM instance of the Google Cloud Platform.
A few articles which I already wrote and might be helpful to you if you are beginning with Google Cloud Platform are:
In this article, we will start by creating a new instance of the VM in Google Cloud Platform. After that, we will see how to create the storage bucket with the Google Console, as well as a gsutil tool which is a Python application with which we can access Cloud Storage from the command line. Then we will upload the file in the bucket and access that file in a web application hosted in the VM instance created in the earlier step ((buckets are the containers to hold the data and verything you store in the cloud storage must be contained in the bucket).
Let’s begin.
Step 1
Log in with Google Cloud Platform by visiting https://console.cloud.google.com/ (Google provides $300 as a free trial, but for that, you need to link your credit card).
On logging in successfully, users will see the Google Cloud Dashboard as shown in the below image.
Step 2
Click on VM Instances submenu available under Compute Engine Menu.
Click on create button in order to create a VM Instance
Step 4
Give a name to your VM Instance then select region, zone, machine configuration as per your need.
Select boot disk operating system as Debian (a Linux based operating system) as I will use the Apache server in order to host a web application.
Make sure to allow HTTP traffic as well as we will access the web application through HTTP Expand Management, Security, Disks, Networking, and Sole Tenancy in order to add the start-up script.
Step 5
Add start-up script as shown in the below image in order to install Apache2, PHP and other modules need in order to access the MySQL DB (In my next article, I will show you how to create and use MySQL DB in Google Cloud Platform). Once installed the next statement will restart the Apache server.
Click on the create button in order to create the VM instance.
Within a minute, the VM Instance is ready to use. You can see the green check in front of the Instance name which clearly indicates that VM Instance is up and ready for use.
Step 6
In order to create a storage bucket from Google Console, click on the browser sub-menu option available in the Storage menu option.
Click on Create Bucket link as shown in the below image to add a Storage Bucket. Once clicked, it will ask you for a global unique name for the Storage Bucket. Then choose where to store your data; i.e. Single Region, Multi-Region, Dual-Region as per your need. In the next step, it will ask you to select the storage class for your data that can be Standard (best for short-term storage, frequently accessed data), Newline (for backups), Coldline (for disaster recovery), and Archive. Then the next option is related to the control access to the objects i.e. either Fine-Grained (Object-level permission) or Uniform (Bucket level permission). The last option is for additional optional settings.
We can also create the Storage bucket with the Cloud Shell by clicking on the Cloud shell icon in the top right corner as shown below.
Step 7
Let’s set the LOCATION (US) into an environment variable called LOCATION using the below command. We will use that variable for during creation of the Storage bucket.
Step 8
Type gsutil mb -l $LOCATION gs://$DEVSHELL_PROJECT_ID command in order to create a bucket with the name of project id (unique). You can also provide a unique name to your bucket manually as well. Here in the command mb stands for the make bucket. gs://<Your_Bucket_Name> in which gs:// refers to the resource in the Cloud Storage.
Once Storage Bucket is created successfully, it will be visible in the Storage browser as shown below.
Click on the Storage Bucket name, in order to check the objects stored in the bucket. As we have not uploaded any object/file yet, it will show an empty bucket.
Step 9
Let’s upload some file with gsutil with the below command,
gsutil cp <FILE_NAME_UPLOADED> gs://<YOUR_BUCKET_NAME>/<FILE_NAME_UPLOADED>
In the above command, we are copying a file to the Storage bucket with gsutil command utility.
Let’s again refresh the Storage Bucket details in order to check whether the file/object is uploaded or not. Currently, the file which we uploaded is not public.
In order to use this uploaded image in the web applications, we need to make it as public. Let’s modify the access control list and make it readable for all users with the below command.
After running the command, refresh the bucket objects, you can see that now that permission is changed and available to the public to the internet. Copy the link and use it on any web page as required.
Users can also upload the objects directly with the console as well. Click on the upload file and select the objects you want to upload as shown below.
Click on the uploaded file name in order to change the file access permission. Make it Public with read access permission. Copy it Public URL and use it in your web application.
Step 10
Login with the SSH in the VM Instance created. Change directory to /var/www/html directory and create a index.php file with nano editor and paste the
<img src=”<PASTE_FILE_PATH_COPIED_FROM_BUCKET>”>.
Now click on the public IP Address shown in the VM Instance and access /index.php file.
Uploaded image in Google Storage is now available on the web page created as shown below.
I hope this article will help you in understanding the Google Cloud Storage Bucket.