Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List buckets that match a bucket label with gsutil

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?

like image 866
red888 Avatar asked Jan 23 '19 16:01

red888


People also ask

How do I get a list of files in a GCS bucket?

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.

Which command is used to see the list of buckets in GCP?

ls - List providers, buckets, or objects.

Where does gsutil LS list buckets?

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.

How to name a Google bucket in gsutil?

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.

How to create a gsutil MB bucket in Linux?

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

How do I create a custom bucket name in GCP?

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.


2 Answers

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
like image 155
ylotfi Avatar answered Oct 15 '22 17:10

ylotfi


Nowadays is not possible to do what you want in one single step. You can do it in 3 steps:

  1. getting all the buckets of your GCP project.
  2. Get the labels of every bucket.
  3. Do the 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)
like image 31
Alex Riquelme Avatar answered Oct 15 '22 17:10

Alex Riquelme