1
votes

I'm using azure blob storage to store some files.

I would like to retrieve the download count for a given blob stored in azure blob storage.

How do I do that?

Currently, i'm looking at TotalEgress, but that does not suffice, as TotalEgress only gives you the downloaded filesize for the blobstorage service in Total.

Maybe a Egress per blob exists, or something similar...?

Any help on this?

1

1 Answers

1
votes

As far as I know, the blob storage doesn't support getting the download count directly now.

I suggest you could add feedback about this requirment.

Here are two workaround:

1.On the client side.

You could try to count the download number by creating a application.

If user click the download button or something else, get the number of downloading count(you could store it into blob file matadata) and calculate.

Notice: the azure blob has public and private permission. If the blob is public we could directly download the blob from the url. So I suggest you could try to set the blob permission to private. By doing this the number of downloading count t is right.

2.On the server side.

As far as I know, if you enable the azure storage account's diagnostics' blob logs, it will log the blob's each read/write/delete operations.

By reading these operations, I think you could get the download(getblob operation) count for a given blob stored in azure blob storage.

These logs are storing in the $log container.

Notice: The storage log has size limit(20TB) and date limit(MAX 365). So I suggest you could try to run a timertrigger webjob to search the logs. In this webjobs, you could use azure storage SDK's CloudAnalyticsClient to get the logs and store the number and scanned date in the file metadata. Each time when the timertrigger webjob runs, you just need to search the context from last scanned date.(This workaround may have some delay.)

More details about how to use CloudAnalyticsClient to get the log records, you could refer to below code sample:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("");

CloudAnalyticsClient c1 = storageAccount.CreateCloudAnalyticsClient();

DateTimeOffset starttime = DateTime.Now.AddHours(-6);
DateTimeOffset endtime = DateTime.Now;


var r1 = c1.ListLogRecords(Microsoft.WindowsAzure.Storage.Shared.Protocol.StorageService.Blob, starttime, endtime).ToList();
if (r1 != null)
{

    Console.WriteLine("Start");
}
int i = 0;
foreach (var item in r1)
{

    if (item.RequestUrl.ToString().Contains("Penjs.png(your file name)"))
    {
        Console.WriteLine(string.Format("AuthenticationType : {0} , ClientRequestId : {1} , ReferrerHeader : {2} , RequestUrl : {3} , RequestStatus : {4} , HttpStatusCode : {5} , OperationType : {6}", item.AuthenticationType, item.ClientRequestId, item.ReferrerHeader, item.RequestUrl, item.RequestStatus, item.HttpStatusCode, item.OperationType));
        Console.WriteLine("----------------------------------");
    }
    //count the get blob number
    if (item.RequestUrl.ToString().Contains("Penjs.png") && item.OperationType == "GetBlob" && item.RequestStatus =="Success")
    {
        i++;
    }

}

Console.WriteLine("Get/Dowanload blob time : " + i);

Console.WriteLine("Complete");

Result:

enter image description here