適用于 .NET 的 Azure 儲存體 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 儲存體是 Microsoft 針對雲端推出的物件儲存體解決方案。 Blob 儲存體已針對儲存大量非結構化資料進行最佳化。 非結構化資料是指不遵守特定資料模型或定義的資料,例如文字或二進位資料。

| 原始程式碼套件 (NuGet) | API 參考檔 | REST API 檔 | 產品檔

開始使用

安裝套件

使用 NuGet安裝適用于 .NET 的 Azure 儲存體 Blob 用戶端程式庫:

dotnet add package Azure.Storage.Blobs

必要條件

您需要 Azure 訂 用帳戶和 儲存體帳戶 才能使用此套件。

若要建立新的儲存體帳戶,您可以使用Azure 入口網站Azure PowerShellAzure CLI。 以下是使用 Azure CLI 的範例:

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

驗證用戶端

若要與 Azure Blobs 儲存體服務互動,您必須建立 BlobServiceClient 類別的實例。 Azure 身分識別程式庫可讓您輕鬆地新增 Azure Active Directory 支援,以使用其對應的 Azure 服務驗證 Azure SDK 用戶端。

// 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 Active Directory 以向 Azure 儲存體進行驗證。

重要概念

Blob 儲存體設計用來:

  • 直接提供映像或文件給瀏覽器。
  • 儲存檔案供分散式存取。
  • 串流影片和音訊。
  • 寫入記錄檔。
  • 儲存備份和還原、災害復原和封存資料。
  • 儲存資料供內部部署或 Azure 裝載服務進行分析。

Blob 儲存體提供三種類型資源:

  • 透過 使用的儲存體帳戶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。

// 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 進行驗證。

參與

如需建置、測試和參與此程式庫的詳細資訊,請參閱 儲存體 CONTRIBUTING.md

此專案歡迎參與和提供建議。 大部分的參與都要求您同意「參與者授權合約 (CLA)」,宣告您有權且確實授與我們使用投稿的權利。 如需詳細資訊,請造訪 cla.microsoft.com

此專案採用 Microsoft Open Source Code of Conduct (Microsoft 開放原始碼管理辦法)。 如需詳細資訊,請參閱管理辦法常見問題集,如有任何其他問題或意見請連絡 opencode@microsoft.com

曝光數