简体   繁体   English

云 Function 将数据从云存储桶导入 CloudSQL 但已获取架构存在错误

[英]Cloud Function to import data into CloudSQL from cloud storage bucket but getting already schema exist error

I'm trying to import data into CloudSQL instance from cloud storage bucket using cloud function.我正在尝试使用云 function 从云存储桶将数据导入 CloudSQL 实例。

How can i delete schema's before importing the data using a single cloud function?如何在使用单个云 function 导入数据之前删除架构?

I am using Node.js in cloud function.我在云 function 中使用 Node.js。

error:错误:

error: exit status 3 stdout(capped at 100k bytes): SET SET SET SET SET set_config ------------ (1 row) SET SET SET SET stderr: ERROR: schema "<  >" already exists

https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/instances/import in below code where do i need to put delete all existing schema's apart from public schema? https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/instances/import在下面的代码中我需要在哪里删除除public模式之外的所有现有模式?

Entry point: importDatabase入口点:importDatabase

index.js索引.js

    const {google} = require('googleapis');
    const {auth} = require("google-auth-library");
    var sqlAdmin = google.sqladmin('v1beta4');
    
    exports.importDatabase = (_req, res) => {
    async function doIt() {
    const authRes = await auth.getApplicationDefault();
    let authClient = authRes.credential;
      var request = {

        project: 'my-project',  // TODO: Update placeholder value.
    

        instance: 'my-instance',  // TODO: Update placeholder value.
    
        resource: {
          importContext: {

          kind: "sql#importContext",
          fileType: "SQL", // CSV
   
          uri: <bucket path>,

          database: <database-name>
          // Options for importing data as SQL statements.
          // sqlimportOptions: {
          //   /**
        },
    
        auth: authClient,
      };
    
      sqladmin.instances.import(request, function(err, result) {
      if (err) {
        console.log(err);
      } else {
        console.log(result);
      }
      res.status(200).send("Command completed", err, result); 
    });
  }

      doIt();
    
    };

package.json package.json

{
  "name": "import-database",
  "version": "0.0.1",
  "dependencies": {
    "googleapis": "^39.2.0",
    "google-auth-library": "3.1.2"
  }
}

The error looks to be occurring due to a previous aborted import managed to transfer the "schema_name" schema, and then this subsequent import was done without first re-initializing the DB,check helpful document on Cloud SQL import One way to prevent this issue is to change the create statements in the SQL file from:该错误看起来是由于先前中止的导入设法传输了“schema_name”架构而发生的,然后在没有首先重新初始化数据库的情况下完成了后续导入,请查看云上的帮助文档 SQL import防止此问题的一种方法是将 SQL 文件中的创建语句从:

CREATE SCHEMA schema_name; to CREATE SCHEMA IF NOT EXISTS schema_name; CREATE SCHEMA IF NOT EXISTS schema_name;

As far the removing of currently created schema is considered by default, only user or service accounts with the Cloud SQL Admin (roles/cloudsql.admin) or Owner (roles/owner) role have the permission to delete a Cloud SQL instance,please check the helpful document on cloudsql.instances.delete to help you understand the next steps.You can also define an IAM custom role for the user or service account that includes the cloudsql.instances.delete permission.由于默认考虑删除当前创建的模式,只有具有 Cloud SQL Admin (roles/cloudsql.admin) 或 Owner (roles/owner) 角色的用户或服务帐户才有权删除 Cloud SQL 实例,请检查有关cloudsql.instances.delete的有用文档可帮助您了解后续步骤。您还可以为用户或服务帐户定义 IAM 自定义角色,其中包含 cloudsql.instances.delete 权限。 This permission is supported in IAM custom roles. IAM 自定义角色支持此权限。

As a best practice for import export operations , we recommend that you adopt the principle of least privilege, which in this case would mean creating a custom role and adding that specific permission and assigning it to your service account.作为导入导出操作的最佳实践,我们建议您采用最小权限原则,在这种情况下,这意味着创建自定义角色并添加该特定权限并将其分配给您的服务帐户。 Alternatively, the service account could be given the “Cloud SQL Admin” role, or the “Cloud Composer API Service Agent” role, which include this permission, and would therefore allow you to execute this command.或者,可以为服务帐户授予“Cloud SQL Admin”角色或“Cloud Composer API Service Agent”角色,其中包括此权限,因此允许您执行此命令。

NOTE:It is recommended and advised to revalidate any delete actions performed as this may lead to loss of useful data.注意:建议并建议重新验证执行的任何删除操作,因为这可能会导致有用数据丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM