Database Migration Service 提供了一个 REST API,用于以编程方式管理您的连接配置文件和迁移作业。REST API 由与创建和管理连接配置文件和迁移作业关联的资源定义。如需详细了解所有资源及其方法,请参阅
Database Migration Service API 参考文档。
当直接向 Database Migration Service REST API 发送请求时,必须创建正确的请求形式,以经过身份验证的用户的身份授权请求,并处理返回的所有响应。本文档中的许多任务提供了使用
c网址 的 API 示例。
您必须具有使用 Database Migration Service API 所需的权限。这些权限包括:
Database Migration Admin:拥有此权限,用户可以获取用于管理连接配置文件和迁移作业的所有 Database Migration Service 资源。
Database Migration Viewer:利用此权限,用户对 Database Migration Service 中的连接配置文件和迁移作业拥有只读权限。
Cloud SQL 管理员:拥有此权限的用户可以在 Database Migration Service 中创建和管理 Cloud SQL 的连接配置文件和迁移作业。
Google Cloud 提供 Identity and Access Management (IAM),可让您授予对特定 Google Cloud 资源的精细访问权限,并防止对其他资源进行不必要的访问。您可以使用 Google Cloud 控制台、API 的 IAM 方法或 Google Cloud CLI 来获取和设置 Database Migration Service 的权限。如需了解详情,请参阅
授予、更改和撤消对资源的访问权限。
通过文件提供 JSON 数据
使用 API 与 cURL 时,可以使用命令行提供属性值。如果使用的是敏感值(如密码或安全密钥),则在命令行中提供这些值会带来安全风险。为了提高安全性,您可以创建包含用于 API 调用的 JSON 数据的文件,并在命令行中提供该文件的路径。
如需通过文件向您的 cURL API 调用提供 JSON 数据,请执行以下操作:
为 --data 字段创建一个文件,用单引号将所有内容括起来。
包含大括号,但不包含单引号。
在命令行中,提供以 @ 字符开头文件路径作为 --data 参数:
--data@<path-to-file>/<filename>
处理长时间运行的 API 调用
某些 API 调用(如对象创建)可能需要一些时间才能完成。由于 API 是异步的,因此即使操作仍在进行中,调用也会立即返回。如果后续 API 调用使用新创建的对象,则必须等到第一个操作完成后才能继续。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[[["\u003cp\u003eThe Database Migration Service provides a REST API for managing connection profiles and migration jobs programmatically, with detailed resources and methods available in the API Reference.\u003c/p\u003e\n"],["\u003cp\u003eUsers can interact with the API directly using tools like cURL, requiring proper request formatting, user authentication, and response handling, or indirectly via the Google Cloud CLI and Google Cloud Console, which simplify these processes.\u003c/p\u003e\n"],["\u003cp\u003eTo utilize the API, it must be enabled in the Google Cloud project, either through the Google Cloud console or via the gcloud CLI, and the user must possess appropriate permissions, such as Database Migration Admin, Viewer, or Cloud SQL Admin.\u003c/p\u003e\n"],["\u003cp\u003eFor security, sensitive data for API calls can be stored in a file and referenced in cURL commands, instead of being directly entered in the command line.\u003c/p\u003e\n"],["\u003cp\u003eThe API handles long-running operations asynchronously, allowing users to manage operations via the API, such as showing details, listing, or deleting them.\u003c/p\u003e\n"]]],[],null,["# Use the Database Migration Service API\n\nDatabase Migration Service provides a REST API for administering your connection profiles and migration jobs programmatically. The REST API is defined by resources associated with creating and managing connection profiles and migration jobs. For details about all resources\nand their methods, see the\n[Database Migration Service API Reference](/database-migration/docs/reference/rest).\n\nWhen you send requests directly to the Database Migration Service REST API, you must create the\ncorrect form of the request, authorize the request as an authenticated user, and\nprocess any responses returned. Many of the tasks in this documentation provide\nAPI examples using [cURL](http://curl.haxx.se).\n\nFor more examples of using the APIs, see the page for the request in the\n[Database Migration Service API Reference](/database-migration/docs/reference/rest). Each page has\nexamples calling the API in several programming languages, as well as a\nrequest-specific Explorer to help you see what goes into a well-formed request\nand what to expect in the response.\n\nYou're also using the Database Migration Service API, indirectly, when you use any of the\nfollowing ways of administering instances:\n\n- [Google Cloud CLI](/sdk): A command-line interface (CLI) that you can use to work with your instances.\n\n- [Google Cloud console](https://console.cloud.google.com/): A graphical web interface.\n\nThe advantage of using these methods, especially the\nGoogle Cloud console, is that they can greatly simplify administering your\nconnection profiles and migration jobs (depending on your use case). If you're just starting out with\nDatabase Migration Service, then we recommend that you start with one of these tools first\nbefore working with the REST API directly.\n\nEnable the API\n--------------\n\nTo use the Database Migration Service API, you need to enable it: \n\n### Console\n\n1. [Enable the Database Migration Service API](https://console.cloud.google.com/apis/library/datamigration.googleapis.com).\n2. Select your project.\n3. Select **Continue**.\n\n### gcloud\n\n1. Enter the following to display the project IDs for your Google Cloud projects: \n\n ```bash\n gcloud projects list\n ```\n2. Set your default project: \n\n ```bash\n gcloud config set project YOUR_PROJECT_ID\n ```\n3. Enable the Cloud SQL Admin API: \n\n ```bash\n gcloud services enable datamigration.googleapis.com\n ```\n\nPermissions\n-----------\n\nYou must have the required permissions to use the Database Migration Service API. These permissions include:\n\n- Database Migration Admin: With this permission, a user gains access to all resources of Database Migration Service used to manage connection profiles and migration jobs.\n- Database Migration Viewer: With this permission, a user has read-only access to connection profiles and migration jobs in Database Migration Service.\n- Cloud SQL Admin: With this permission, a user can create and manage connection profiles and migration jobs for Cloud SQL in Database Migration Service.\n\nGoogle Cloud offers Identity and Access Management (IAM), which lets you give more granular access to specific Google Cloud resources and prevents unwanted access to other resources. You can get and set permissions for Database Migration Service using the Google Cloud console, the IAM methods of the API, or the Google Cloud CLI. For more information, see\n[Granting, changing, and revoking access to resources](/iam/docs/granting-changing-revoking-access).\n\nProvide JSON data from a file\n-----------------------------\n\nWhen you use the API with cURL, you provide property values using the command\nline. If you're working with sensitive values such as passwords or security\nkeys, providing them on the command line poses a security risk. For increased\nsecurity, you can create a file containing the JSON data for the API call, and\nprovide the path to the file on the command line.\n\nTo provide JSON data to your cURL API call from a file:\n\n1. Create a file containing everything enclosed in single quotes for the\n `--data` field.\n\n Include the curly brackets, but don't include the single quotes.\n2. At the command line, provide the path to the file, preceded by the\n `@` character, as the `--data` parameter:\n\n ```bash\n --data @\u003cpath-to-file\u003e/\u003cfilename\u003e\n ```\n\n \u003cbr /\u003e\n\nWork with long-running API calls\n--------------------------------\n\nSome API calls, such as object creation, can take some time to complete. Because\nthe API is asynchronous, the call returns immediately, even if the operation is\nstill in progress. If a subsequent API call uses the newly created object, then you\nmust wait for the first operation to complete before proceeding.\n\nYou can use the Database Migration Service API to\n[manage operations](/sdk/gcloud/reference/database-migration/operations). This includes:\n\n- [Showing details about an operation](/sdk/gcloud/reference/database-migration/operations/describe)\n- [Listing operations for a region](/sdk/gcloud/reference/database-migration/operations/list)\n- [Deleting an operation](/sdk/gcloud/reference/database-migration/operations/delete)\n\nDatabase Migration Service and Google APIs Discovery Service\n------------------------------------------------------------\n\n[Google APIs Discovery Service](/discovery) is a service that you can use to discover Google APIs. For example, when you use the [Google APIs Explorer](/apis-explorer) tool, you're using the Discovery Service.\n\n\u003cbr /\u003e\n\nIn the Discovery Service, Database Migration Service is represented as \"datamigration\" (for example:\nhttps://**datamigration**.googleapis.com/$discovery/rest?version=v1)."]]