[GCP] Operate Google Cloud Storage with Python

Install google-cloud-storage

$ pip install --upgrade google-cloud-storage

Creating a storage client


from google.cloud import storage
client = storage.Client()

Operations on buckets

Creating a bucket

bucket = storage.Bucket(client)
bucket.name = "test-bucket"
bucket.location = "asia-northeast1"
client.create_bucket(bucket)

Get bucket

bucket_name = "test-bucket"
bucket = client.get_bucket(bucket_name)

List of buckets

for bucket in client.list_buckets():
    print(bucket.name)

# test-bucket1
# test-bucket2
# test-bucket3

Bucket existence check

print(bucket.exists())

# True

Delete bucket

bucket.delete(force=True)

If you want to send a delete request to a bucket, the bucket must be empty. By setting the parameter "force" to True, you can delete all the objects in the bucket and then delete the bucket. (Empty with True, default False) However, be aware that if there are more than 256 objects in the bucket, a ValueError will occur.

Operations on objects in the bucket

List objects

for blob in client.list_blobs(bucket_name):
    print(blob.name)

# test_dir/
# test_dir/hoge.txt
# test_dir/test_file_in_dir_1.txt
# test_dir/test_file_in_dir_2.txt
# test_file_1.txt
# test_file_2.txt

List objects (Prefix specified)

for blob in client.list_blobs(bucket_name, prefix="test_dir/test"):
    print(blob.name)

# test_dir/test_file_in_dir_1.txt
# test_dir/test_file_in_dir_2.txt

Create an instance of an object

blob = bucket.blob("test_dir/test_file_in_dir_1.txt") #Specify storage path

Object existence check

print(blob.exists())

# True

Download object

blob.download_to_filename("test_file_in_dir_1.txt") #Specify the download destination path

Object upload

blob.upload_from_filename("test_file_in_dir_1.txt") #Specify the upload source path

Delete object

blob.delete()

reference

google-cloud-storage Library Reference https://googleapis.dev/python/storage/latest/client.html

Recommended Posts

[GCP] Operate Google Cloud Storage with Python
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Try using Python with Google Cloud Functions
Operate Sakura's cloud object storage from Python
Operate Kinesis with Python
Operate Blender with Python
Operate Excel with Python (1)
Operate Excel with Python (2)
Text extraction with GCP Cloud Vision API (Python3.6)
Copy data from Amazon S3 to Google Cloud Storage with Python (boto)
Operate Excel with Python openpyxl
Operate TwitterBot with Lambda, Python
Study Python with Google Colaboratory
[Note] Operate MongoDB with Python
A story linked with Google Cloud Storage with a little ingenuity
Use of Google Cloud Storage (GCS) with "GAE / Py"
[Python] [SQLite3] Operate SQLite with Python (Basic)
Access Google Drive with Python
How to upload files to Cloud Storage with Firebase's python SDK
Operate limited shared Google Calendar with Lambda (Python) [cloudpack Osaka]
[Package cloud] Manage python packages with package cloud
Try to operate Facebook with Python
Run XGBoost with Cloud Dataflow (Python)
Using Cloud Storage from Python3 (Introduction)
Operate ECHONET Lite appliances with Python
[GCP] How to publish Cloud Storage signed URLs (temporary URLs) in Python
How to connect to Cloud Firestore from Google Cloud Functions with python code
Upload file to GCP's Cloud Storage (GCS) ~ Load with local Python
Google Cloud Vision API sample for python
Streaming speech recognition with Google Cloud Speech API
Operate smartlife power supply with python (de-IFTTT)
Clogged with python update of GCP console ①
Use Google Cloud Vision API from Python
Register users with Google Admin SDK (python)
[Pyto] Operate iPhone Taptic Engine with Python
Text mining with Python ② Visualization with Word Cloud
[Python] Automatically operate the browser with Selenium
Operate home appliances with Python and IRKit
Upload images to Google Drive with Python
Handle structured logs with GCP Cloud Logging
[AWS] [GCP] I tried to make cloud services easy to use with Python
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
FizzBuzz with Python3
Csv output from Google search with [Python]! 【Easy】
Scraping with Python
Play with Google Spread Sheets in python (OAuth)
Statistics with python
Firebase: Use Cloud Firestore and Cloud Storage from Python
Try it with Word Cloud Japanese Python JupyterLab.
Scraping with Python
Python with Go
Try running Google Chrome with Python and Selenium
Clogged with python update of GCP console ② (Solution)
Twilio with Python
Integrate with Python
Get Google Cloud Storage object list in Java
Play with 2016-Python
[Cloud102] # 1 Let's get started with Python (Part 3 Jupyter Notebook Construction GCP Cloud Shell Edition)
AES256 with python
Tested with Python
python starts with ()