Azure CloubdBlob's Properties.Length returns 0
Asked Answered
L

3

8

The following code returns blob file size of 0:

public long GetFileSize(string fileUrl)
{
    var blob = GetBlobContainer().GetBlobReference(fileUrl);
    return blob == null ? 0 : blob.Properties.Length;
}

, its almost as it does not find the blob. But of I delete the blob I see that it gets deleted. This works when deleting:

void DeleteFileFromBlob(string fileUrl, bool deleteSnapshots)
{
    var blob = GetBlobContainer().GetBlobReference(fileUrl);
    if (deleteSnapshots)
    {
        var options = new BlobRequestOptions { DeleteSnapshotsOption = DeleteSnapshotsOption.IncludeSnapshots };
        blob.DeleteIfExists(options);
    }
    else blob.DeleteIfExists();
}

Its basically the same code as the one above, so it seems that the blob is found.

If I iterate through the blobs I get the correct blob file size, like I do when I calculate the total amount of stored bytes i my storage:

public long GetStorageUsageByteSize()
{
    var blobClient = GetBlobClient();
    return (from container in blobClient.ListContainers()
                      select
                      (from CloudBlob blob in
                           container.ListBlobs(new BlobRequestOptions { UseFlatBlobListing = true })
                       select blob.Properties.Length
                      ).Sum()
                     ).Sum();            
}

So, I cant figure out why the CloubdBlob::Properties.Length returns 0 when I use GetBlobReference with a url.

Loring answered 26/4, 2012 at 11:15 Comment(0)
M
15

It looks like you're missing a call to the FetchAttributes method, which loads the blob's metadata:

blob.FetchAttributes(); 

Reference: https://azure.microsoft.com/en-us/documentation/articles/storage-properties-metadata/

Metrist answered 26/4, 2012 at 11:41 Comment(2)
This is now blob.downloadAttributes() in version 2.0.0 of the Azure Java SDK: javadox.com/com.microsoft.azure/azure-storage/2.0.0/com/…Sappanwood
await blob.FetchAttributesAsync() in .NET CoreGitagitel
M
0

Getting Ref from the server should do the trick!

await blobContainer.GetBlobReferenceFromServerAsync(blobPath);
Macadam answered 4/10, 2019 at 1:24 Comment(0)
H
0

The FetchAttributesAsync method of the CloudBlockBlob class retrieves the system properties and user-defined metadata of the blob. Here is an example of how to use FetchAttributesAsync():

// Example 1: Retrieve the properties of an existing blob
CloudBlockBlob blob = container.GetBlockBlobReference("myblob.txt");
await blob.FetchAttributesAsync();
Console.WriteLine("Blob size: " + blob.Properties.Length);
Console.WriteLine("Content type: " + blob.Properties.ContentType);

// Example 2: Update the metadata of a blob
CloudBlockBlob blob = container.GetBlockBlobReference("myblob.txt");
await blob.FetchAttributesAsync();
blob.Metadata["author"] = "John Doe";
await blob.SetMetadataAsync();
Helman answered 26/8, 2023 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.