USMC6072
USMC6072

Reputation: 656

How do I retrieve the contents of an Azure blob?

I've never done this before but I am saving an object to Azure blob storage by turning it into a string and streaming to Azure. I've checked the blob in Azure and it looks exactly how I would expect, as this:

{"ClientID":"xxxx.xxxxxxxx.xxxx","ClientSecret":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx","RedirectURL":"","GrantToken":"GRANT token","RefreshToken":null,"AccessToken":null,"ExpiresIn":null,"UserMail":null,"Id":null}

When I retrieve the blob, it appears I am only getting the meta-data and the Content element is blank. Here is the code to retreive the blob

        public Token GetToken(UserSignature user, Token token)
        {

            try
            {
                var connectionString = "connection-string-goes-here";
                
                BlobClient blobClient = new BlobClient(connectionString, "container-name-goes-here", "blob-name-goes-here");
                var download = blobClient.DownloadContent();
                BinaryData data = new BinaryData(download);
                var test = data.ToArray();
                var test2 = Encoding.UTF8.GetString(test);
                 Token result = JsonConvert.DeserializeObject<OAuthToken>(test2);

                return token;
            }
            catch (Exception e)
            {
                //ToDo: implement error logging to blob
                
            }

And here is the result after calling '''GetString(test)''' The very last element is Content and its empty.

{"Value":{"Details":{"BlobType":0,"ContentLength":236,"ContentType":"application/octet-stream","ContentHash":"t6oIPlIWyChaWSo9tseiMw==","LastModified":"2021-09-12T18:14:36+00:00","Metadata":{},"ContentRange":null,"ETag":"\u00220x8D976192D9F53B0\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":0,"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"},"Content":{}}}

I need to get the blob and turn it back into a c# object. I've read so many articles and they all show a slightly different way of doing it. Thanks in advance for your help.

Upvotes: 2

Views: 9486

Answers (3)

Muflix
Muflix

Reputation: 6778

https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-download

 public static async Task DownloadBlobToStringAsync(BlobClient blobClient)
 {
     BlobDownloadResult downloadResult = await blobClient.DownloadContentAsync();
     string blobContents = downloadResult.Content.ToString();
 }

Upvotes: 0

USMC6072
USMC6072

Reputation: 656

I'm closing this post and I don't know any way to do this but to answer it. I didn't get the error fixed but I also discovered I was taking the wrong approach. Thanks for assisting me.

Upvotes: 0

Engincan Veske
Engincan Veske

Reputation: 1585

You can use BlobContainerClient.GetBlobClient method to get your blob.


BlobServiceClient blobServiceClient = new BlobServiceClient(yourConnectionString);
BlobContainerClient containerClient = await blobServiceClient.GetBlobContainerClient(yourContainerName);


// Get specific blob
var blob = await containerClient.GetBlobClient(yourBlobName);

It returns a BlobClient. You can use BlobClient's Uri property to see the image you've saved or if you want to download the content you can use DownloadContentAsync method.

var content = await blob.DownloadContentAsync();

Upvotes: 4

Related Questions