다음을 통해 공유


.NET용 Azure Storage Blob 클라이언트 라이브러리 - 버전 12.15.1

서버 버전: 2021-02-12, 2020-12-06, 2020-10-02, 2020-08-04, 2020-06-12, 2020-04-08, 2020-02-10, 2019-12-12, 2019-07-07 및 2019-02-02

Azure Blob Storage는 클라우드를 위한 Microsoft의 개체 스토리지 솔루션입니다. Blob Storage는 구조화되지 않은 대량의 데이터를 저장하는 데 최적화되어 있습니다. 비정형 데이터는 특정 데이터 모델이나 정의를 따르지 않는, 텍스트 또는 이진 데이터와 같은 데이터입니다.

소스 코드 | 패키지(NuGet) | API 참조 설명서 | REST API 설명서 | 제품 설명서

시작

패키지 설치

NuGet을 사용하여 .NET용 Azure Storage Blob 클라이언트 라이브러리를 설치합니다.

dotnet add package Azure.Storage.Blobs

필수 구성 요소

이 패키지를 사용하려면 Azure 구독스토리지 계정이 필요합니다.

새 Storage 계정을 만들려면 Azure Portal, Azure PowerShell 또는 Azure CLI를 사용할 수 있습니다. 다음은 Azure CLI 사용 예입니다.

az storage account create --name MyStorageAccount --resource-group MyResourceGroup --location westus --sku Standard_LRS

클라이언트 인증

Azure Blobs Storage 서비스와 상호 작용하려면 BlobServiceClient 클래스의 instance 만들어야 합니다. Azure ID 라이브러리를 사용하면 해당 Azure 서비스를 사용하여 Azure SDK 클라이언트를 인증하기 위한 Azure Active Directory 지원을 쉽게 추가할 수 있습니다.

// Create a BlobServiceClient that will authenticate through Active Directory
Uri accountUri = new Uri("https://MYSTORAGEACCOUNT.blob.core.windows.net/");
BlobServiceClient client = new BlobServiceClient(accountUri, new DefaultAzureCredential());

설명서 및 샘플에서 Azure Storage를 사용하여 Azure Active Directory 인증을 사용하도록 설정하는 방법에 대해 자세히 알아봅니.

주요 개념

Blob Storage는 다음을 위해 설계되었습니다.

  • 브라우저에 이미지 또는 문서 직접 제공
  • 분산 액세스용 파일 저장.
  • 비디오 및 오디오 스트리밍.
  • 로그 파일에 쓰기
  • 백업/복원, 재해 복구 및 보관용 데이터 저장
  • 온-프레미스 또는 Azure 호스팅 서비스에서 분석하기 위한 데이터 저장.

Blob Storage는 다음 세 가지 유형의 리소스를 제공합니다.

  • 를 통해 사용되는 스토리지 계정BlobServiceClient
  • 를 통해 사용되는 스토리지 계정의 컨테이너BlobContainerClient
  • 를 통해 사용되는 컨테이너의 BlobBlobClient

샘플에서 인증 옵션(연결 문자열, 공유 키, 공유 키 서명, Active Directory 및 익명 공용 액세스 포함) 대해 자세히 알아봅니다.

스레드로부터의 안전성

모든 클라이언트 instance 메서드는 스레드로부터 안전하고 서로 독립적임을 보장합니다(지침). 이렇게 하면 스레드 간에 클라이언트 인스턴스를 다시 사용하는 것이 항상 안전합니다.

추가 개념

클라이언트 옵션 | 응답 | 에 액세스 장기 실행 작업 | 오류 | 처리 진단 | 조롱 | 클라이언트 수명

예제

Blob 업로드

// Get a connection string to our Azure Storage account.  You can
// obtain your connection string from the Azure Portal (click
// Access Keys under Settings in the Portal Storage account blade)
// or using the Azure CLI with:
//
//     az storage account show-connection-string --name <account_name> --resource-group <resource_group>
//
// And you can provide the connection string to your application
// using an environment variable.

string connectionString = "<connection_string>";
string containerName = "sample-container";
string blobName = "sample-blob";
string filePath = "sample-file";

// Get a reference to a container named "sample-container" and then create it
BlobContainerClient container = new BlobContainerClient(connectionString, containerName);
container.Create();

// Get a reference to a blob named "sample-file" in a container named "sample-container"
BlobClient blob = container.GetBlobClient(blobName);

// Upload local file
blob.Upload(filePath);

Blob 다운로드

// Get a temporary path on disk where we can download the file
string downloadPath = "hello.jpg";

// Download the public blob at https://aka.ms/bloburl
new BlobClient(new Uri("https://aka.ms/bloburl")).DownloadTo(downloadPath);

Blob 열거

// Get a connection string to our Azure Storage account.
string connectionString = "<connection_string>";
string containerName = "sample-container";
string filePath = "hello.jpg";

// Get a reference to a container named "sample-container" and then create it
BlobContainerClient container = new BlobContainerClient(connectionString, containerName);
container.Create();

// Upload a few blobs so we have something to list
container.UploadBlob("first", File.OpenRead(filePath));
container.UploadBlob("second", File.OpenRead(filePath));
container.UploadBlob("third", File.OpenRead(filePath));

// Print out all the blob names
foreach (BlobItem blob in container.GetBlobs())
{
    Console.WriteLine(blob.Name);
}

비동기 API

동기 API와 비동기 API를 모두 완벽하게 지원합니다.

// Get a temporary path on disk where we can download the file
string downloadPath = "hello.jpg";

// Download the public blob at https://aka.ms/bloburl
await new BlobClient(new Uri("https://aka.ms/bloburl")).DownloadToAsync(downloadPath);

문제 해결

모든 Blob 서비스 작업은 유용한 ErrorCodes를 사용하여 실패 시 RequestFailedException을 throw합니다. 이러한 오류 중 대부분은 복구할 수 있습니다.

// Get a connection string to our Azure Storage account.
string connectionString = "<connection_string>";
string containerName = "sample-container";

// Try to delete a container named "sample-container" and avoid any potential race conditions
// that might arise by checking if the container is already deleted or is in the process
// of being deleted.
BlobContainerClient container = new BlobContainerClient(connectionString, containerName);

try
{
    container.Delete();
}
catch (RequestFailedException ex)
    when (ex.ErrorCode == BlobErrorCode.ContainerBeingDeleted ||
          ex.ErrorCode == BlobErrorCode.ContainerNotFound)
{
    // Ignore any errors if the container being deleted or if it has already been deleted
}

다음 단계

Blob 샘플 시작:

  1. 헬로 월드: Blob 업로드, 다운로드 및 나열(또는 비동기)
  2. 인증: 연결 문자열, 공용 액세스, 공유 키, 공유 액세스 서명 및 Azure Active Directory를 사용하여 인증합니다.

참여

이 라이브러리의 빌드, 테스트 및 기여에 대한 자세한 내용은 Storage CONTRIBUTING.md 참조하세요.

이 프로젝트에 대한 기여와 제안을 환영합니다. 대부분의 경우 기여하려면 권한을 부여하며 실제로 기여를 사용할 권한을 당사에 부여한다고 선언하는 CLA(기여자 라이선스 계약)에 동의해야 합니다. 자세한 내용은 cla.microsoft.com.

이 프로젝트에는 Microsoft Open Source Code of Conduct(Microsoft 오픈 소스 준수 사항)가 적용됩니다. 자세한 내용은 Code of Conduct FAQ(규정 FAQ)를 참조하세요. 또는 추가 질문이나 의견은 opencode@microsoft.com으로 문의하세요.

Impressions