可以从Azure块Blob中获取特定的块吗?

huangapple go评论94阅读模式
英文:

Can I get a specific Block from an Azure Block Blob?

问题

我想通过 blockId 从 Azure 块 blob 中获取特定块,这是否可能?
类似这样的代码:

  1. var blockBlob = new BlockBlobClient(connectionString, containerName, blobName);
  2. var blocklist = await GetBlobBlockList(blobName, cancellationToken);
  3. var firstBlock = blocklist.First();
  4. var memStream = new MemoryStream();
  5. await blockBlob.DownloadStreamingAsync(memStream, firstBlock.Name);
英文:

I want to get a specific block from an azure block blob with the blockId, is this even possible?
something like

  1. var blockBlob = new BlockBlobClient(connectionString, containerName, blobName);
  2. var blocklist = await GetBlobBlockList(blobName, cancellationToken);
  3. var firstBlock = blocklist.First();
  4. var memStream = new MemoryStream();
  5. await blockBlob.DownloadStreamingAsync(memStream, firstBlock.Name);

答案1

得分: 0

应该是可能的,但不会像你在示例代码中提到的那么简单。

你需要做以下几步:

  1. 获取块的列表。列表中的每个元素都将包含块标识和块的大小。
  2. 假设你想获取第“n”个块的数据,你需要遍历列表,从0n - 1的块,并累加每个块的大小。
  3. 接下来,你需要调用DownloadRangeToStreamAsync(Stream, Nullable<Int64>, Nullable<Int64>),其中offset值将是在步骤2中计算的每个块大小的总和,length值将是你希望下载的块的大小。
英文:

> I want to get a specific block from an azure block blob with the
> blockId, is this even possible?

It should be possible to do so however it won't be as simple as you mentioned in your sample code.

Here's what you would need to do:

  1. Fetch list of blocks. Each element in the list will have a block id and the size of the block.
  2. Assuming you want to get data for block "n", what you will do is iterate over the list from 0 to n - 1 block and add the size of each block.
  3. Next you would need to call DownloadRangeToStreamAsync(Stream, Nullable&lt;Int64&gt;, Nullable&lt;Int64&gt;), where your offset value will be the sum of the size of each block calculated in step 2 and length value will be the size of the block you wish to download.

答案2

得分: 0

你需要创建一个块 Blob,以下是操作步骤:

你需要在存储账户下创建一个容器,如下所示:

在容器中,你可以找到块 Blob,如下所示:

要获取块 ID,可以按照以下代码:

  1. using System;
  2. using System.Threading.Tasks;
  3. using Azure.Storage.Blobs;
  4. using Azure.Storage.Blobs.Models;
  5. class Program
  6. {
  7. static async Task Main(string[] args)
  8. {
  9. string connectionString = "DefaultEndpointsProtocol=https;AccountName=<account-name>;AccountKey=<account-key>;EndpointSuffix=core.windows.net";
  10. string containerName = "***";
  11. string blobName = "***.txt";
  12. BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
  13. BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
  14. BlobClient blobClient = containerClient.GetBlobClient(blobName);
  15. BlockList blockList = await blobClient.GetBlockListAsync(BlockListType.All);
  16. string firstBlockId = blockList.Value.First().Name;
  17. Console.WriteLine("First block ID: " + firstBlockId);
  18. }
  19. }

获取块 ID 后,现在可以按照以下代码获取特定块:

  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Azure.Storage.Blobs;
  6. using Azure.Storage.Blobs.Models;
  7. class Program
  8. {
  9. static async Task Main(string[] args)
  10. {
  11. string connectionString = "DefaultEndpointsProtocol=https;AccountName=<account-name>;AccountKey=<account-key>;EndpointSuffix=core.windows.net";
  12. string containerName = "mycontainer---";
  13. string blobName = "myblob**";
  14. string blockId = "1234567**";
  15. BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
  16. BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
  17. BlobClient blobClient = containerClient.GetBlobClient(blobName);
  18. MemoryStream blockData = new MemoryStream();
  19. await blobClient.DownloadStreamingAsync(blockId, blockData, cancellationToken: CancellationToken.None);
  20. Console.WriteLine("Block data:");
  21. Console.WriteLine(Convert.ToBase64String(blockData.ToArray()));
  22. }
  23. }

在上述代码中,你需要替换连接字符串、容器名称、块 ID 和 Blob 名称,如下所示:

通过按照上述步骤操作,你将成功完成任务。

英文:

You need to create a block blob below you can find the procedure:

可以从Azure块Blob中获取特定的块吗?

you need to create a container under storage account as follows:

可以从Azure块Blob中获取特定的块吗?

inside the container you can find the block blob under properties as shown below

可以从Azure块Blob中获取特定的块吗?

To Get the block Id you can follow the code below:

可以从Azure块Blob中获取特定的块吗?

code:

  1. using System;
  2. using System.Threading.Tasks;
  3. using Azure.Storage.Blobs;
  4. using Azure.Storage.Blobs.Models;
  5. class Program
  6. {
  7. static async Task Main(string[] args)
  8. {
  9. string connectionString = &quot;DefaultEndpointsProtocol=https;AccountName=&lt;account-name&gt;;AccountKey=&lt;account-key&gt;;EndpointSuffix=core.windows.net&quot;;
  10. string containerName = &quot;***&quot;;
  11. string blobName = &quot;***.txt&quot;;
  12. BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
  13. BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
  14. BlobClient blobClient = containerClient.GetBlobClient(blobName);
  15. BlockList blockList = await blobClient.GetBlockListAsync(BlockListType.All)
  16. string firstBlockId = blockList.Value.First().Name;
  17. Console.WriteLine(&quot;First block ID: &quot; + firstBlockId);
  18. }
  19. }

After getting the Block Id now you can get the certain block using the code below:

可以从Azure块Blob中获取特定的块吗?

code:

  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Azure.Storage.Blobs;
  6. using Azure.Storage.Blobs.Models;
  7. class Program
  8. {
  9. static async Task Main(string[] args)
  10. {
  11. string connectionString = &quot;DefaultEndpointsProtocol=https;AccountName=&lt;account-name&gt;;AccountKey=&lt;account-key&gt;;EndpointSuffix=core.windows.net&quot;;
  12. string containerName = &quot;mycontainer---&quot;;
  13. string blobName = &quot;myblob**&quot;;
  14. string blockId = &quot;1234567**&quot;;
  15. BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
  16. BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
  17. BlobClient blobClient = containerClient.GetBlobClient(blobName);
  18. MemoryStream blockData = new MemoryStream();
  19. await blobClient.DownloadStreamingAsync(blockId, blockData, cancellationToken: CancellationToken.None);
  20. Console.WriteLine(&quot;Block data:&quot;);
  21. Console.WriteLine(Convert.ToBase64String(blockData.ToArray()));
  22. }
  23. }

In the above code you need to replace connection string, container name, block Id, blob name as below:

可以从Azure块Blob中获取特定的块吗?

By following the above procedure, I got successfully.

huangapple
  • 本文由 发表于 2023年2月18日 01:53:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定