使用 .NET 匿名访问公共容器和 BlobAccess public containers and blobs anonymously with .NET

Azure 存储支持对容器和 blob 进行可选公共读取访问。Azure Storage supports optional public read access for containers and blobs. 客户端可以使用 Azure 存储客户端库以及支持对 Azure 存储进行数据访问的其他工具和实用工具来匿名访问公共容器和 Blob。Clients can access public containers and blobs anonymously by using the Azure Storage client libraries, as well as by using other tools and utilities that support data access to Azure Storage.

本文介绍如何从 .NET 访问公共容器或 blob。This article shows how to access a public container or blob from .NET. 若要了解如何在容器上配置匿名读取访问,请参阅为容器和 Blob 配置匿名公共读取访问For information about configuring anonymous read access on a container, see Configure anonymous public read access for containers and blobs. 若要了解如何阻止对存储帐户进行所有匿名访问,请参阅阻止对容器和 blob 的匿名公共读取访问For information about preventing all anonymous access to a storage account, see Prevent anonymous public read access to containers and blobs.

如果某个客户端需要以匿名方式访问容器和 Blob,该客户端则可以使用不需要凭据的构造函数。A client that accesses containers and blobs anonymously can use constructors that do not require credentials. 以下示例演示如何通过多种不同的方法以匿名方式引用容器和 Blob。The following examples show a few different ways to reference containers and blobs anonymously.

创建匿名客户端对象Create an anonymous client object

通过提供帐户的 Blob 存储终结点,可以创建一个可匿名访问的新服务客户端对象。You can create a new service client object for anonymous access by providing the Blob storage endpoint for the account. 但是,也必须要知道该帐户中允许进行匿名访问的容器的名称。However, you must also know the name of a container in that account that's available for anonymous access.

public static void CreateAnonymousBlobClient()
{
    // Create the client object using the Blob storage endpoint for your account.
    BlobServiceClient blobServiceClient = new BlobServiceClient
        (new Uri(@"https://storagesamples.blob.core.chinacloudapi.cn/"));

    // Get a reference to a container that's available for anonymous access.
    BlobContainerClient container = blobServiceClient.GetBlobContainerClient("sample-container");

    // Read the container's properties. 
    // Note this is only possible when the container supports full public read access.          
    Console.WriteLine(container.GetProperties().Value.LastModified);
    Console.WriteLine(container.GetProperties().Value.ETag);
}

以匿名方式引用容器Reference a container anonymously

如果拥有可以通过匿名方式使用的容器的 URL,则可使用该 URL 来直接引用容器。If you have the URL to a container that is anonymously available, you can use it to reference the container directly.

public static void ListBlobsAnonymously()
{
    // Get a reference to a container that's available for anonymous access.
    BlobContainerClient container = new BlobContainerClient
        (new Uri(@"https://storagesamples.blob.core.chinacloudapi.cn/sample-container"));

    // List blobs in the container.
    // Note this is only possible when the container supports full public read access.
       foreach (BlobItem blobItem in container.GetBlobs())
        {
            Console.WriteLine(container.GetBlockBlobClient(blobItem.Name).Uri);
        }
}

以匿名方式引用 BlobReference a blob anonymously

如果拥有允许进行匿名访问的 Blob 的 URL,则可使用该 URL 来直接引用 Blob:If you have the URL to a blob that is available for anonymous access, you can reference the blob directly using that URL:

public static void DownloadBlobAnonymously()
{
    BlockBlobClient blob = new BlockBlobClient
        (new Uri(@"https://storagesamples.blob.core.chinacloudapi.cn/sample-container/logfile.txt"));
    blob.DownloadTo(@"C:\Temp\logfile.txt");
}

后续步骤Next steps