Python to create Google Storage buckets

Question:

I am trying to use python to create a storage bucket on Google Cloud in a campaign to teach myself python and Cloud computing.

I have created the script below and it works well:

"""Creates a new bucket."""
import gcloud
from gcloud import storage

client = storage.Client(project='boot-script-test')

bucket_name = 'test-bucket-7283428'

bucket = client.create_bucket(bucket_name)

My problem is that the bucket is created as a storage class that I dont like and is in the wrong ‘location’. I asked how to specify storage class and location on Google Groups and kindly received a response to say that it’s not possible using Python directly (?):

https://groups.google.com/forum/#!topic/gce-discussion/s-TJZrYihw0

My understanding is that if I want more control over the Cloud and the command to create buckets, I need to use either the JSON or the XML Api and somehow “fit” them into my python code. Is this correct?

Is there any advantage to using one or the other to interface with Google Storage?

Would using the JSON API (for example) mean that I need to ‘encode’ requests/posts and decode responses every time I want to do anything that involves interfacing with the Cloud?

I am sorry if my query doesn’t make much sense – I am very new to the Cloud and to Python!

Thank you for any help!

Paul

Asked By: Paul

||

Answers:

You can create a bucket in a specific location like so:

client = storage.Client()
bucket = client.bucket('mybucket')
bucket.location = 'eu'
bucket.create()

The documentation for Google Cloud Client Libraries for Python can be found here: https://googlecloudplatform.github.io/google-cloud-python/latest/storage/client.html

This page shows the methods and attributes of Client. Links to the pages about Bucket, Blob, ACL and Batch can be found in the menu on the left.

Answered By: Brandon Yarbrough