Sunday, 9 August 2020

Create a storage bucket in Google Cloud Platform

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 Web Application hosted in VM instance of the Google Cloud Platform. 

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 Google Console as well as gsutil tool which is a python application with which we can access the Cloud Storage from the command line. Then we will upload the file in the bucket (bucket are the containers to hold the data. Everything you store in the cloud storage must be contained in the bucket) and access that file in a web application hosted in the VM instance created in the earlier step.

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 login 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 up 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, 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, 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 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 the 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 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.

Thank you.

0 comments:

Post a comment

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Subscribe us on YouTube

Like us on Facebook

Follow us on Google+