using System;
using System.Threading.Tasks;
using Amazon.KeyManagementService;
using Amazon.KeyManagementService.Model;
///<summary>/// List the AWS Key Management Service (AWS KMS) grants that are associated with/// a specific key.///</summary>publicclassListGrants{publicstaticasync Task Main(){// The identifier of the AWS KMS key to disable. You can use the// key Id or the Amazon Resource Name (ARN) of the AWS KMS key.var keyId = "1234abcd-12ab-34cd-56ef-1234567890ab";
var client = new AmazonKeyManagementServiceClient();
var request = new ListGrantsRequest
{
KeyId = keyId,
};
var response = new ListGrantsResponse();
do{
response = await client.ListGrantsAsync(request);
response.Grants.ForEach(grant =>
{
Console.WriteLine($"{grant.GrantId}");
});
request.Marker = response.NextMarker;
}
while (response.Truncated);
}
}
如需 API 詳細資訊,請參閱 AWS SDK for .NET API 參考中的 ListGrants。
/**
* Asynchronously displays the grant IDs for the specified key ID.
*
* @param keyId the ID of the AWS KMS key for which to list the grants
* @return a {@link CompletableFuture} that, when completed, will be null if the operation succeeded, or will throw a {@link RuntimeException} if the operation failed
* @throws RuntimeException if there was an error listing the grants, either due to an {@link KmsException} or an unexpected error
*/public CompletableFuture<Object> displayGrantIdsAsync(String keyId){
ListGrantsRequest grantsRequest = ListGrantsRequest.builder()
.keyId(keyId)
.limit(15)
.build();
ListGrantsPublisher paginator = getAsyncClient().listGrantsPaginator(grantsRequest);
return paginator.subscribe(response -> {
response.grants().forEach(grant -> {
logger.info("The grant Id is: " + grant.grantId());
});
})
.thenApply(v -> null)
.exceptionally(ex -> {
Throwable cause = ex.getCause();
if (cause instanceof KmsException) {thrownew RuntimeException("Failed to list grants: " + cause.getMessage(), cause);
} else{thrownew RuntimeException("An unexpected error occurred: " + cause.getMessage(), cause);
}
});
}
如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 ListGrants。
/***
* @param string $keyId
* @return Result
*/publicfunctionlistGrants(string$keyId)
{try{return$this->client->listGrants([
'KeyId' => $keyId,
]);
}catch(KmsException $caught){if($caught->getAwsErrorMessage() == "NotFoundException"){echo" The request was rejected because the specified entity or resource could not be found.\n";
}
throw$caught;
}
}
如需 API 詳細資訊,請參閱 AWS SDK for PHP API 參考中的 ListGrants。
classGrantManager:def__init__(self, kms_client):
self.kms_client = kms_client
@classmethoddeffrom_client(cls) -> "GrantManager":"""
Creates a GrantManager instance with a default KMS client.
:return: An instance of GrantManager initialized with the default KMS client.
"""
kms_client = boto3.client("kms")
return cls(kms_client)
deflist_grants(self, key_id):"""
Lists grants for a key.
:param key_id: The ARN or ID of the key to query.
:return: The grants for the key.
"""try:
paginator = self.kms_client.get_paginator("list_grants")
grants = []
page_iterator = paginator.paginate(KeyId=key_id)
for page in page_iterator:
grants.extend(page["Grants"])
print(f"Grants for key {key_id}:")
pprint(grants)
return grants
except ClientError as err:
logger.error(
"Couldn't list grants for key %s. Here's why: %s",
key_id,
err.response["Error"]["Message"],
)
raise
如需 API 詳細資訊,請參閱 SDK AWS for Python (Boto3) API 參考中的 ListGrants。
using System;
using System.Threading.Tasks;
using Amazon.KeyManagementService;
using Amazon.KeyManagementService.Model;
///<summary>/// List the AWS Key Management Service (AWS KMS) grants that are associated with/// a specific key.///</summary>publicclassListGrants{publicstaticasync Task Main(){// The identifier of the AWS KMS key to disable. You can use the// key Id or the Amazon Resource Name (ARN) of the AWS KMS key.var keyId = "1234abcd-12ab-34cd-56ef-1234567890ab";
var client = new AmazonKeyManagementServiceClient();
var request = new ListGrantsRequest
{
KeyId = keyId,
};
var response = new ListGrantsResponse();
do{
response = await client.ListGrantsAsync(request);
response.Grants.ForEach(grant =>
{
Console.WriteLine($"{grant.GrantId}");
});
request.Marker = response.NextMarker;
}
while (response.Truncated);
}
}
如需 API 詳細資訊,請參閱 AWS SDK for .NET API 參考中的 ListGrants。