Share via


Blob 인덱스 태그를 사용하여 Python으로 데이터 관리 및 찾기

이 문서에서는 Blob 인덱스 태그를 사용하여 Python용 Azure Storage 클라이언트 라이브러리를 사용하여 데이터를 관리하고 찾는 방법을 보여 줍니다.

비동기 API를 사용하여 Blob 인덱스 태그를 설정하는 방법에 대한 자세한 내용은 비동기적으로 Blob 인덱스 태그 설정을 참조하세요.

필수 조건

  • 이 문서에서는 Python용 Azure Blob Storage 클라이언트 라이브러리로 작업하도록 프로젝트가 이미 설정되어 있다고 가정합니다. 패키지 설치, import 문 추가 및 권한 있는 클라이언트 개체 만들기를 포함하여 프로젝트를 설정하는 방법에 대한 자세한 내용은 Azure Blob Storage 및 Python 시작을 참조하세요.
  • 권한 부여 메커니즘에는 Blob 인덱스 태그를 사용하는 권한이 있어야 합니다. 자세한 내용은 다음 REST API 작업에 대한 권한 부여 지침을 참조하세요.

Blob 인덱스 태그 정보

Blob 인덱스 태그는 키-값 태그 특성을 사용하여 스토리지 계정의 데이터를 분류합니다. 이러한 태그는 데이터를 쉽게 찾을 수 있도록 검색 가능한 다차원 인덱스로 자동으로 인덱싱되고 표시됩니다. 이 문서에서는 Blob 인덱스 태그를 사용하여 데이터를 설정하고 가져오고 검색하는 방법을 보여 줍니다.

Blob 인덱스 태그는 계층 구조 네임스페이스를 사용하도록 설정한 스토리지 계정에 대해 지원되지 않습니다. 알려진 문제 및 제한과 Blob 인덱스 태그 기능에 대해 자세히 알아보려면 Blob 인덱스 태그를 사용하여 Azure Blob 데이터 관리 및 찾기를 참조하세요.

태그 설정

코드에 다음 메커니즘 중 하나를 통해 Blob 데이터에 대한 액세스 권한이 있는 경우 인덱스 태그를 설정할 수 있습니다.

자세한 내용은 Blob 인덱스 태그 설정을 참조하세요.

다음 메서드를 사용하여 태그를 설정할 수 있습니다.

이 메서드에 지정된 태그는 기존 태그를 대체합니다. 이전 값을 보존해야 하는 경우 다운로드하여 이 메서드 호출에 포함해야 합니다. 다음 예제에서는 태그를 설정하는 방법을 보여 줍니다.

def set_blob_tags(self, blob_service_client: BlobServiceClient, container_name):
    blob_client = blob_service_client.get_blob_client(container=container_name, blob="sample-blob.txt")

    # Get any existing tags for the blob if they need to be preserved
    tags = blob_client.get_blob_tags()

    # Add or modify tags
    updated_tags = {'Sealed': 'false', 'Content': 'image', 'Date': '2022-01-01'}
    tags.update(updated_tags)

    blob_client.set_blob_tags(tags)

dict 개체를 set_blob_tags 메서드에 전달하여 모든 태그를 삭제할 수 있습니다.

def clear_blob_tags(self, blob_service_client: BlobServiceClient, container_name):
    blob_client = blob_service_client.get_blob_client(container=container_name, blob="sample-blob.txt")

    # Pass in empty dict object to clear tags
    tags = dict()
    blob_client.set_blob_tags(tags)

태그 가져오기

코드에 다음 메커니즘 중 하나를 통해 Blob 데이터에 대한 액세스 권한이 있는 경우 인덱스 태그를 가져올 수 있습니다.

자세한 내용은 Blob 인덱스 태그 가져오기 및 나열을 참조하세요.

다음 메서드를 사용하여 태그를 가져올 수 있습니다.

다음 예제에서는 Blob의 태그를 검색하고 반복하는 방법을 보여 줍니다.

def get_blob_tags(self, blob_service_client: BlobServiceClient, container_name):
    blob_client = blob_service_client.get_blob_client(container=container_name, blob="sample-blob.txt")

    tags = blob_client.get_blob_tags()
    print("Blob tags: ")
    for k, v in tags.items():
        print(k, v)

Blob 인덱스 태그를 사용하여 데이터 필터링 및 찾기

