Move File from One GCP Bucket to Another using Python
In this tutorial, we will learn how to move an object (blob) from one Google Cloud Storage bucket to another using Python.
Moving a file in GCS is essentially a copy and delete operation.
Python Code to Move File
We will use the copy_blob() method of the bucket object followed by the delete() method.
Python
# Import the packages
from dotenv import load_dotenv
import os
from google.oauth2 import service_account
from google.cloud import storage
def main() :
# Loads environment variables from a .env file
load_dotenv()
# Use environment variables from .env file
credentials_path = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
project_name = os.getenv("project_id")
# Get the service account credentials
credentials = service_account.Credentials.from_service_account_file(credentials_path)
# Create the Storage client
client = storage.Client(credentials=credentials, project=project_name)
# Define the bucket name and file details
source_bucket_name = "source-bucket-via-python-sdk"
# Create a bucket object
source_bucket = client.bucket(source_bucket_name)
# Get the source blob object
source_blob = source_bucket.blob("Dummy.pdf")
# Define the destination bucket name and blob name
destination_bucket_name = "destination-bucket-via-python-sdk"
destination_blob_name = "Dummy.pdf"
destination_bucket = client.bucket(destination_bucket_name)
# Copy the blob to the destination bucket
# This creates a copy and keeps the original
new_blob = source_bucket.copy_blob(source_blob, destination_bucket, destination_blob_name)
# Now delete the original blob from the source bucket
source_blob.delete()
print(f"File {destination_blob_name} moved from {source_bucket_name} to {destination_bucket_name}.")
# Run the main function
if __name__ == "__main__":
main() Key Steps Explained
- copy_blob() creates a duplicate of the source file in the destination bucket.
- delete() removes the original file after the copy is successful.
- The move operation is completed only after both steps succeed.
Note: Cloud Storage performs the copy server-side, which is highly efficient even for large objects.