Create Cloud Storage Buckets using Command-line and REST APIs in GCP

Cloud Storage Bucket using Command-Line in GCP
This tutorial focuses on creating a cloud storage bucket using the Google Cloud command-line and REST APIs, such as XML and JSON in Google Cloud Platform.  

 

Google Cloud Platform (GCP) is a set of cloud computing services offered by Google. GCP is a public platform that offers IT resources, such as storage, compute, database, Big Data, application development tools, networking, and more. Since 2008, Google Cloud Platform is one of the leading cloud providers in the cloud computing field. Google has always been at the top among its competitors by offering the most reliable and highly scalable platform for building, deploying, and testing real-time application environments. The GCP services run on the same cloud infrastructure that Google internally uses to run its end-user products, such as Google Photos, Gmail, Google Search, Google Drive, YouTube, etc. Software engineers, IT professionals, and cloud administrators can access the services provided by Google Cloud Platform over the Internet or a dedicated network connection.

Buckets are the primary containers that hold user data in Google Cloud. Every data stored in Cloud Storage must be contained in a bucket. Users can also use buckets to control and organize their data; unlike directories and folders, they can’t nest buckets. Since there are some limits to create and delete buckets in Google Cloud, users should design their storage applications to favor intensive object operations and relatively few buckets operations.

While creating a bucket, the user should specify a globally unique name, a default storage class, and a geographic location where the bucket and its contents are stored. The default storage class that the user chooses applies to objects added to the bucket that doesn’t have a storage class specified explicitly.

If the user didn’t specify a location and storage class in their request, buckets are created with a default storage class of Standard Storage in the US multi-region. After creating a bucket, the user can always change its default storage class to any class supported in that bucket’s location. However, the user needs to delete and re-create the bucket if they want to change the bucket name and location.

To create a bucket in GCP, the user must have the storage.buckets.create permission for the project. If the user is working within a project that they did not create, they might need the project owner to assign a role containing this permission, such as Storage Admin

 

Create a Storage Bucket via Command-line

gsutil is a Python application that lets the user access Cloud Storage from the command line. Users can use hegsutil to do a wide range of object and bucket management tasks, including:

  • Upload, download and delete objects.
  • Create and delete buckets.
  • List objects and buckets.
  • Move, rename and copy objects.
  • Edit bucket and object ACLs.

 

gsutil performs all operations, including uploads and downloads, using transport-layer security (TLS) and HTTPS protocol. To create a new storage bucket using the gsutil command, execute the following gsutil mb command:

 

gsutil mb gs://BUCKET_NAME

 

Where: 

  • BUCKET_NAME is the name the user wants to give their bucket. The bucket name should meet the naming requirements. For example, test-bucket.

 

If the above request is successful, the command will return the following message:

 

Creating gs://BUCKET_NAME/...

 

Users can also set the following optional flags to create a bucket by specifying the parameters instead of using the default: 

  • c: Specify the default storage class of the bucket. For example, NEARLINE.
  • p: Specify the project with which the bucket will be associated. For example, test-project.
  • l: Specify the location in which the user wants to store the bucket. For example, US-WEST1.
  • b: Enable uniform bucket-level access for the bucket.

 

For Example, 

 

gsutil mb -p PROJECT_ID -c STORAGE_CLASS -l BUCKET_LOCATION -b on gs://BUCKET_NAME

 

Create a Storage Bucket via REST APIs

 

XML API 

To create a new storage bucket using an XML API, follow the below steps: 

1. First, get an authorization access token from the OAuth 2.0 Playground

2. Next, configure the playground to use its OAuth credentials.

3. Create a .xml file that contains the following information:

 

<CreateBucketConfiguration>
   <LocationConstraint>BUCKET_LOCATION</LocationConstraint>
   <StorageClass>STORAGE_CLASS</StorageClass>
</CreateBucketConfiguration>

 

Where:

·        BUCKET_LOCATION is the location where the user wants to store their bucket’s object data. For example, US-EAST1.

·        STORAGE_CLASS is the default storage class of the bucket. For example, NEARLINE.

 

4. Finally, execute the following cURL command to call the XML API:

 

curl -X PUT --data-binary @XML_FILE_NAME.xml \
     -H "Authorization: Bearer OAUTH2_TOKEN" \
     -H "x-goog-project-id: PROJECT_ID" \
     "https://storage.googleapis.com/BUCKET_NAME"

 

Where:

·        XML_FILE_NAME is the name of the XML file the user created in Step 3.

·        OAUTH2_TOKEN is the access token that we generated in Step 1.

·        PROJECT_ID is the ID of the project with which the bucket will be associated. For example, test-project.

·        BUCKET_NAME is the name the user wants to give their bucket. For example, test-bucket.

 

JSON API

To create a new storage bucket using a JSON API, follow the below steps: 

1. First, get an authorization access token from the OAuth 2.0 Playground. 

2. Next, configure the playground to use its OAuth credentials.

3. Create a .json file containing the bucket settings, which must include a name for the bucket. The common settings that need to be included are:

 

{
  "name": "BUCKET_NAME",
  "location": "BUCKET_LOCATION",
  "storageClass": "STORAGE_CLASS",
  "iamConfiguration": {
    "uniformBucketLevelAccess": {
      "enabled": true
    },
  }
}

 

where:

·        BUCKET_NAME is the name the user wants to give their bucket. For example, test-bucket.

·        BUCKET_LOCATION is the location where the user wants to store their bucket’s object data. For example, US-EAST1.

·        STORAGE_CLASS is the default storage class of the bucket. For example, NEARLINE.

 

4. Finally, execute the following cURL command to call the JSON API:

 

curl -X POST --data-binary @JSON_FILE_NAME.json \
     -H "Authorization: Bearer OAUTH2_TOKEN" \
     -H "Content-Type: application/json" \
     "https://storage.googleapis.com/storage/v1/b?project=PROJECT_ID"

 

Where:

·        JSON_FILE_NAME is the name of the JSON file that we created in Step 3.

·        OAUTH2_TOKEN is the access token generated in Step 1.

·        PROJECT_ID is the ID of the project with which the bucket will be associated. For example, test-project.

 

Conclusion 

This tutorial presents the steps to create a new Cloud Storage bucket using command-line and REST APIs in Google Cloud Platform. Hope this tutorial was helpful, and do reach out to us if you have any query or suggestions.

Share this post

Services to Explore

Stay up to date!

Stay up to date with the Web Hosting, Cloud and Server Management Industry News and Tutorials!

We will send you only the relevant emails, and we respect your privacy. Please review our privacy policy for more info.

Managed Google Cloud Services

Focus on your business, and let us take care of your Google Cloud Infrastructure!
From what you are reading, it seems you are interested in Google Cloud and related technologies. If you have a moment to spare, please take a look at our Managed GCP Services, and which might interest you even more!
Managed Google Cloud

Value-Added Services

We have services that can help you run a successful business. With us, you don't have to worry about these areas because our experts will take care of it for you.

Introducing - WPHealers!

Complete WordPress Management, 24x7 Support & White Label Agency Plans!

WordPress migration service, security, white-label reseller services and lot more! Grab the introductory deals now!

WPHealers

ServerHealers uses cookies.