鍵のメイン バージョンを更新する
もっと見る
このコードサンプルを含む詳細なドキュメントについては、以下をご覧ください。
コードサンプル
C#
Cloud KMS 用のクライアント ライブラリをインストールして使用する方法については、Cloud KMS クライアント ライブラリをご覧ください。
using Google.Cloud.Kms.V1;
public class UpdateKeySetPrimarySample
{
public CryptoKey UpdateKeySetPrimary(
string projectId = "my-project", string locationId = "us-east1", string keyRingId = "my-key-ring", string keyId = "my-key",
string keyVersionId = "123")
{
// Create the client.
KeyManagementServiceClient client = KeyManagementServiceClient.Create();
// Build the key name.
CryptoKeyName keyName = new CryptoKeyName(projectId, locationId, keyRingId, keyId);
// Call the API.
CryptoKey result = client.UpdateCryptoKeyPrimaryVersion(keyName, keyVersionId);
// Return the updated key.
return result;
}
}
Go
Cloud KMS 用のクライアント ライブラリをインストールして使用する方法については、Cloud KMS クライアント ライブラリをご覧ください。
import (
"context"
"fmt"
"io"
kms "cloud.google.com/go/kms/apiv1"
"cloud.google.com/go/kms/apiv1/kmspb"
)
// updateKeySetPrimary updates the primary key version on a Cloud KMS key.
func updateKeySetPrimary(w io.Writer, name, version string) error {
// name := "projects/my-project/locations/us-east1/keyRings/my-key-ring/cryptoKeys/my-key"
// version := "123"
// Create the client.
ctx := context.Background()
client, err := kms.NewKeyManagementClient(ctx)
if err != nil {
return fmt.Errorf("failed to create kms client: %v", err)
}
defer client.Close()
// Build the request.
req := &kmspb.UpdateCryptoKeyPrimaryVersionRequest{
Name: name,
CryptoKeyVersionId: version,
}
// Call the API.
result, err := client.UpdateCryptoKeyPrimaryVersion(ctx, req)
if err != nil {
return fmt.Errorf("failed to update key: %v", err)
}
fmt.Fprintf(w, "Updated key primary: %s\n", result.Name)
return nil
}
Java
Cloud KMS 用のクライアント ライブラリをインストールして使用する方法については、Cloud KMS クライアント ライブラリをご覧ください。
import com.google.cloud.kms.v1.CryptoKey;
import com.google.cloud.kms.v1.CryptoKeyName;
import com.google.cloud.kms.v1.KeyManagementServiceClient;
import java.io.IOException;
public class UpdateKeySetPrimary {
public void updateKeySetPrimary() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String locationId = "us-east1";
String keyRingId = "my-key-ring";
String keyId = "my-key";
String keyVersionId = "123";
updateKeySetPrimary(projectId, locationId, keyRingId, keyId, keyVersionId);
}
// Update a key's primary version.
public void updateKeySetPrimary(
String projectId, String locationId, String keyRingId, String keyId, String keyVersionId)
throws IOException {
// Initialize client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests. After
// completing all of your requests, call the "close" method on the client to
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the name from the project, location, key ring, and keyId.
CryptoKeyName cryptoKeyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);
// Create the key.
CryptoKey createdKey = client.updateCryptoKeyPrimaryVersion(cryptoKeyName, keyVersionId);
System.out.printf("Updated key primary version %s%n", createdKey.getName());
}
}
}
Node.js
Cloud KMS 用のクライアント ライブラリをインストールして使用する方法については、Cloud KMS クライアント ライブラリをご覧ください。
//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-east1';
// const keyRingId = 'my-key-ring';
// const keyId = 'my-key';
// const versionId = '123';
// Imports the Cloud KMS library
const {KeyManagementServiceClient} = require('@google-cloud/kms');
// Instantiates a client
const client = new KeyManagementServiceClient();
// Build the key name
const keyName = client.cryptoKeyPath(projectId, locationId, keyRingId, keyId);
async function updateKeySetPrimary() {
const [key] = await client.updateCryptoKeyPrimaryVersion({
name: keyName,
cryptoKeyVersionId: versionId,
});
console.log(`Set primary to ${versionId}`);
return key;
}
return updateKeySetPrimary();
PHP
Cloud KMS 用のクライアント ライブラリをインストールして使用する方法については、Cloud KMS クライアント ライブラリをご覧ください。
use Google\Cloud\Kms\V1\KeyManagementServiceClient;
function update_key_set_primary(
string $projectId = 'my-project',
string $locationId = 'us-east1',
string $keyRingId = 'my-key-ring',
string $keyId = 'my-key',
string $versionId = '123'
) {
// Create the Cloud KMS client.
$client = new KeyManagementServiceClient();
// Build the key name.
$keyName = $client->cryptoKeyName($projectId, $locationId, $keyRingId, $keyId);
// Call the API.
$updatedKey = $client->updateCryptoKeyPrimaryVersion($keyName, $versionId);
printf('Updated primary %s to %s' . PHP_EOL, $updatedKey->getName(), $versionId);
return $updatedKey;
}
Python
Cloud KMS 用のクライアント ライブラリをインストールして使用する方法については、Cloud KMS クライアント ライブラリをご覧ください。
def update_key_set_primary(project_id, location_id, key_ring_id, key_id, version_id):
"""
Update the primary version of a key.
Args:
project_id (string): Google Cloud project ID (e.g. 'my-project').
location_id (string): Cloud KMS location (e.g. 'us-east1').
key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring').
key_id (string): ID of the key to use (e.g. 'my-key').
version_id (string): ID of the key to make primary (e.g. '2').
Returns:
CryptoKey: Updated Cloud KMS key.
"""
# Import the client library.
from google.cloud import kms
# Create the client.
client = kms.KeyManagementServiceClient()
# Build the key name.
key_name = client.crypto_key_path(project_id, location_id, key_ring_id, key_id)
# Call the API.
updated_key = client.update_crypto_key_primary_version(request={'name': key_name, 'crypto_key_version_id': version_id})
print('Updated {} primary to {}'.format(updated_key.name, version_id))
return updated_key
Ruby
Cloud KMS 用のクライアント ライブラリをインストールして使用する方法については、Cloud KMS クライアント ライブラリをご覧ください。
# TODO(developer): uncomment these values before running the sample.
# project_id = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id = "my-key"
# version_id = "123"
# Require the library.
require "google/cloud/kms"
# Create the client.
client = Google::Cloud::Kms.key_management_service
# Build the parent key name.
key_name = client.crypto_key_path project: project_id,
location: location_id,
key_ring: key_ring_id,
crypto_key: key_id
# Call the API.
updated_key = client.update_crypto_key_primary_version name: key_name, crypto_key_version_id: version_id
puts "Updated primary #{updated_key.name} to #{version_id}"
次のステップ
他の Google Cloud プロダクトに関連するコードサンプルの検索およびフィルタ検索を行うには、Google Cloud のサンプルをご覧ください。