使用 TypeScript 刪除和還原 Blob 容器
本文說明如何使用適用於 JavaScript 的 Azure 儲存體用戶端程式庫來刪除容器。 如果您已啟用容器虛刪除,則可以還原已刪除的容器。
必要條件
- 本文中的範例假設您已有設定好要搭配適用於 JavaScript 的 Azure Blob 儲存體用戶端程式庫使用的專案。 若要了解如何設定專案,包括套件安裝、匯入模組,以及建立授權的用戶端物件來處理資料資源,請參閱開始使用 Azure Blob 儲存體和 TypeScript。
- 授權機制必須具有刪除 Blob 容器的權限,或還原虛刪除的容器。 若要深入了解,請參閱下列 REST API 作業的授權指導:
刪除容器
若要刪除 TypeScript 中的容器,請建立 BlobServiceClient 或 ContainerClient,然後使用下列其中一種方法:
- BlobServiceClient.deleteContainer
- ContainerClient.delete
- ContainerClient.deleteIfExists
刪除容器之後,您就無法建立具有相同名稱的容器至少 30 秒。 嘗試建立具有相同名稱的容器將會失敗,並出現 HTTP 錯誤碼 409 (衝突)。 對容器或其包含的 blob 進行的任何其他作業都會失敗,並出現 HTTP 錯誤碼 404 (找不到)。
使用 BlobServiceClient 刪除容器
下列範例會刪除指定的容器。 使用 BlobServiceClient 刪除容器:
// delete container immediately on blobServiceClient
async function deleteContainerImmediately(
blobServiceClient: BlobServiceClient,
containerName: string
): Promise<ContainerDeleteResponse> {
return await blobServiceClient.deleteContainer(containerName);
}
使用 ContainerClient 刪除容器
下列範例說明如何使用 ContainerClient 刪除名稱以指定前置詞開頭的所有容器。
async function deleteContainersWithPrefix(
blobServiceClient: BlobServiceClient,
prefix: string
): Promise<void> {
const containerOptions: ServiceListContainersOptions = {
// only delete containers not deleted
includeDeleted: false,
includeMetadata: false,
includeSystem: true,
prefix
};
for await (const containerItem of blobServiceClient.listContainers(
containerOptions
)) {
try {
const containerClient: ContainerClient =
blobServiceClient.getContainerClient(containerItem.name);
const containerDeleteMethodOptions: ContainerDeleteMethodOptions = {};
await containerClient.delete(containerDeleteMethodOptions);
console.log(`deleted ${containerItem.name} container - success`);
} catch (err: unknown) {
if (err instanceof Error) {
console.log(
`deleted ${containerItem.name} container - failed - ${err.message}`
);
}
}
}
}
還原已刪除的容器
針對儲存體帳戶啟用容器虛刪除時,可以在您指定的保留期間內對刪除的容器及其內容進行復原。 您可以使用 BlobServiceClient 物件還原虛刪除的容器:
- BlobServiceClient.undeleteContainer
下列範例會尋找已刪除的容器、取得該已刪除容器的版本識別碼,然後將該識別碼傳遞至 undeleteContainer 方法來還原容器。
// Undelete specific container - last version
async function undeleteContainer(
blobServiceClient: BlobServiceClient,
containerName: string
): Promise<void> {
// version to undelete
let containerVersion: string | undefined;
const containerOptions: ServiceListContainersOptions = {
includeDeleted: true,
prefix: containerName
};
// container listing returns version (timestamp) in the ContainerItem
for await (const containerItem of blobServiceClient.listContainers(
containerOptions
)) {
// if there are multiple deleted versions of the same container,
// the versions are in asc time order
// the last version is the most recent
if (containerItem.name === containerName) {
containerVersion = containerItem.version as string;
}
}
if (containerVersion !== undefined) {
const serviceUndeleteContainerOptions: ServiceUndeleteContainerOptions = {};
const {
containerClient,
containerUndeleteResponse
}: {
containerClient: ContainerClient;
containerUndeleteResponse: ContainerUndeleteResponse;
} = await blobServiceClient.undeleteContainer(
containerName,
containerVersion,
// optional/new container name - if unused, original container name is used
//newContainerName
serviceUndeleteContainerOptions
);
// Check delete
if (containerUndeleteResponse.errorCode)
throw Error(containerUndeleteResponse.errorCode);
// undelete was successful
console.log(`${containerName} is undeleted`);
// do something with containerClient
// ...
// containerClient.listBlobsFlat({ includeMetadata: true,
// includeSnapshots: false,
// includeTags: true,
// includeVersions: false,
// prefix: ''});
}
}
資源
若要深入了解如何使用適用於 JavaScript 的 Azure Blob 儲存體用戶端程式庫刪除容器,請參閱下列資源。
REST API 操作
適用於 JavaScript 的 Azure SDK 包含建置在 Azure REST API 之上的程式庫,可讓您透過熟悉的 JavaScript 範例與 REST API 作業進行互動。 用來刪除或還原容器的用戶端程式庫方法會使用下列 REST API 作業:
程式碼範例
- 檢視本文中的程式碼範例 (GitHub) (英文)
用戶端程式庫資源
另請參閱
意見反映
https://aka.ms/ContentUserFeedback。
即將推出:我們會在 2024 年淘汰 GitHub 問題,並以全新的意見反應系統取代並作為內容意見反應的渠道。 如需更多資訊,請參閱:提交及檢視以下的意見反映: