How display a list of Azure blob of a Container in

2019-03-05 23:51发布

问题:

How display a list of Azure blob of a Container in Silverlight application?

I know how to do it in regular .Net but I need it in Silverlight. I'm able to upload but I want to show a list of what already uploaded.

Something like this but for Silverlight:

CloudStorageAccount account =
            CloudStorageAccount.FromConfigurationSetting("DataConnectionString");

CloudBlobClient blobClient = account.CreateCloudBlobClient();

IEnumerable<CloudBlobContainer> containers = blobClient.ListContainers()

Thanks

回答1:

There are two ways to communicate with Azure Blob Storage:

  1. .NET based API - this is the one, that you have used in regular .NET app - however this one cannot be used from within Silverlight application
  2. RESTfull HTTP API - this is the one that you might use directly from Silverlight

There is however no build-in library. You will have to write the HTTP requests on your own. That might be little complicated, and it will look something like this:

private void ListFiles()
    {
        var uri = String.Format("{0}{1}", _containerUrl, "?restype=container&comp=list&include=snapshots&include=metadata");

        _webRequest = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri(uri));
        _webRequest.BeginGetResponse(EndListFiles, Guid.NewGuid().ToString());
    }

    private void EndListFiles(IAsyncResult result)
    {
        var doc = _webRequest.EndGetResponse(result);

        var xDoc = XDocument.Load(doc.GetResponseStream());
        var blobs = from blob in xDoc.Descendants("Blob")
                    select ConvertToUserFile(blob);
    //do whatever you need here with the blobs.


    }

Please note, that this supposes, that the container is public. If your container is not public, than you would have two options:

  1. Sign your HTTP request with the application key - that is generally bad ideas, while you are giving your access key to the silverlight applications (which might be distributed over the internet).
  2. Use Shared Access Signatures

You can read more about the options here.

Hope that helps.



回答2:

Okay no magic here. I can use the API or (what I will do) use a WCF Service to fetch Azure blob storage and pass thru this service in my Silverlight application to get the data.
Not magic but not complicated.