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

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

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

问题

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

var blockBlob = new BlockBlobClient(connectionString, containerName, blobName);
var blocklist = await GetBlobBlockList(blobName, cancellationToken);
var firstBlock = blocklist.First();
var memStream = new MemoryStream();
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

var blockBlob = new BlockBlobClient(connectionString, containerName, blobName);
var blocklist = await GetBlobBlockList(blobName, cancellationToken);
var firstBlock = blocklist.First();
var memStream = new MemoryStream();
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,可以按照以下代码:

using System;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

class Program
{
    static async Task Main(string[] args)
    {
        string connectionString = "DefaultEndpointsProtocol=https;AccountName=<account-name>;AccountKey=<account-key>;EndpointSuffix=core.windows.net";
        string containerName = "***";
        string blobName = "***.txt";
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
        BlobClient blobClient = containerClient.GetBlobClient(blobName);

        BlockList blockList = await blobClient.GetBlockListAsync(BlockListType.All);
        string firstBlockId = blockList.Value.First().Name;
        Console.WriteLine("First block ID: " + firstBlockId);
    }
}

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

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

class Program
{
    static async Task Main(string[] args)
    {
        string connectionString = "DefaultEndpointsProtocol=https;AccountName=<account-name>;AccountKey=<account-key>;EndpointSuffix=core.windows.net";
        string containerName = "mycontainer---";
        string blobName = "myblob**";
        string blockId = "1234567**";
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
        BlobClient blobClient = containerClient.GetBlobClient(blobName);
        MemoryStream blockData = new MemoryStream();
        await blobClient.DownloadStreamingAsync(blockId, blockData, cancellationToken: CancellationToken.None);
        Console.WriteLine("Block data:");
        Console.WriteLine(Convert.ToBase64String(blockData.ToArray()));
    }
}

在上述代码中,你需要替换连接字符串、容器名称、块 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:

using System;  
using System.Threading.Tasks;  
using Azure.Storage.Blobs;  
using Azure.Storage.Blobs.Models;

class Program  
{  
static async Task Main(string[] args)  
{  
string connectionString = &quot;DefaultEndpointsProtocol=https;AccountName=&lt;account-name&gt;;AccountKey=&lt;account-key&gt;;EndpointSuffix=core.windows.net&quot;;  
string containerName = &quot;***&quot;;  
string blobName = &quot;***.txt&quot;;  
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);  
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);  
BlobClient blobClient = containerClient.GetBlobClient(blobName);

  
BlockList blockList = await blobClient.GetBlockListAsync(BlockListType.All)  
string firstBlockId = blockList.Value.First().Name;  
Console.WriteLine(&quot;First block ID: &quot; + firstBlockId);  
}  
}

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

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

code:

using System;  
using System.IO;  
using System.Threading;  
using System.Threading.Tasks;  
using Azure.Storage.Blobs;  
using Azure.Storage.Blobs.Models;

class Program  
{  
static async Task Main(string[] args)  
{  
string connectionString = &quot;DefaultEndpointsProtocol=https;AccountName=&lt;account-name&gt;;AccountKey=&lt;account-key&gt;;EndpointSuffix=core.windows.net&quot;;  
string containerName = &quot;mycontainer---&quot;;  
string blobName = &quot;myblob**&quot;;  
string blockId = &quot;1234567**&quot;;  
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);  
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);  
BlobClient blobClient = containerClient.GetBlobClient(blobName);  
MemoryStream blockData = new MemoryStream();  
await blobClient.DownloadStreamingAsync(blockId, blockData, cancellationToken: CancellationToken.None);  
Console.WriteLine(&quot;Block data:&quot;);  
Console.WriteLine(Convert.ToBase64String(blockData.ToArray()));  
}  
}

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:

确定