.NET 用 Azure Storage BLOB クライアント ライブラリ - バージョン 12.19.0

サーバー バージョン: 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 サブスクリプションストレージ アカウント が必要です。

新しいストレージ アカウントを作成するには、Azure PortalAzure PowerShell、または Azure CLI を使用できます。 Azure CLI を使う例を次に示します。

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

クライアントを認証する

Azure Blobs Storage サービスを操作するには、BlobServiceClient クラスのインスタンスを作成する必要があります。 Azure Identity ライブラリを使用すると、対応する 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 には、3 種類のリソースがあります。

  • によって使用されるストレージ アカウントBlobServiceClient
  • によって使用されるストレージ アカウント内のコンテナーBlobContainerClient
  • によって使用されるコンテナー内の BLOBBlobClient

認証のオプション (接続文字列、共有キー、共有キー署名、Active Directory、匿名パブリック アクセスなど) の詳細については、サンプルを参照してください。

スレッド セーフ

すべてのクライアント インスタンス メソッドがスレッド セーフであり、相互に独立していることを保証します (ガイドライン)。 これにより、スレッド間であっても、クライアント インスタンスの再利用に関する推奨事項が常に安全になります。

その他の概念

クライアント オプション | 応答 | へのアクセス実行時間の長い操作 | エラーの | 処理診断 | あざける | クライアントの有効期間

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 サービス操作では、失敗時に RequestFailedException がスローされ、役に立ちますErrorCode。 これらのエラーの多くは回復可能です。

// 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. Hello World: BLOB のアップロード、ダウンロード、一覧表示 (または非同期)
  2. 認証: 接続文字列、パブリック アクセス、共有キー、共有アクセス署名、Azure Active Directory を使用して認証します。

共同作成

このライブラリのビルド、テスト、および投稿の詳細については、「 Storage CONTRIBUTING.md 」を参照してください。

このプロジェクトでは、共同作成と提案を歓迎しています。 ほとんどの共同作成では、共同作成者使用許諾契約書 (CLA) にご同意いただき、ご自身の共同作成内容を使用する権利を Microsoft に供与する権利をお持ちであり、かつ実際に供与することを宣言していただく必要があります。 詳細については、「 cla.microsoft.com」を参照してください。

このプロジェクトでは、Microsoft オープン ソースの倫理規定を採用しています。 詳しくは、「Code of Conduct FAQ (倫理規定についてよくある質問)」を参照するか、opencode@microsoft.com 宛てに質問またはコメントをお送りください。

インプレッション数