I met a problem when retrieving data from blob in Azure. My code is like follows:
static async Task Main()
{
BlobServiceClient blobServiceClient = new BlobServiceClient("*");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("*");
var blobClient = containerClient.GetBlobClient("*");
var content = blobClient.DownloadContent();
BinaryData data = new BinaryData(content);
var blobContents = Encoding.UTF8.GetString(data);
Console.WriteLine("\t" data);
}
But the result shows all the properties except the contents area:
{"Value":{"Details":{"BlobType":0,"ContentLength":66924,"ContentType":"application/octet-stream","ContentHash":"JwTlI7cLcG FWuewCaRuaQ==","LastModified":"2021-04-10T00:18:36 00:00","Metadata":{},"ContentRange":null,"ETag":"\u00220x8D8FBB62F1EC10C\u0022","ContentEncoding":null,"CacheControl":null,"ContentDisposition":null,"ContentLanguage":null,"BlobSequenceNumber":0,"CopyCompletedOn":"0001-01-01T00:00:00 00:00","CopyStatusDescription":null,"CopyId":null,"CopyProgress":null,"CopySource":null,"CopyStatus":0,"LeaseDuration":0,"LeaseState":0,"LeaseStatus":1,"AcceptRanges":"bytes","BlobCommittedBlockCount":0,"IsServerEncrypted":true,"EncryptionKeySha256":null,"EncryptionScope":null,"BlobContentHash":null,"TagCount":0,"VersionId":null,"IsSealed":false,"ObjectReplicationSourceProperties":null,"ObjectReplicationDestinationPolicyId":null,"LastAccessed":"0001-01-01T00:00:00 00:00","ImmutabilityPolicy":{"ExpiresOn":null,"PolicyMode":null},"HasLegalHold":false},"Content":{}}}
So I change my code as follows:
class Program
{
static async Task Main()
{
BlobServiceClient blobServiceClient = new BlobServiceClient("*");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("*");
var blobClient = containerClient.GetBlobClient("*");
string localPath = "./data/tmp.datablock";
blobClient.DownloadTo(localPath);
string contents;
using (StreamReader streamReader = new StreamReader("./data/tmp.datablock", Encoding.UTF8))
{
contents = streamReader.ReadToEnd();
}
var options = new JsonSerializerOptions { WriteIndented = true };
string jsonString = System.Text.Json.JsonSerializer.Serialize(contents, options);
Dictionary<string, dynamic>? values = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(contents);
foreach(var pair in values)
{
Console.WriteLine(pair.Value);
}
}
}
And it works.
I just do not know why my first way can not work. And is there any better way to get the content of blob?
Thank all of you!
CodePudding user response:
Result of BlobClient.Download
method is response of type BlobDownloadResult
. To read the content of the blob, you will need to use the Content
property there.
Please try the following code:
static async Task Main()
{
BlobServiceClient blobServiceClient = new BlobServiceClient("*");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("*");
var blobClient = containerClient.GetBlobClient("*");
var response = blobClient.DownloadContent();
BinaryData data = response.Value.Content;
var blobContents = Encoding.UTF8.GetString(data);
Console.WriteLine("\t" data);
}