코드에 다음 메커니즘 중 하나를 통해 Blob 데이터에 대한 액세스 권한이 있는 경우 인덱스 태그를 사용하여 데이터를 찾고 필터링할 수 있습니다.

자세한 내용은 Blob 인덱스 태그를 사용하여 데이터 찾기를 참조하세요.

참고 항목

인덱스 태그를 사용하여 이전 버전을 검색할 수 없습니다. 이전 버전의 태그는 Blob 인덱스 엔진에 전달되지 않습니다. 자세한 내용은 조건 및 알려진 문제를 참조하세요.

다음 메서드를 사용하여 데이터를 찾을 수 있습니다.

다음 예제에서는 이미지로 태그가 지정된 모든 Blob을 찾아 나열합니다.

def find_blobs_by_tags(self, blob_service_client: BlobServiceClient, container_name):
    container_client = blob_service_client.get_container_client(container=container_name)

    query = "\"Content\"='image'"
    blob_list = container_client.find_blobs_by_tags(filter_expression=query)
    
    print("Blobs tagged as images")
    for blob in blob_list:
        print(blob.name)

비동기적으로 Blob 인덱스 태그 설정

Python용 Azure Blob Storage 클라이언트 라이브러리는 Blob 인덱스 태그를 비동기적으로 사용할 수 있습니다. 프로젝트 설정 요구 사항에 대해 자세히 알아보려면 비동기 프로그래밍을 참조하세요.

비동기 API를 사용하여 Blob 인덱스 태그를 설정하려면 다음 단계를 수행합니다.

  1. 다음 import 문을 추가합니다.

    import asyncio
    
    from azure.identity.aio import DefaultAzureCredential
    from azure.storage.blob.aio import BlobServiceClient
    
  2. asyncio.run을 사용하여 프로그램을 실행하는 코드를 추가합니다. 이 함수는 전달된 코루틴(예에서는 main())을 실행하고 asyncio 이벤트 루프를 관리합니다. 코루틴은 async/await 구문으로 선언됩니다. 이 예제에서 main() 코루틴은 먼저 async with를 사용하여 최상위 수준 BlobServiceClient를 만든 다음 Blob 인덱스 태그를 설정하는 메서드를 호출합니다. 이 클라이언트에서 만들어진 다른 클라이언트는 동일한 연결 풀을 공유하기 때문에 최상위 클라이언트에서만 async with를 사용해야 합니다.

    async def main():
        sample = BlobSamples()
    
        # TODO: Replace <storage-account-name> with your actual storage account name
        account_url = "https://<storage-account-name>.blob.core.windows.net"
        credential = DefaultAzureCredential()
    
        async with BlobServiceClient(account_url, credential=credential) as blob_service_client:
            await sample.set_blob_tags(blob_service_client, "sample-container")
    
    if __name__ == '__main__':
        asyncio.run(main())
    
  3. Blob 인덱스 태그를 설정하는 코드를 추가합니다. 메서드가 async 키워드로 선언되고 get_blob_tagsset_blob_tags 메서드를 호출할 때 await 키워드가 사용된다는 점을 제외하면 코드는 동기 예와 동일합니다.

    async def set_blob_tags(self, blob_service_client: BlobServiceClient, container_name):
        blob_client = blob_service_client.get_blob_client(container=container_name, blob="sample-blob.txt")
    
        # Get any existing tags for the blob if they need to be preserved
        tags = await blob_client.get_blob_tags()
    
        # Add or modify tags
        updated_tags = {'Sealed': 'false', 'Content': 'image', 'Date': '2022-01-01'}
        tags.update(updated_tags)
    
        await blob_client.set_blob_tags(tags)
    

이 기본 설정을 사용하면 async/await 구문을 통해 이 문서의 다른 예를 코루틴으로 구현할 수 있습니다.

리소스

Python용 Azure Blob Storage 클라이언트 라이브러리를 사용하여 인덱스 태그를 통해 데이터를 관리하고 찾는 방법에 대한 자세한 내용은 다음 리소스를 참조하세요.

REST API 작업

Python용 Azure SDK에는 Azure REST API를 기반으로 빌드되는 라이브러리가 포함되어 있어 친숙한 Python 패러다임을 통해 REST API 작업과 상호 작용할 수 있습니다. Blob 인덱스 태그를 관리하고 사용하기 위한 클라이언트 라이브러리 메서드는 다음 REST API 작업을 사용합니다.

코드 샘플

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

참고 항목