Azure中的BlobRequest选项,存储,Blobs

ubbxdtey  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(126)

我有一些用C#编写的blob访问代码,我需要将它们从使用Microsoft.WindowsAzure. Storage.Blob迁移到使用Azure. Storage.Blob
在旧代码中,读取blob时的故障转移是使用以下代码行处理的:

using Microsoft.WindowsAzure.Storage;
Blob.BlobRequestOptions myBlobOptions = new Blob.BlobRequestOptions();
myBlobOptions.LocationMode = RetryPolicies.LocationMode.PrimaryThenSecondary;
myBlobClient.DefaultRequestOptions = myBlobOptions;
        
Blob.CloudBlobContainer myContainer = myBlobClient.GetContainerReference("container");
Blob.CloudBlockBlob blockBlob = myContainer.GetBlockBlobReference("blobfile");

使用Azure.存储. Blob时该怎么做?
新代码:

BlobServiceClient BlobServiceClient = new BlobServiceClient("connectionString");
BlobContainerClient container = BlobServiceClient.GetBlobContainerClient("container");
BlobClient blob = container.GetBlobClient("blobfile");
dly7yett

dly7yett1#

我在自己的环境中尝试过,并通过一段代码成功读取了Azure Blob存储中的文件。
代码:

using Azure.Storage.Blobs;

namespace blobstr
{
    class program {
        public static async Task Main()
        {
            BlobServiceClient blobServiceClient = new BlobServiceClient("< Connection string >");
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("test");
            BlobClient blobClient = containerClient.GetBlobClient("sample1.csv");
            if (await blobClient.ExistsAsync())
            {
                var response = await blobClient.DownloadAsync();
                using (var streamReader = new StreamReader(response.Value.Content))
                {
                    while (!streamReader.EndOfStream)
                    {
                        var line = await streamReader.ReadLineAsync();
                        Console.WriteLine(line);
                    }
                }
            }

        }
    }
}

门户网站:

  • 在Blob存储中,我有一个csv文件,查看下面的快照。*

控制台:

  • 在控制台中执行代码后,我可以成功从Azure Blob存储中读取文件。*

相关问题