1. Code
  2. Cloud & Hosting

Google Cloud Storage: Managing Buckets

Learn how to use the very powerful Google Cloud Storage for your projects or applications. In this tutorial, we cover how to manage buckets on GCS.
Scroll to top
This post is part of a series called Google Cloud Storage.
Google Cloud Storage: Managing Files and Objects

Google Cloud Storage (GCS) is a very simple and powerful object storage offering from Google as a part of its Google Cloud Platform (GCP). It provides a highly durable, scalable, consistent and available storage solution to developers and is the same technology that Google uses to power its own object storage. 

It is billable with a pay for what you use model, and GCP comes with a 60-day trial period, so it is free to try out to see if it fits the needs of your organization. GCS has different service levels (also called storage classes) which can be chosen as needed (detailed discussion on these is out of the scope of this tutorial). GCS can be used for a variety of purposes, such as serving static/dynamic website content, storing user-specific application files, disaster recovery, or making large data objects downloadable to users.

Those who have worked on GCP will know that everything in GCP revolves around projects. Each project can have many buckets around which the architecture of Google Cloud Storage is structured. Buckets are the basic containers on GCS that contain the data stored. These are used as basic blocks to organize your data and look like folders on an operating system, but they cannot be nested. 

Each bucket can contain any number of objects, which can be folders and/or files. A bucket is assigned a storage class and geographic location when being created. These settings can be specified while creating the bucket but cannot be changed later.

Buckets have specific naming conventions which need to be followed strictly, otherwise GCP will not allow you to create a bucket. Bucket names are globally unique, so they need to chosen in a way that prevents conflicts. However, a name used by a deleted bucket can be reused. 

Also, the name cannot be changed once it's been assigned to a bucket. The only solution if you want to change it is to create a new bucket with the desired name, move the contents from the previous bucket to the new one, and then delete the previous bucket.

In this tutorial, I will cover how to manage buckets from the Google Cloud Console. This is followed by a Python script where I will demonstrate performing the same operations programmatically.

Using Google Cloud Console

First, let's see how to manage buckets using the web user interface provided by GCP known as Google Cloud Console

Open Storage Browser in a web browser of your choice. If you are a first-time user, you will be prompted to create a project first. Also, an option will be shown to sign up for a free trial. Go ahead with the free trial signup, otherwise you will not be allowed to create a new bucket by yourself. By default, GCP only provides one free bucket per App Engine instance. 

Once done with all these formal processes, navigating to this page should open up the page shown below.

Google Cloud Storage BrowserGoogle Cloud Storage BrowserGoogle Cloud Storage Browser

To create a new bucket, click on the Create Bucket button highlighted above. Create a bucket by filling in a desired name as shown below. The name should follow the bucket naming conventions.

Creating a new bucketCreating a new bucketCreating a new bucket

After you've created a bucket, the GCS browser will list it. Buckets can be deleted by selecting them from the list and clicking on the delete button.

Delete a bucketDelete a bucketDelete a bucket

Clicking on the refresh button will populate the UI with any changes to the list of buckets without refreshing the whole page.

Managing Buckets Programmatically

First, let's create a Google Compute Engine instance as that will allow quick demonstration of the targeted concepts rather than dealing with extra authentication steps on local machines. To create a GCE instance, open the link and click on the Create Instance button as shown below.

Create a new Compute Engine instanceCreate a new Compute Engine instanceCreate a new Compute Engine instance

A form will come up asking for relevant details, which can be filled in at your convenience. Once the GCE instance is created, open up the SSH client as shown below, which by default opens in a new browser window.

SSH into Compute Engine instanceSSH into Compute Engine instanceSSH into Compute Engine instance

The SSH client screen will look something like as shown below. All the further operations in this tutorial will be done directly on the SSH client itself.

SSH Client for newly created Compute Engine instanceSSH Client for newly created Compute Engine instanceSSH Client for newly created Compute Engine instance

Writing a Python Script

Below are the commands you need to run to set up the newly created server for a Python development environment.

1
$ sudo apt-get update
2
$ sudo apt-get install python-dev python-setuptools
3
$ sudo easy_install pip

Below is the dependency that needs to be installed for writing this script.

1
$ sudo pip install google-api-python-client

On production systems, it is not advisable to install libraries using "sudo". Please follow Python virtualenv best practices for this.

gcs_bucket.py

1
import sys
2
from pprint import pprint
3
4
from googleapiclient import discovery
5
from googleapiclient import http
6
from oauth2client.client import GoogleCredentials
7
8
9
def create_service():
10
    credentials = GoogleCredentials.get_application_default()
11
    return discovery.build('storage', 'v1', credentials=credentials)
12
    
13
    
14
def list_buckets(project):
15
    service = create_service()
16
    res = service.buckets().list(project=project).execute()
17
    pprint(res)
18
    
19
    
20
def create_bucket(project, bucket_name):
21
    service = create_service()
22
    res = service.buckets().insert(
23
        project=project, body={
24
            "name": bucket_name
25
        }
26
    ).execute()
