使用 JavaScript 刪除和還原 Blob 容器
本文說明如何使用適用於 JavaScript 的 Azure 儲存體用戶端程式庫來刪除容器。 如果您已啟用容器虛刪除,則可以還原已刪除的容器。
必要條件
- 本文中的範例假設您已設定專案,以搭配使用適用於 JavaScript 的 Azure Blob 儲存體用戶端程式庫。 若要了解如何設定專案,包括套件安裝、匯入模組,以及建立授權的用戶端物件來處理資料資源,請參閱開始使用 Azure Blob 儲存體和 JavaScript。
- 授權機制必須具有刪除 Blob 容器的權限,或還原虛刪除容器的權限。 若要深入了解,請參閱下列 REST API 作業的授權指導:
刪除容器
若要刪除 JavaScript 中的容器,請建立 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, containerName) {
const response = await blobServiceClient.deleteContainer(containerName);
if (!response.errorCode) {
console.log(`deleted ${containerItem.name} container`);
}
}
使用 ContainerClient 刪除容器
下列範例說明如何使用 ContainerClient 刪除名稱以指定前置詞開頭的所有容器。
async function deleteContainersWithPrefix(blobServiceClient, blobNamePrefix){
const containerOptions = {
includeDeleted: false,
includeMetadata: false,
includeSystem: true,
prefix: blobNamePrefix
}
for await (const containerItem of blobServiceClient.listContainers(containerOptions)) {
const containerClient = blobServiceClient.getContainerClient(containerItem.name);
const response = await containerClient.delete();
if(!response.errorCode){
console.log(`deleted ${containerItem.name} container`);
}
}
}
還原已刪除的容器
針對儲存體帳戶啟用容器虛刪除時,可以在您指定的保留期間內對刪除的容器及其內容進行復原。 您可以使用 BlobServiceClient 物件還原虛刪除的容器:
- BlobServiceClient.undeleteContainer
下列範例會尋找已刪除的容器、取得該已刪除容器的版本識別碼,然後將該識別碼傳遞至 undeleteContainer 方法來還原容器。
// Undelete specific container - last version
async function undeleteContainer(blobServiceClient, containerName) {
// version to undelete
let containerVersion;
const containerOptions = {
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;
}
}
const containerClient = await blobServiceClient.undeleteContainer(
containerName,
containerVersion,
// optional/new container name - if unused, original container name is used
//newContainerName
);
// undelete was successful
console.log(`${containerName} is undeleted`);
// do something with containerClient
// ...
}
資源
若要深入了解如何使用適用於 JavaScript 的 Azure Blob 儲存體用戶端程式庫刪除容器,請參閱下列資源。
REST API 操作
適用於 JavaScript 的 Azure SDK 包含建置在 Azure REST API 之上的程式庫,可讓您透過熟悉的 JavaScript 範例與 REST API 作業進行互動。 用來刪除或還原容器的用戶端程式庫方法會使用下列 REST API 作業:
程式碼範例
- 檢視本文中的程式碼範例 (GitHub) (英文)
用戶端程式庫資源
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應