I am trying to set up the cloud scheduler to trigger the cloud function. I set the frequency to 0 9 1 * *
, target type http
, url https://europe-west2-project_name.cloudfunctions.net/xxxxx
.
And also I have added the `OIDC token, created the service account that invoke cloud function. When I try to run my cloud scheduler manually it fails and I get the following error
{
httpRequest: {
status: 500
}
insertId: "132gtrbf1pw9rq"
jsonPayload: {
@type: "type.googleapis.com/google.cloud.scheduler.logging.AttemptFinished"
jobName: "projects/project_name/locations/europe-west2/jobs/cloud-scheduler-job"
status: "INTERNAL"
targetType: "HTTP"
url: "https://europe-west2-project_name.cloudfunctions.net/xxxx"
}
logName: "projects/project_name/logs/cloudscheduler.googleapis.com%2Fexecutions"
receiveTimestamp: "2023-06-28T07:37:31.684339045Z"
resource: {
labels: {3}
type: "cloud_scheduler_job"
}
severity: "ERROR"
timestamp: "2023-06-28T07:37:31.684339045Z"
}
I have been stuck with this issue for 2 days and I don't know how to resolve it. can anyone please help me?
updated:
{
insertId: "-r6s7cscbc"
logName: "projects/project-name/logs/cloudaudit.googleapis.com%2Fdata_access"
protoPayload: {
@type: "type.googleapis.com/google.cloud.audit.AuditLog"
authenticationInfo: {
principalEmail: "service-1234567890@gcf-admin-robot.iam.gserviceaccount.com"
}
requestMetadata: {
requestAttributes: {
time: "2023-06-27T18:17:21.627074Z"
auth: {
}}
destinationAttributes: {0}}
serviceName: "cloudfunctions.googleapis.com"
methodName: "google.cloud.functions.v1.CloudFunctionsService.GetFunction"
authorizationInfo: [
0: {
resource: "projects/project-name/locations/europe-west2/functions/cloud-function-name"
permission: "cloudfunctions.functions.get"
granted: true
resourceAttributes: {
}}]
resourceName: "projects/project-name/locations/europe-west2/functions/cloud-function-name"
resourceLocation: {
currentLocations: [
0: "europe-west2"
]}}
receiveTimestamp: "2023-06-27T18:17:21.927721083Z"
resource: {
labels: {
function_name: "cloud-function-name"
project_id: "project-name"
region: "europe-west2"
}
type: "cloud_function"
}
severity: "INFO"
timestamp: "2023-06-27T18:17:21.556303Z"
}
updated: python code
from google.cloud import storage
import zipfile
import datetime
import os
def move_files(data, context):
source_bucket_name = 'source-input'
destination_bucket_name = 'destination-input'
client = storage.client()
source_bucket = client.get_bucket(source_bucket_name)
destination_bucket = client.get_bucket(destination_bucket_name)
three_month_file = datetime.datetime.now() - datetime.timedelta(days=90)
for blob in source_bucket.list_blobs(prefix='files/data/document'):
blob_updated = blob.updated
if blob_updated < three_month_file:
#Create temporary local directory to store the zipped file
temp_dir = 'temp'
os.makedirs(temp_dir,exist_ok=True)
#Store the file to the temporary local directory
blob.store_filename(os.path.join(temp_dir,blob.name))
#Zip the file
zip_Name = os.path.join(temp_dir,f"{blob.name}.zip")
with zipfile.ZipFile(zip_Name,'w') as zipfile_obj:
zipfile_obj.write(os.path.join(temp_dir,blob.name),blob.name)
#move the zipeed file to destination bucket
destination_blob = destination_bucket.blob(f"{blob.name}.zip")
destination_blob.upload_files(zip_Name)
#Delete the original file and zipped file
os.remove(os.path.join(temp_dir,blob.name))
os.remove(zip_Name)
#Delete the original from the source bucket
blob.delete()
#Remove the temporary local directory
os.rmdir(temp_dir)