You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用.sql文件导入SQL实例时遇报错:需且仅需指定一个文件路径

问题描述

我用以下Ruby代码尝试通过Google Cloud SQL Admin API导入SQL文件:

def import_sql()
      #
      # Authtication completed
      # Authenticated obj = sqladmin
      #
       
      sql_import=Google::Apis::SqladminV1beta4::ImportContext.new
    
      sql_import.database='postgres'
      sql_import.file_type ='SQL'
      sql_import.import_user ='postgres'
      sql_import.uri='gs://sample-bucket-name/sample.sql'
      
      operation=sqladmin.import_instance(instance_name,sql_import)
    
end

执行时触发错误:

Invalid request: One and only one file path must be specified when importing data

明明只指定了一个文件路径,却出现这个错误,求解决办法。

解决办法

这个错误是因为Google Cloud SQL Admin API V1beta4版本中,ImportContext的文件路径参数不能直接通过uri字段设置,需要嵌套在sql_import_config子对象里。API会忽略直接设置的uri,导致判定你未指定任何文件路径,从而抛出该错误。

修改后的代码如下:

def import_sql()
      #
      # Authtication completed
      # Authenticated obj = sqladmin
      #
       
      sql_import=Google::Apis::SqladminV1beta4::ImportContext.new
      # 将文件路径放在sql_import_config中
      sql_import.sql_import_config = Google::Apis::SqladminV1beta4::ImportContext::SqlImportConfig.new(
        uri: 'gs://sample-bucket-name/sample.sql'
      )
    
      sql_import.database='postgres'
      sql_import.file_type ='SQL'
      sql_import.import_user ='postgres'
      
      operation=sqladmin.import_instance(instance_name,sql_import)
    
end

调整后API能正确识别你指定的GCS文件路径,即可正常执行导入操作。

内容的提问来源于stack exchange,提问作者Jr Dna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 19:01:01