1
- # Copyright 2021 Google LLC
1
+ # Copyright 2021 Google LLC All Rights Reserved.
2
2
#
3
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
4
# you may not use this file except in compliance with the License.
5
5
# You may obtain a copy of the License at
6
6
#
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
8
#
9
9
# Unless required by applicable law or agreed to in writing, software
10
10
# distributed under the License is distributed on an "AS IS" BASIS,
@@ -32,12 +32,12 @@ def set(self, url, content):
32
32
33
33
# The default cache (file_cache) is unavailable when using oauth2client >= 4.0.0 or google-auth,
34
34
# and it will log worrisome messages unless given another interface to use.
35
- datastore = build (' datastore' , 'v1' , cache = MemoryCache ())
36
- project_id = os .environ .get (' GCP_PROJECT' )
35
+ datastore = build (" datastore" , "v1" , cache = MemoryCache ())
36
+ project_id = os .environ .get (" GCP_PROJECT" )
37
37
38
38
39
39
def datastore_export (event , context ):
40
- ''' Triggers a Datastore export from a Cloud Scheduler job.
40
+ """ Triggers a Datastore export from a Cloud Scheduler job.
41
41
42
42
Args:
43
43
event (dict): event[data] must contain a json object encoded in
@@ -46,26 +46,28 @@ def datastore_export(event, context):
46
46
and 'namespaceIds' values.
47
47
context (google.cloud.functions.Context): The Cloud Functions event
48
48
metadata.
49
- '''
49
+ """
50
50
51
- json_data = json .loads (base64 .b64decode (event ['data' ]).decode ('utf-8' ))
52
- bucket = json_data ['bucket' ]
51
+ if "data" in event :
52
+ # Triggered via Cloud Scheduler, decode the inner data field of the json payload.
53
+ json_data = json .loads (base64 .b64decode (event ["data" ]).decode ("utf-8" ))
54
+ else :
55
+ # Otherwise, for instance if triggered via the Cloud Console on a Cloud Function, the event is the data.
56
+ json_data = event
57
+
58
+ bucket = json_data ["bucket" ]
53
59
entity_filter = {}
54
60
55
- if ' kinds' in json_data :
56
- entity_filter [' kinds' ] = json_data [' kinds' ]
61
+ if " kinds" in json_data :
62
+ entity_filter [" kinds" ] = json_data [" kinds" ]
57
63
58
- if ' namespaceIds' in json_data :
59
- entity_filter [' namespaceIds' ] = json_data [' namespaceIds' ]
64
+ if " namespaceIds" in json_data :
65
+ entity_filter [" namespaceIds" ] = json_data [" namespaceIds" ]
60
66
61
- request_body = {
62
- 'outputUrlPrefix' : bucket ,
63
- 'entityFilter' : entity_filter
64
- }
67
+ request_body = {"outputUrlPrefix" : bucket , "entityFilter" : entity_filter }
65
68
66
69
export_request = datastore .projects ().export (
67
- projectId = project_id ,
68
- body = request_body
70
+ projectId = project_id , body = request_body
69
71
)
70
72
response = export_request .execute ()
71
73
print (response )
0 commit comments