Check an import job
Stay organized with collections
Save and categorize content based on your preferences.
Check the state of an import job in Cloud KMS.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Check an import job\n\nCheck the state of an import job in Cloud KMS.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Import a key version into Cloud KMS](/kms/docs/importing-a-key)\n\nCode sample\n-----------\n\n### Go\n\n\nTo learn how to install and use the client library for Cloud KMS, see\n[Cloud KMS client libraries](/kms/docs/reference/libraries).\n\n\nTo authenticate to Cloud KMS, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tkms \"cloud.google.com/go/kms/apiv1\"\n \t\"cloud.google.com/go/kms/apiv1/kmspb\"\n )\n\n // checkStateImportJob checks the state of an ImportJob in KMS.\n func checkStateImportJob(w io.Writer, name string) error {\n \t// name := \"projects/PROJECT_ID/locations/global/keyRings/my-key-ring/importJobs/my-import-job\"\n\n \t// Create the client.\n \tctx := context.Background()\n \tclient, err := kms.https://cloud.google.com/go/docs/reference/cloud.google.com/go/kms/latest/apiv1.html#cloud_google_com_go_kms_apiv1_KeyManagementClient_NewKeyManagementClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to create kms client: %w\", err)\n \t}\n \tdefer client.Close()\n\n \t// Call the API.\n \tresult, err := client.GetImportJob(ctx, &kmspb.GetImportJobRequest{\n \t\tName: name,\n \t})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to get import job: %w\", err)\n \t}\n \tfmt.Fprintf(w, \"Current state of import job %q: %s\\n\", result.https://cloud.google.com/go/docs/reference/cloud.google.com/go/kms/latest/apiv1.html#cloud_google_com_go_kms_apiv1_CreateKeyHandleOperation_Name, result.State)\n \treturn nil\n }\n\n### Java\n\n\nTo learn how to install and use the client library for Cloud KMS, see\n[Cloud KMS client libraries](/kms/docs/reference/libraries).\n\n\nTo authenticate to Cloud KMS, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.cloud.kms.v1.https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.ImportJob.html;\n import com.google.cloud.kms.v1.https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.ImportJobName.html;\n import com.google.cloud.kms.v1.https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.KeyManagementServiceClient.html;\n import java.io.IOException;\n\n public class CheckStateImportJob {\n\n public void checkStateImportJob() throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n String projectId = \"your-project-id\";\n String locationId = \"us-east1\";\n String keyRingId = \"my-key-ring\";\n String importJobId = \"my-import-job\";\n checkStateImportJob(projectId, locationId, keyRingId, importJobId);\n }\n\n // Check the state of an import job in Cloud KMS.\n public void checkStateImportJob(\n String projectId, String locationId, String keyRingId, String importJobId)\n throws IOException {\n // Initialize client that will be used to send requests. This client only\n // needs to be created once, and can be reused for multiple requests. After\n // completing all of your requests, call the \"close\" method on the client to\n // safely clean up any remaining background resources.\n try (https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.KeyManagementServiceClient.html client = https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.KeyManagementServiceClient.html.create()) {\n // Build the parent name from the project, location, and key ring.\n https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.ImportJobName.html importJobName = https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.ImportJobName.html.of(projectId, locationId, keyRingId, importJobId);\n\n // Retrieve the state of an existing import job.\n https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.ImportJob.html importJob = client.getImportJob(importJobName);\n System.out.printf(\n \"Current state of import job %s: %s%n\", importJob.https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.ImportJob.html#com_google_cloud_kms_v1_ImportJob_getName__(), importJob.https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.ImportJob.html#com_google_cloud_kms_v1_ImportJob_getState__());\n }\n }\n }\n\n### Node.js\n\n\nTo learn how to install and use the client library for Cloud KMS, see\n[Cloud KMS client libraries](/kms/docs/reference/libraries).\n\n\nTo authenticate to Cloud KMS, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n //\n // TODO(developer): Uncomment these variables before running the sample.\n //\n // const projectId = 'my-project';\n // const locationId = 'us-east1';\n // const keyRingId = 'my-key-ring';\n // const importJobId = 'my-import-job';\n\n // Imports the Cloud KMS library\n const {KeyManagementServiceClient} = require('https://cloud.google.com/nodejs/docs/reference/kms/latest/overview.html');\n\n // Instantiates a client\n const client = new https://cloud.google.com/nodejs/docs/reference/kms/latest/overview.html();\n\n // Build the import job name\n const importJobName = client.importJobPath(\n projectId,\n locationId,\n keyRingId,\n importJobId\n );\n\n async function checkStateImportJob() {\n const [importJob] = await client.getImportJob({\n name: importJobName,\n });\n\n console.log(\n `Current state of import job ${importJob.name}: ${importJob.state}`\n );\n return importJob;\n }\n\n return checkStateImportJob();\n\n### Python\n\n\nTo learn how to install and use the client library for Cloud KMS, see\n[Cloud KMS client libraries](/kms/docs/reference/libraries).\n\n\nTo authenticate to Cloud KMS, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import kms\n\n\n def check_state_import_job(\n project_id: str, location_id: str, key_ring_id: str, import_job_id: str\n ) -\u003e None:\n \"\"\"\n Check the state of an import job in Cloud KMS.\n\n Args:\n project_id (string): Google Cloud project ID (e.g. 'my-project').\n location_id (string): Cloud KMS location (e.g. 'us-east1').\n key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring').\n import_job_id (string): ID of the import job (e.g. 'my-import-job').\n \"\"\"\n\n # Create the client.\n client = kms.https://cloud.google.com/python/docs/reference/cloudkms/latest/google.cloud.kms_v1.services.key_management_service.KeyManagementServiceClient.html()\n\n # Retrieve the fully-qualified import_job string.\n import_job_name = client.https://cloud.google.com/python/docs/reference/cloudkms/latest/google.cloud.kms_v1.services.key_management_service.KeyManagementServiceClient.html#google_cloud_kms_v1_services_key_management_service_KeyManagementServiceClient_import_job_path(\n project_id, location_id, key_ring_id, import_job_id\n )\n\n # Retrieve the state from an existing import job.\n import_job = client.https://cloud.google.com/python/docs/reference/cloudkms/latest/google.cloud.kms_v1.services.key_management_service.KeyManagementServiceClient.html#google_cloud_kms_v1_services_key_management_service_KeyManagementServiceClient_get_import_job(name=import_job_name)\n\n print(f\"Current state of import job {import_job.name}: {import_job.state}\")\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=kms)."]]