27
    pprint(res)
28
    
29
    
30
def delete_bucket(bucket_name):
31
    service = create_service()
32
    res = service.buckets().delete(bucket=bucket_name).execute()
33
    pprint(res)
34
35
36
def get_bucket(bucket_name):
37
    service = create_service()
38
    res = service.buckets().get(bucket=bucket_name).execute()
39
    pprint(res)
40
41
42
def print_help():
43
        print """Usage: python gcs_bucket.py <command>

44
Command can be:

45
    help: Prints this help

46
    list: Lists all the buckets in specified project

47
    create: Create the provided bucket name in specified project

48
    delete: Delete the provided bucket name

49
    get: Get details of the provided bucket name

50
"""
51
52
53
if __name__ == "__main__":
54
    if len(sys.argv) < 2 or sys.argv[1] == "help" or \
55
        sys.argv[1] not in ['list', 'create', 'delete', 'get']:
56
        print_help()
57
        sys.exit()
58
    if sys.argv[1] == 'list':
59
        if len(sys.argv) == 3:
60
            list_buckets(sys.argv[2])
61
            sys.exit()
62
        else:
63
            print_help()
64
            sys.exit()
65
    if sys.argv[1] == 'create':
66
        if len(sys.argv) == 4:
67
            create_bucket(sys.argv[2], sys.argv[3])
68
            sys.exit()
69
        else:
70
            print_help()
71
            sys.exit()
72
    if sys.argv[1] == 'delete':
73
        if len(sys.argv) == 3:
74
            delete_bucket(sys.argv[2])
75
            sys.exit()
76
        else:
77
            print_help()
78
            sys.exit()
79
    if sys.argv[1] == 'get':
80
        if len(sys.argv) == 3:
81
            get_bucket(sys.argv[2])
82
            sys.exit()
83
        else:
84
            print_help()
85
            sys.exit()

The above Python script demonstrates the major operations that can be performed on a bucket. These include:

  • creation of a new bucket in a project
  • listing of all buckets in a project
  • getting details of a specific bucket
  • deleting a specific bucket

Let's see what these operations look like when the script is run.

1
$ python gcs_bucket.py 
2
Usage: python gcs_bucket.py <command>
3
Command can be:
4
    help: Prints this help

5
    list: Lists all the buckets in specified project
6
    create: Create the provided bucket name in specified project
7
    delete: Delete the provided bucket name
8
    get: Get details of the provided bucket name
9
    
10
$ python gcs_bucket.py list tutsplus-demo
11
{u'items': [{u'etag': u'CAE=',
12
             u'id': u'tutsplus-demo.appspot.com',
13
             u'kind': u'storage#bucket',
14
             u'location': u'US',
15
             u'metageneration': u'1',
16
             u'name': u'tutsplus-demo.appspot.com',
17
             u'projectNumber': u'1234567890',
18
             u'selfLink': u'https://www.googleapis.com/storage/v1/b/tutsplus-demo.appspot.com',
19
             u'storageClass': u'STANDARD',
20
             u'timeCreated': u'2016-10-05T15:30:52.237Z',
21
             u'updated': u'2016-10-05T15:30:52.237Z'}],
22
 u'kind': u'storage#buckets'}
23
 
24
 $ python gcs_bucket.py create tutsplus-demo tutsplus-demo-test
25
{u'etag': u'CAE=',
26
 u'id': u'tutsplus-demo-test',
27
 u'kind': u'storage#bucket',
28
 u'location': u'US',
29
 u'metageneration': u'1',
30
 u'name': u'tutsplus-demo-test',
31
 u'projectNumber': u'1234567890',
32
 u'selfLink': u'https://www.googleapis.com/storage/v1/b/tutsplus-demo-test',
33
 u'storageClass': u'STANDARD',
34
 u'timeCreated': u'2016-10-07T05:55:29.638Z',
35
 u'updated': u'2016-10-07T05:55:29.638Z'}
36
 
37
 $ python gcs_bucket.py get tutsplus-demo-test
38
{u'etag': u'CAE=',
39
 u'id': u'tutsplus-demo-test',
40
 u'kind': u'storage#bucket',
41
 u'location': u'US',
42
 u'metageneration': u'1',
43
 u'name': u'tutsplus-demo-test',
44
 u'projectNumber': u'1234567890',
45
 u'selfLink': u'https://www.googleapis.com/storage/v1/b/tutsplus-demo-test',
46
 u'storageClass': u'STANDARD',
47
 u'timeCreated': u'2016-10-07T05:55:29.638Z',
48
 u'updated': u'2016-10-07T05:55:29.638Z'}
49
 
50
 $ python gcs_bucket.py delete tutsplus-demo-test
51
''

Conclusion

In this tutorial, you saw how to manage buckets on Google Cloud Storage. This was also accompanied by a small introduction to creating a Google Compute Engine instance and using it via an SSH client.

In the next tutorial, I will cover how to manage objects, i.e. folders and files inside a bucket.