'How check byte count (size) of existing blob (file) on Azure

I cannot find a decent example of how to use the latest version of Azure BlobClient to get the byte count of an existing blob.

Here is the code I am working with so far. I cannot figure out how to filter the blob I need to find. I can get them all, but it takes ages.

protected BlobContainerClient AzureBlobContainer
    {
        get
        {
            if (!isConfigurationLoaded) { throw new Exception("AzureCloud currently has no configuration loaded"); }
            if (_azureBlobContainer == null)
            {
                if (!string.IsNullOrEmpty(_configuration.StorageEndpointConnection))
                {

                    BlobServiceClient blobClient = new BlobServiceClient(_configuration.StorageEndpointConnection);
                    BlobContainerClient container = blobClient.GetBlobContainerClient(_configuration.StorageContainer);
                    container.CreateIfNotExists();
                    _azureBlobContainer = container;
                }
            }
            return _azureBlobContainer;
        }
    }

public async Task<Response<BlobProperties>> GetAzureFileSize(string fileName)
    {
        BlobClient cloudFile = AzureBlobContainer.GetBlobClient(fileName);
        await foreach (BlobItem blobItem in AzureBlobContainer.GetBlobsAsync())
        {
            Console.WriteLine("\t" + blobItem.Name);
        }

// Am i supposed to just iterate every single blob on there? how do I filter?
        

        return blobProps;
    }

Thoughts?



Solution 1:[1]

Ended up doing it like this... I get that it isn't the proper way, but it works.

public async Task<(BlobClient cloudFile, CopyFromUriOperation result)> MigrateFileToAzureBlobAsync(Uri url, string fileName, long? bytesCount)
{
    BlobClient cloudFile = AzureBlobContainer.GetBlobClient(fileName);
    cloudFile.DeleteIfExists();
    
    BlobCopyFromUriOptions options = new BlobCopyFromUriOptions();
    options.Metadata = new Dictionary<string, string>();
    options.Metadata.Add("confexbytecount", bytesCount.ToString());
    CopyFromUriOperation result = await cloudFile.StartCopyFromUriAsync(url, options);
    Response x = await result.UpdateStatusAsync();
    await result.WaitForCompletionAsync();


    return (cloudFile, result);
}

public async Task<long> GetAzureFileSize(string fileName)
{
    long retVal = 0;
    await foreach (BlobItem blobItem in AzureBlobContainer.GetBlobsAsync(BlobTraits.All, BlobStates.All, fileName))
    {
        if (blobItem.Metadata.TryGetValue("confexbytecount", out string confexByteCount))
        {
            long.TryParse(confexByteCount, out retVal);
        }
    }

    return retVal;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CarComp