.NET을 사용하여 Blob Storage에 대한 요청에 고객이 제공한 키를 지정

Azure Blob Storage에 대한 요청을 수행하는 클라이언트에는 개별 요청에 대한 AES-256 암호화 키를 제공하는 옵션이 있습니다. 요청에 암호화 키를 포함하면 Blob Storage 작업에 대한 암호화 설정의 세부적인 제어 기능이 제공됩니다. 고객이 제공한 키는 Azure Key Vault 또는 다른 키 스토리지에 저장할 수 있습니다.

이 문서에서는 .NET을 사용하여 요청 시 고객이 제공한 키를 지정하는 방법을 보여 줍니다.

클라이언트 라이브러리 패키지 설치

참고 항목

여기에 표시된 예제는 Azure Storage 클라이언트 라이브러리 버전 12를 사용합니다. 버전 12 클라이언트 라이브러리는 Azure SDK의 일부입니다. Azure SDK에 대한 자세한 내용은 GitHub의 Azure SDK 리포지토리를 참조하세요.

Blob 스토리지 패키지를 설치하려면 NuGet 패키지 관리자 콘솔에서 다음 명령을 실행합니다.

Install-Package Azure.Storage.Blobs

여기에 표시된 예제에서는 .NET용 Azure ID 클라이언트 라이브러리의 최신 버전을 사용하여 Microsoft Entra 자격 증명으로 인증합니다. 패키지를 설치하려면 NuGet 패키지 관리자 콘솔에서 다음 명령을 실행합니다.

Install-Package Azure.Identity

Azure ID 클라이언트 라이브러리로 인증하는 방법에 대한 자세한 내용은 .NET용 Azure ID 클라이언트 라이브러리를 참조하세요.

고객이 제공한 키를 사용하여 Blob에 쓰기

다음 예에서는 Blob Storage용 v12 클라이언트 라이브러리를 사용하여 Blob을 업로드할 때 AES-256 키를 제공합니다. 이 예에서는 DefaultAzureCredential 개체를 사용하여 Microsoft Entra에서 쓰기 요청에 권한을 부여하지만 공유 키 자격 증명으로 요청에 권한을 부여할 수도 있습니다. DefaultAzureCredential 클래스를 사용하여 Azure Storage에 액세스할 수 있는 관리 ID를 인증하는 방법에 대한 자세한 내용은 .NET용 Azure ID 클라이언트 라이브러리를 참조하세요.

async static Task UploadBlobWithClientKey(Uri blobUri,
                                          Stream data,
                                          byte[] key,
                                          string keySha256)
{
    // Create a new customer-provided key.
    // Key must be AES-256.
    var cpk = new CustomerProvidedKey(key);

    // Check the key's encryption hash.
    if (cpk.EncryptionKeyHash != keySha256)
    {
        throw new InvalidOperationException("The encryption key is corrupted.");
    }

    // Specify the customer-provided key on the options for the client.
    BlobClientOptions options = new BlobClientOptions()
    {
        CustomerProvidedKey = cpk
    };

    // Create the client object with options specified.
    BlobClient blobClient = new BlobClient(
        blobUri,
        new DefaultAzureCredential(),
        options);

    // If the container may not exist yet,
    // create a client object for the container.
    // The container client retains the credential and client options.
    BlobContainerClient containerClient =
        blobClient.GetParentBlobContainerClient();

    try
    {
        // Create the container if it does not exist.
        await containerClient.CreateIfNotExistsAsync();

        // Upload the data using the customer-provided key.
        await blobClient.UploadAsync(data);
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
        throw;
    }
}

다음 단계