Oksana Ok
Oksana Ok

Reputation: 555

Google Cloud Storage - Move file from one folder to another - By using Python

I would like to move a list of file from google storage to another folder:

storage_client = storage.Client()
count = 0

# Retrieve all blobs with a prefix matching the file.
bucket=storage_client.get_bucket(BUCKET_NAME)
# List blobs iterate in folder 
blobs=bucket.list_blobs(prefix=GS_FILES_PATH, delimiter='/') # Excluding folder inside bucket
for blob in blobs:
if fnmatch.fnmatch(blob.name, FILE_PREF):
         WHAT CAN GO HERE?
         count += 1   

The only useful information which I found in Google Documentation is:

By this documentation, the only method is to copy from one folder to another and delete it.

  1. Any way to actually MOVE files?
  2. What is the best way to move all the files based on PREFIX like *BLABLA*.csv

P.S. Do not want to use

Upvotes: 4

Views: 7577

Answers (2)

alefh
alefh

Reputation: 11

You could use method rename_blob in google.cloud.storage.Bucket, this function moves the file and deletes the old one. It's necessary that names of files(blobs) are different
Take a look at the code below:

from google.cloud import storage  

dest_bucket = storage_client.create_bucket(bucket_to)
source_bucket = storage_client.get_bucket(bucket_from)
blobs = source_bucket.list_blobs(prefix=GS_FILES_PATH, delimiter='/') #assuming this is tested

for blob in blobs:
    if fnmatch.fnmatch(blob.name, FILE_PREF): #assuming this is tested
        source_bucket.rename_blob(blob,dest_bucket,new_name = blob.name)

Upvotes: 1

marian.vladoi
marian.vladoi

Reputation: 8066

This could be a possible solution, as there is no move_blob function in google.cloud.storage:

from google.cloud import storage  

dest_bucket = storage_client.create_bucket(bucket_to)
source_bucket = storage_client.get_bucket(bucket_from)
blobs = source_bucket.list_blobs(prefix=GS_FILES_PATH, delimiter='/') #assuming this is tested

for blob in blobs:
    if fnmatch.fnmatch(blob.name, FILE_PREF): #assuming this is tested
        source_bucket.copy_blob(blob,dest_bucket,new_name = blob.name)
        source_bucket.delete_blob(blob.name)

Upvotes: 3

Related Questions