2
votes

We have a azure blob storage, with logging enabled. I can view these logs and download the blobs using the Management Portal. But now I'm trying to list those logs using the Client api. Something along the lines:

let account = new CloudStorageAccount(credentials, true)
let client = account.CreateCloudBlobClient()
let container = client.GetContainerReference "$logs"
container.ListBlobs()

But this throws a web exception code 400 Bad Request. I can. however, list blobs from other containers on this client. I understand I need to be authenticated for this container, but I'm using the primary access key for the credentials. So why can't I get the $logs blobs?

Thanks

1
I tried it and it worked for me. Can you tell me what version of storage client library are you using? I used both 1.8 and 2.0 and it worked in both cases.Gaurav Mantri
on my machine it's 1.7.0... um.. where to update? :)sharp johnny
You can either download the latest SDK from windowsazure.com/en-us/downloads or reference latest storage client libraries from Nuget: nuget.org/packages/WindowsAzure.Storage. HTH.Gaurav Mantri
Oh well, I was using depreceated StorageClient, now I'm using the up to date dlls. Now listblobs returns only one item: https://{storage}.blob.core.windows.net/$logs/blob/ though there a lot more blobs in the container...sharp johnny
changing this to answer :)Gaurav Mantri

1 Answers

4
votes

As I mentioned in my comments above, you would need to use the latest version of storage client library which you can get from Nuget: http://nuget.org/packages/WindowsAzure.Storage/.

Here's the sample code:

open Microsoft.WindowsAzure.Storage
open Microsoft.WindowsAzure.Storage.Auth
open Microsoft.WindowsAzure.Storage.Blob

[<EntryPoint>]
let main argv = 
    let credentials = new StorageCredentials("accountname", "accountkey")
    System.Console.WriteLine(credentials.AccountName)
    let account = new CloudStorageAccount(credentials, true)
    System.Console.WriteLine(account.BlobEndpoint)
    let client = account.CreateCloudBlobClient();
    let container = client.GetContainerReference "$logs"
    System.Console.WriteLine(container.Uri)
    let blobs = container.ListBlobs("", true, BlobListingDetails.All, null, null);
    for blob in blobs do
        System.Console.WriteLine(blob.Uri)
    let response = System.Console.ReadLine()
    0 // return an integer exit code

Above code needs Storage Client Library 2.0.

The reason you're getting back only one item is because you're calling ListBlobs function with no parameters. If you look at the definition for this function here (http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.storage.blob.cloudblobcontainer.listblobs.aspx), you'll see that you can get all blobs in a blob container by specifying useFlatBlobListing parameter to true (which I did in the code above). Do give it a try, it would return a list of all blobs in your blob container.