I'm encountering a 'NotFound' error while attempting to use the Blob.rewrite() method to copy files from one Google Cloud Storage bucket to another within a Google Cloud Function. The error message I'm receiving is as follows:
NotFound: 404 POST https://storage.googleapis.com/storage/v1/b/ca-app-corp-finance-dev-444/o/process%2Ffin_essbase%2Fdata%2Fincoming%2Fessbase%2Fkey.ppk/rewriteTo/b/gcf-v2-uploads-632430838198-us-east1/o/key.ppk?prettyPrint=false: No such object: ca-app-corp-finance-dev-444/process/fin_essbase/data/incoming/essbase/key.ppk
Here's the detailed description of my setup and the problem:
I've set up a Google Cloud Function that triggers whenever a new file is uploaded to a source bucket (gcf-v2-uploads-632430838198-us-east1). The intention is to copy the uploaded file to a destination bucket (ca-app-corp-finance-dev-444) under the specific path process/fin_essbase/data/incoming/essbase/. To achieve this, I'm using the Blob.rewrite() method within the function. Below is an excerpt from my Python code within the Cloud Function:
from google.cloud import storage
def transfer_file(data, context):
source_bucket_name = "gcf-v2-uploads-632430838198-us-east1"
destination_bucket_name = "ca-app-corp-finance-dev-444"
target_path = "process/fin_essbase/data/incoming/essbase/"
filename = "key.ppk"
storage_client = storage.Client()
source_bucket = storage_client.bucket(source_bucket_name)
destination_bucket = storage_client.bucket(destination_bucket_name)
source_blob = source_bucket.blob(filename)
destination_blob = destination_bucket.blob(target_path + filename)
# Check if the destination blob already exists
if destination_blob.exists():
print(f"Destination blob {destination_blob.name} already exists.")
else:
try:
# Perform a rewrite operation to copy the content
token = source_blob.rewrite(destination_blob)
# Wait for the rewrite to complete
token.result()
# Delete the source blob
source_blob.delete()
print(f"File {filename} transferred successfully.")
except Exception as e:
print(f"An error occurred: {e}")
I've verified the existence of the source object in the source bucket and ensured that the bucket names and file paths are accurate. The service account associated with the Cloud Function has the necessary permissions for reading from the source bucket and writing to the destination bucket.
Despite this, I'm encountering the 'NotFound' error. Could there be any potential misconfigurations or additional factors causing this issue? I'm hoping to receive insights or guidance on how to troubleshoot and resolve this problem.