2022-07-01

Cloud Storage API

Cloud Storage API

The Google Cloud Storage API is a tool that allows developers to interact with Google Cloud Storage using Python. It provides a comprehensive set of methods and functions to manage buckets and objects within the cloud storage environment.

Installation

You can install the Google Cloud Storage Python library by running the following command:

bash
$ pip install google-cloud-storage

Retrieving a Bucket

To retrieve information about a specific bucket in Google Cloud Storage using the Python API, you can follow these steps:

python
from google.cloud import storage

# Create a client
client = storage.Client()

# Retrieve the bucket
bucket = client.get_bucket(bucket_name)

# Print bucket details
print("Bucket Name: {}".format(bucket.name))
print("Location: {}".format(bucket.location))
print("Storage Class: {}".format(bucket.storage_class))

Retrieving a List of Buckets

To obtain a list of all the buckets associated with your project, you can use the following code:

python
from google.cloud import storage

# Create a client
client = storage.Client()

# Retrieve the list of buckets
buckets = client.list_buckets()

# Print bucket names
for bucket in buckets:
    print("Bucket Name: {}".format(bucket.name))

Creating a Bucket

To create a new bucket programmatically using the Google Cloud Storage API, you can use the following code:

python
from google.cloud import storage

# Create a client
client = storage.Client()

# Create the bucket
bucket = client.create_bucket(bucket_name, location=location, storage_class=storage_class)

print("Bucket {} created.".format(bucket.name))

Deleting a Bucket

To delete a bucket in Google Cloud Storage using the Python API, you can follow these steps:

python
from google.cloud import storage

# Create a client
client = storage.Client()

# Retrieve the bucket
bucket = client.get_bucket(bucket_name)

# Delete the bucket
bucket.delete()

print("Bucket {} deleted.".format(bucket.name))

Retrieving a List of Objects

To retrieve a list of objects within a Google Cloud Storage bucket using the Python API, you can use the following code:

python
from google.cloud import storage

# Create a client
client = storage.Client()

# Retrieve the bucket
bucket = client.get_bucket(bucket_name)

# List objects in the bucket
objects = bucket.list_blobs()

# Print object names
for obj in objects:
    print("Object Name: {}".format(obj.name))

Downloading and Uploading Objects as Files

To download and upload objects as files in Google Cloud Storage using the Python API, you can use the following code:

python
from google.cloud import storage

# Create a client
client = storage.Client()

# Retrieve the bucket
bucket = client.get_bucket(bucket_name)

# Download an object as a file
blob = bucket.blob(object_name)
blob.download_to_filename(local_file_path)

print("Object downloaded and saved as file: {}".format(local_file_path))

# Upload a file as an object
blob = bucket.blob(new_object_name)
blob.upload_from_filename(local_file_path)

print("File uploaded as object: {}".format(new_object_name))

Downloading Objects as File Objects from Resources

To download objects from Google Cloud Storage and retrieve them as file objects in Python, you can use the following code:

python
from google.cloud import storage

# Create a client
client = storage.Client()

# Retrieve the bucket
bucket = client.get_bucket(bucket_name)

# Retrieve the object
blob = bucket.get_blob(object_name)

# Download the object as a file object
file_object = blob.download_as_file()

# Read the contents from the file object
content = file_object.read()

print("Object Content: {}".format(content))

Downloading Objects as File Objects from URLs

To download objects from Google Cloud Storage using a URL and retrieve them as file objects in Python, you can use the following code:

python
import requests

# Specify the URL of the object
object_url = "https://storage.googleapis.com/{}/{}".format(bucket_name, object_name)

# Download the object and retrieve it as a file object
response = requests.get(object_url)
file_object = response.content

# Read the contents from the file object
content = file_object.read()

print("Object Content: {}".format(content))

Deleting an Object

To delete an object from a Google Cloud Storage bucket using the Python API, you can use the following code:

python
from google.cloud import storage

# Create a client
client = storage.Client()

# Retrieve the bucket
bucket = client.get_bucket(bucket_name)

# Delete the object
blob = bucket.blob(object_name)
blob.delete()

print("Object {} deleted.".format(object_name))

References

https://cloud.google.com/storage/docs/reference/libraries#client-libraries-install-python
https://cloud.google.com/python/docs/reference/storage/latest/index.html

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!