1

We have many videos uploaded to GCP Cloud Storage. We need to change file name and remove [].

Asking if there is a good solution.

file example: gs://xxxxxx/xxxxxx/[BlueLobster] Saint Seiya The Lost Canvas - 06 [1080p].mkv

Bryan L
  • 550
  • 1
  • 9

2 Answers2

2

You can't rename file in Cloud Storage. Renaming file equals to copying the file with a new name and to delete the older name.

It will take time if you have a lot of (large) files, but it's not impossible.

guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76
2

Based on the given scenario, you want to bulk rename all the filenames with ¨[]¨. Based on this documentation, gsutil interprets these characters as wildcards. gsutil does not support this currently.

There´s a way to handle this kind of request by using a custom script to rename all the files with ´[´.

You may use any programming languages that have Cloud Storage client libraries. For this instructions, we´ll be using Python for the custom script.

  1. On your Google Cloud Console, Click the Activate Cloud Shell on the top right of the Google Cloud Console beside the question mark sign. For more information, you may refer here.

  2. On your Cloud Shell, Install the Python client library by using this command:

pip install --upgrade google-cloud-storage

For more information, please refer on this documentation.

  1. After the installation of client library, launch the Cloud Shell Editor by clicking the Open Editor on the top right side of the Cloud Shell. You may refer here for more information.

  2. On your Cloud Shell Editor, click the File menu and choose New File. Name it script.py. Click Ok.

  3. This code assumes that all the objects on your bucket have the same name from the sample you provided.:

import re
from google.cloud import storage

storage_client = storage.Client()

bucket_name = "my_bucket"
bucket = storage_client.bucket(bucket_name)

storage_client = storage.Client()

blobs = storage_client.list_blobs(bucket_name)
pattern = r"[\([{})\]]"
for blob in blobs:
    out_var = blob.name
    fixed_var = re.sub(pattern, '', blob.name)
    print(out_var + " " + fixed_var)
    new_blob = bucket.rename_blob(blob, fixed_var)
  1. Change the content of ¨my_bucket¨ to the name of your bucket.

  2. Click File and then Save or you can just press Ctrl + S.

  3. Go back to the terminal by clicking the Open Terminal on the top right section of the Cloud Shell Editor.

  4. Copy and paste this code to the editor:

python script.py
  1. To run the script, press the Enter key.
  2. Files that have brackets are now renamed.

The files aren´t renamed in the backend. Under the hood, it's more of being rewritten with a new name and it's due to object immutability. This will only copy the old files with a new name and removes the old file afterwards.

Marc Anthony B
  • 3,635
  • 2
  • 4
  • 19
  • I repeat my warning, the file aren't renamed, they are copied with the new name and the old ones deleted. It can take time (2 API call + processing duration), and cost (a little) Operation B type. – guillaume blaquiere Sep 26 '21 at 11:02