I have my google cloud storage buckets labeled
I can't find anything in the docs on how to do a gsutil ls
but only filter buckets with a specific label- is this possible?
List the objects in a bucket. In the Google Cloud console, go to the Cloud Storage Buckets page. In the bucket list, click on the name of the bucket whose contents you want to view.
ls - List providers, buckets, or objects.
If you specify one or more provider URLs, gsutil ls lists buckets at each listed provider: gsutil currently supports gs:// and s3:// as valid providers If you specify bucket URLs, or use Wildcards to capture a set of buckets, gsutil ls lists objects at the top level of each bucket, along with the names of each subdirectory.
All buckets names share a single global Google namespace and must not be already taken. $ gsutil mb gs://<bucketname> Note that there are certain restrictionson bucket naming and creation beyond the uniqueness condition. For instance you cannot change the name of an existing bucket, and a bucket name cannot include the word google.
Step 1: Install and configure gsutil. Step 2: Create a bucket using gsutil mb command. ## gsutil mb [-b <on|off>] [-c class] [-l location] [-p proj_id] [--retention time] url... ## -b = Specifies the uniform bucket-level access setting. Default is "off" ## -c = Specifies the default storage class. Default is "Standard".
Use the command gsutil mb gs://<YOUR_BUCKET_NAME> to create it (Fig. 4). You will need to make sure that <YOUR_BUCKET_NAME> is a unique name, not just in your GCP account but in the entire global Google Cloud Platform.
Just had a use case where I wanted to list all buckets with a specific label. The accepted answer using subprocess was noticeably slow for me. Here is my solution using the Python client library for Cloud Storage:
from google.cloud import storage
def list_buckets_by_label(label_key, label_value):
# List out buckets in your default project
client = storage.Client()
buckets = client.list_buckets() # Iterator
# Only return buckets where the label key/value match inputs
output = list()
for bucket in buckets:
if bucket.labels.get(label_key) == label_value:
output.append(bucket.name)
return output
Nowadays is not possible to do what you want in one single step. You can do it in 3 steps:
gsutil ls
of every bucket that accomplish your criteria.This is my python 3 code that I did for you.
import subprocess
out = subprocess.getoutput("gsutil ls")
for line in out.split('\n'):
label = subprocess.getoutput("gsutil label get "+line)
if "YOUR_LABEL" in str(label):
gsout = subprocess.getoutput("gsutil ls "+line)
print("Files in "+line+":\n")
print(gsout)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With