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.
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.
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.
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
