Share via


TypeScript를 사용하여 Blob 컨테이너 삭제 및 복원

이 문서에서는 JavaScript용 Azure Storage 클라이언트 라이브러리를 사용하여 컨테이너를 삭제하는 방법을 보여 줍니다. 컨테이너 일시 삭제를 사용하도록 설정한 경우 삭제된 컨테이너를 복원할 수 있습니다.

필수 조건

  • 이 문서의 예제에서는 JavaScript용 Azure Blob Storage 클라이언트 라이브러리로 작업하도록 프로젝트가 이미 설정되어 있다고 가정합니다. 패키지 설치, 모듈 가져오기, 데이터 리소스 작업을 위한 권한 있는 클라이언트 개체 만들기 등 프로젝트를 설정하는 방법에 대한 자세한 내용은 Azure Blob Storage 및 TypeScript 시작을 참조하세요.
  • 권한 부여 메커니즘에는 Blob 컨테이너를 삭제하거나 일시 삭제된 컨테이너를 복원할 수 있는 권한이 있어야 합니다. 자세한 내용은 다음 REST API 작업에 대한 권한 부여 참고 자료를 참조하세요.

컨테이너 삭제

TypeScript에서 컨테이너를 삭제하려면 BlobServiceClient 또는 ContainerClient를 만든 다음, 다음 방법 중 하나를 사용합니다.

컨테이너를 삭제한 후 최소 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 개체를 사용하여 일시 삭제된 컨테이너를 복원할 수 있습니다.

다음 예에서는 삭제된 컨테이너를 찾고 삭제된 컨테이너의 버전 ID를 가져온 다음 해당 ID를 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 Storage 클라이언트 라이브러리를 사용하여 컨테이너를 삭제하는 방법에 대해 자세히 알아보려면 다음 리소스를 참조하세요.

REST API 작업

JavaScript용 Azure SDK에는 Azure REST API를 기반으로 빌드되는 라이브러리가 포함되어 있으므로 익숙한 JavaScript 패러다임을 통해 REST API 작업과 상호 작용할 수 있습니다. 컨테이너를 삭제하거나 복원하기 위한 클라이언트 라이브러리 메서드는 다음 REST API 작업을 사용합니다.

코드 샘플

클라이언트 라이브러리 리소스

참고 항목