Managing Cloud Storage Objects

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

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