1

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.

m4v21
  • 21
  • 3
  • Do you mean Unicode, unicast is something very different - it's also 'lowercase' not undercast - either way take a look at this from our sister site; http://stackoverflow.com/questions/152514/how-to-rename-all-folders-and-files-to-lowercase-on-linux – Chopper3 Nov 01 '16 at 10:45
  • Thanks for your reply @Chopper3. I have edited the question to say 'lowercase' rather than 'undercast'.I would simply like a filename like N845-001.JPG to be renamed to n845-001.jpg using gsutil. My knowledge of gsutil is very basic, but I don't think it will accept the command as suggested in your link. – m4v21 Nov 01 '16 at 11:06

2 Answers2

2

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")
    
1

One workaround that you can use is to mount the bucket to a linux machine using GCSFuse and than use tr [:upper:] [:lower:] in a for loop to change the object names e.g:

 for i in *; do mv $i `echo $i | tr [:upper:] [:lower:]`; done
Faizan
  • 1,408
  • 10
  • 17
  • Thanks for your reply, @Faizan. Sorry for the late reply; only just saw the notification. I will definitely give this a try. – m4v21 Nov 22 '16 at 10:25