Introduction to Cloud Storage Objects

This lesson expands your cloud storage knowledge to include managing objects (files) within buckets. We'll explore uploading, downloading, and deleting objects using the client library, focusing on real-world applications such as user uploads or maintaining an image archive.

Understanding Blobs

In Cloud Storage, files are represented as blobs (Binary Large Objects). A blob is a reference to a file within a bucket. Before performing operations on a file, you need to create a blob object that represents that file.

bucket = client.bucket('cosmo-user-uploads')
blob = bucket.blob('cosmo-profile-2023.jpg')  # Creates a blob reference

The blob() method creates a blob object that references a file named cosmo-profile-2023.jpg in the bucket. Note that this doesn't create the actual file yet—it just creates a reference to it.

Uploading Files to Buckets

To upload a file, specify the bucket name (cosmo-user-uploads), the object name within the bucket (cosmo-profile-2023.jpg), and the local file path. The upload_from_filename() method transfers the file from your local system to the cloud.

from google.cloud import storage
from google.auth.credentials import AnonymousCredentials

client = storage.Client(
    project=os.environ.get('PROJECT_ID'),
    credentials=AnonymousCredentials(),
    client_options={'api_endpoint': os.environ.get('STORAGE_HOST')}
)
bucket = client.bucket('cosmo-user-uploads')
blob = bucket.blob('cosmo-profile-2023.jpg')
blob.upload_from_filename('path/to/local/file.jpg')  # Uploads the actual file
Listing Objects in a Bucket

To efficiently manage data within buckets, it's important to list the objects stored. The list_blobs() method returns all blob objects in a bucket, allowing you to iterate through them.

blobs = client.list_blobs('cosmo-user-uploads')  # Returns iterator of all blobs
for blob in blobs:
    print(blob.name)  # blob.name gives the object's name in the bucket

In this snippet, cosmo-user-uploads is the bucket name. The loop iterates over each blob object in the bucket, printing each object's name.

Downloading Files from Buckets

To download a file, create a blob reference and use download_to_filename() to save the cloud file to your local system.

bucket = client.bucket('cosmo-user-uploads')
blob = bucket.blob('cosmo-profile-2023.jpg')  # Reference to the cloud file
blob.download_to_filename('local/path/cosmo-profile.jpg')  # Downloads to local path
Retrieving Object Metadata

Each object has metadata, such as content type and size. The reload() method fetches the latest metadata from the server, ensuring you have current information.

bucket = client.bucket('cosmo-images-archive-2023')
blob = bucket.blob('cosmo-profile-2023.jpg')
blob.reload()  # Refreshes metadata from the server

print("Content type:", blob.content_type)  # e.g., 'image/jpeg'
print("Size (bytes):", blob.size)          # File size in bytes
print("Custom metadata:", blob.metadata)   # Custom key-value pairs

Metadata includes details such as content type (image/jpeg), size, and any custom metadata added during upload.

Deleting Files from Buckets

The delete() method removes objects from the bucket, helping keep your storage organized:

# Deleting a single object
bucket = client.bucket('cosmo-user-uploads')
blob = bucket.blob('temporary-file.jpg')
blob.delete()  # Permanently removes the object

# Deleting all objects in the bucket
blobs = client.list_blobs('cosmo-user-uploads')
for blob in blobs:
    blob.delete()  # Deletes each object individually
Handling Exceptions Gracefully

When interacting with cloud storage, errors can occur, such as trying to access a non-existent bucket or object. The client library provides specific exceptions to help you handle these situations clearly and effectively.

from google.cloud import storage
from google.api_core.exceptions import NotFound, GoogleAPIError

client = storage.Client()

try:
    bucket = client.get_bucket('cosmo-user-uploads')
    bucket.delete()
except NotFound:
    print("Error: The specified bucket does not exist.")
except GoogleAPIError as e:
    # This captures other unexpected errors
    print(f"An unexpected error occurred: {e.message}")

This approach allows you to specifically catch errors like NotFound, which occur when the requested bucket or object doesn't exist. It also prepares your application to handle other, less common errors, ensuring robust interaction with your storage resources.

Lesson Summary

You've learned to manage objects in cloud storage, from uploading and downloading to handling errors using specific exceptions. These skills are foundational for cloud storage management, setting the stage for more advanced data handling techniques.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal