【发布时间】:2020-12-13 05:53:08
【问题描述】:
我正在尝试构建小功能(后来部署到云功能)以将 BAK 文件恢复到云 sql。此函数将由 cron 触发。 在阅读有关授权此 API 的文档时,我有点迷茫:https://cloud.google.com/sql/docs/sqlserver/import-export/importing#importing_data_from_a_bak_file_in
已创建包含此角色的服务帐户:Cloud SQL Admin、Storage Admin、Storage Object Admin、Storage Object Viewer,并在创建 Cloud Function 时从下拉列表中选择该服务帐户但不起作用。
阅读后还尝试生成 API 密钥:https://cloud.google.com/sql/docs/sqlserver/admin-api/how-tos/authorizing
所以我的 POST 网址变成了这样:
https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id/import?key=generatedAPIKey
但还是报错:
"error": {
"code": 401,
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"errors": [
{
"message": "Login Required.",
"domain": "global",
"reason": "required",
"location": "Authorization",
"locationType": "header"
}
],
"status": "UNAUTHENTICATED"
}
}
我需要为此使用 Oauth 2 吗?这是我在 Cloud Function 中的代码:
import http.client
import mimetypes
def restore_bak(request):
conn = http.client.HTTPSConnection("www.googleapis.com")
payload = "{\r\n \"importContext\":\r\n {\r\n \"fileType\": \"BAK\",\r\n \"uri\": \"gs://{bucket_name}/{backup_name}.bak\",\r\n \"database\": \"{database_name}\"\r\n }\r\n}\r\n"
headers = {
'Content-Type': 'application/json'
}
conn.request("POST", "/sql/v1beta4/projects/{project_id}/instances/{instance_name}/import", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
return(data.decode("utf-8"))
【问题讨论】:
标签: google-cloud-platform google-cloud-functions google-cloud-storage google-cloud-sql