Convert filenames to lowercase [Google Cloud Storage] [gsutil]

google-cloud-platformgsutil

We recently moved all product images from our website to Google Cloud Storage. Due to bad naming policies in the past the casing (lowercase, uppercase) in the file names does not always match the way file names are listed in our product database. This was never a problem for the website, but with GCS it now is.

The quickest workaround I see is to force the website to call all file names in lowercase. But this means I would have to change all file names (including extensions) in Cloud Storage to lowercase. I have been experimenting with gsutil, but have been unable to accomplish this so far.

Does anybody know whether there is a command that can do this?

TL;DR: Which gsutil command will change all file names to lowercase?

Thanks for your help.

Best Answer

The Cloud Storage Client Libraries can perform that job for you.

  1. In Python, first you install the library with:

pip install --upgrade google-cloud-storage

  1. Create a Service Account, download the service account key JSON file and set an environment variable:

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-file.json"

  1. With the code samples from here, you can build something like this (tested it):

    # Imports the Google Cloud client library
    from google.cloud import storage
    
    def rename_blobs(bucket_name):
        """Renames a group of blobs."""
        #storage client instance
        storage_client = storage.Client()
    
        #get bucket by name
        bucket = storage_client.get_bucket(bucket_name)
    
        #list files stored in bucket
        all_blobs = bucket.list_blobs()
    
        #Renaming all files to lowercase:
        for blob in all_blobs:
            new_name = str(blob.name).lower()
            new_blob = bucket.rename_blob(blob, new_name)
            print('Blob {} has been renamed to {}'.format( blob.name, new_blob.name))
    
    rename_blobs("my-new-bucket")