使用Google Cloud SQL中的CURL将数据导出到CSV文件

时间:2019-04-12 07:48:34

标签: curl google-cloud-platform export-to-csv google-cloud-sql

在终端中运行CURL时出现错误。错误是这样的:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "parseError",
    "message": "Parse Error"
   }
  ],
  "code": 400,
  "message": "Parse Error"
 }
}

这是我的CURL代码:

ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
     --header 'Content-Type: application/json' \
     --data '{"exportContext":
                {"fileType": "CSV",
                 "uri": "gs://project-initial-db/test_gutil.csv",
                 "databases": ["my_db"] },
                 "csvExportOptions": 
                     {"selectQuery":"SELECT * FROM `trans_channel` INTO OUTFILE "test_gutil.csv" CHARACTER SET "utf8mb4" FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY "\"" ESCAPED BY "\\" LINES TERMINATED BY "\n";"}}' \
   -X POST \
   https://www.googleapis.com/sql/v1beta4/projects/project-11/instances/project-db/export

有人知道这个错误吗?

我寻求使用Google Cloud SQL上的CURL导出到CSV的信息,但是我只得到了一些信息。

仅供参考,我已经设置了环境变量(GOOGLE_APPLICATION_CREDENTIALS),并且身份验证成功。

如果您能提供任何帮助,我将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

您只是在w字段“ cvsExportOptions”中缺少了一些东西,它看起来应该像这样:

"csvExportOptions": 
                 {"selectQuery":"[YOUR_QUERY]"}

您可能拥有更详细的信息here,在这里您还可以使用页面右侧的“尝试API”实用程序检查卷曲是否正确。

编辑

从“尝试此API”实用程序复制api调用,该实用程序用于测试,与为我使用的完全相同。尝试使用您的数据替换占位符

ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
     --header 'Content-Type: application/json' \
     --data '{
  "exportContext": {
    "fileType": "CSV",
    "uri": "gs://[YOUR_BUCKET]/[YOUR_PATH_TO_DUMP_FILE]",
    "databases": [
      "[YOUR_DATABASE]"
    ],
    "csvExportOptions": {
      "selectQuery": "[YOUR_QUERY]"
    }
  }
}' \
   -X POST \
   https://www.googleapis.com/sql/v1beta4/projects/[YOUR_PROJECT]/instances/[YOUR_INSTANCE]/export