Google Cloud Functions 为数据存储实体安排导出
Google Cloud Function to schedule exports for datastore entities
我们在 GCP AppEngine 中托管了一个应用程序。此应用程序将图像和其他数据保存在数据存储区中。图像是分离的实体(种类),例如 Kind1 和 Kind2。我们只需要导出这两种实体类型并将导出存储在名为 datastore-exports 的存储桶中。我们已经通过控制台手动导出了这两种实体类型。我们想创建一个云函数,可以每天每 24 小时导出上述两个数据存储实体类型。我需要有关文件和代码逻辑的帮助才能实现。
下面是我遇到的两个例子,它们在某种程度上是我们想要完成的。
我看到他们正在使用 firestore HERE 这样做。
我也看过这个文档 HERE 但我们需要使用 python 3.
任何对 node.js 或 python3 方法的帮助将不胜感激。
谢谢,
我试图重现您的用例:
运行 命令:
gcloud app decribe
# .......
#locationId: europe-west2
确保您的导出存储桶和云函数部署在同一位置。
您的云函数将使用 App Engine 默认服务帐户。
PROJECT_ID@appspot.gserviceaccount.com
为此服务帐户分配角色 Datastore Import Export Admin
(我建议为您的云功能创建一个新的服务帐户,而不是使用 App Engine 默认服务帐户。)
创建云函数:
a.main.py
def export_datastore(request):
import google.auth
import google.auth.transport.requests
import json
import requests
#Get the Access Token
creds, project_id = google.auth.default()
auth_req = google.auth.transport.requests.Request()
creds.refresh(auth_req)
token = creds.token
output_url_prefix = 'gs://your-bucket'
url = 'https://datastore.googleapis.com/v1/projects/{}:export'.format(project_id)
#We export all kinds and all namespaces
entity_filter = {
'kinds': [],
'namespace_ids': []
}
request = {
'project_id': project_id,
'output_url_prefix': output_url_prefix,
'entity_filter': entity_filter
}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
'Accept': 'application/json'
}
#Call the API to make the Datastore export
r = requests.post(url, data=json.dumps(request), headers=headers)
print(r.json())
return('Export command sent')
b。 requirements.txt
# Function dependencies, for example:
google-auth
- 使用Google Cloud Scheduler 每24小时调用一次云函数。
我们在 GCP AppEngine 中托管了一个应用程序。此应用程序将图像和其他数据保存在数据存储区中。图像是分离的实体(种类),例如 Kind1 和 Kind2。我们只需要导出这两种实体类型并将导出存储在名为 datastore-exports 的存储桶中。我们已经通过控制台手动导出了这两种实体类型。我们想创建一个云函数,可以每天每 24 小时导出上述两个数据存储实体类型。我需要有关文件和代码逻辑的帮助才能实现。
下面是我遇到的两个例子,它们在某种程度上是我们想要完成的。
我看到他们正在使用 firestore HERE 这样做。
我也看过这个文档 HERE 但我们需要使用 python 3.
任何对 node.js 或 python3 方法的帮助将不胜感激。
谢谢,
我试图重现您的用例:
运行 命令:
gcloud app decribe # ....... #locationId: europe-west2
确保您的导出存储桶和云函数部署在同一位置。
您的云函数将使用 App Engine 默认服务帐户。
PROJECT_ID@appspot.gserviceaccount.com
为此服务帐户分配角色
Datastore Import Export Admin
(我建议为您的云功能创建一个新的服务帐户,而不是使用 App Engine 默认服务帐户。)
创建云函数:
a.
main.py
def export_datastore(request):
import google.auth
import google.auth.transport.requests
import json
import requests
#Get the Access Token
creds, project_id = google.auth.default()
auth_req = google.auth.transport.requests.Request()
creds.refresh(auth_req)
token = creds.token
output_url_prefix = 'gs://your-bucket'
url = 'https://datastore.googleapis.com/v1/projects/{}:export'.format(project_id)
#We export all kinds and all namespaces
entity_filter = {
'kinds': [],
'namespace_ids': []
}
request = {
'project_id': project_id,
'output_url_prefix': output_url_prefix,
'entity_filter': entity_filter
}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
'Accept': 'application/json'
}
#Call the API to make the Datastore export
r = requests.post(url, data=json.dumps(request), headers=headers)
print(r.json())
return('Export command sent')
b。 requirements.txt
# Function dependencies, for example:
google-auth
- 使用Google Cloud Scheduler 每24小时调用一次云函数。