使用 JavaScript 列出 Azure Key Vault 中的金鑰和版本
列出所有索引鍵
使用可反覆運算的 listPropertiesOfKeys,列出所有金鑰的目前版本。
import { KeyClient, CreateKeyOptions, KeyVaultKey } from '@azure/keyvault-keys';
import { DefaultAzureCredential } from '@azure/identity';
const credential = new DefaultAzureCredential();
const client = new KeyClient(
`https://${process.env.AZURE_KEYVAULT_NAME}.vault.azure.net`,
credential
);
// Get latest version of (not soft-deleted) keys
for await (const keyProperties of client.listPropertiesOfKeys()) {
console.log(keyProperties.version);
}
傳回的 KeyProperties 物件包含金鑰版本。
依頁面列出所有索引鍵
若要列出 Azure Key Vault 中的所有金鑰,請使用 listPropertiesOfKeys 方法,藉由設定 PageSettings 物件,一次取得一頁的祕密屬性。
import { KeyClient } from '@azure/keyvault-keys';
import { DefaultAzureCredential } from '@azure/identity';
const credential = new DefaultAzureCredential();
const client = new KeyClient(
`https://${process.env.AZURE_KEYVAULT_NAME}.vault.azure.net`,
credential
);
let page = 1;
const maxPageSize = 5;
// Get latest version of not-deleted keys
for await (const keyProperties of client.listPropertiesOfKeys().byPage({maxPageSize})) {
console.log(`Page ${page++} ---------------------`)
for (const props of keyProperties) {
console.log(`${props.name}`);
}
}
傳回的 KeyProperties 物件包含金鑰版本。
列出所有版本的金鑰
若要列出 Azure Key Vault 中的所有金鑰版本,請使用 listPropertiesOfKeyVersions 方法。
import { KeyClient } from '@azure/keyvault-keys';
import { DefaultAzureCredential } from '@azure/identity';
const credential = new DefaultAzureCredential();
const client = new KeyClient(
`https://${process.env.AZURE_KEYVAULT_NAME}.vault.azure.net`,
credential
);
// Get all versions of key
for await (const versionProperties of client.listPropertiesOfKeyVersions(
keyName
)) {
console.log(`\tversion: ${versionProperties.version} created on ${versionProperties.createdOn}`);
}
傳回的 KeyProperties 物件包含金鑰版本。
請參閱依頁面列出所有金鑰範例,以了解如何逐頁查看結果。
列出已刪除的金鑰
若要列出 Azure Key Vault 中的所有已刪除金鑰,請使用 listDeletedKeys 方法。
import { KeyClient } from '@azure/keyvault-keys';
import { DefaultAzureCredential } from '@azure/identity';
const credential = new DefaultAzureCredential();
const client = new KeyClient(
`https://${process.env.AZURE_KEYVAULT_NAME}.vault.azure.net`,
credential
);
for await (const deletedKey of client.listDeletedKeys()) {
console.log(
`Deleted: ${deletedKey.name} deleted on ${deletedKey.properties.deletedOn}, to be purged on ${deletedKey.properties.scheduledPurgeDate}`
);
}
deletedKey 對像是 DeletedKey 物件,其中包含具有其他屬性的 KeyProperties 物件,例如:
請參閱依頁面列出所有金鑰範例,以了解如何逐頁查看結果。