Getting the File type in Azure Storage

风流意气都作罢 提交于 2019-12-11 12:58:00

问题


Is it possible to get the file type of an image file or blob in Azure Storage? i have researched everything but no to avail. any response will be appreciated! I was wondering, is there anything that i could add in order to get it? here is my code:

ImageSource wew=new BitmapImage(new Uri("http://sweetapp.blob.core.windows.net/cakepictures/"+newfilename+filetypeofthepicture, UriKind.RelativeOrAbsolute)); //need to get the file type of the image
CakeData.Add(new CakeData { Cakename = item.Cakename, ImagePath = wew });

回答1:


Each blob has a property called Content-Type which can be set when the blob is uploaded. You can make use of Storage Client Library to fetch blob properties to get its content type property. If you are using .Net Storage Client Library, you could use code below:

    var blob = new CloudBlockBlob(new Uri("blob uri"), new StorageCredentials("sweetapp", "account key"));
    blob.FetchAttributes();
    var contentType = blob.Properties.ContentType;

This would however require you to include the credentials in your client app. If you don't want to do that, other alternative would be to use Shared Access Signature Token and use that to create an instance of StorageCredentials object. You could create this SAS token somewhere on the server.

    var credentials = new StorageCredentials(sasToken);
    var blob = new CloudBlockBlob(new Uri("blob uri"), credentials);
    blob.FetchAttributes();
    var contentType = blob.Properties.ContentType;

3rd alternative would be access the registry and get the mime-type based on file extension but I'm not sure if a Windows 8 app would have access to the registry.

Last alternative would be to hard code stuff in your application. There's a predefined set of mime-types which you can hard code in your application and based on the file's extension, you can retrieve the content type.



来源:https://stackoverflow.com/questions/22062648/getting-the-file-type-in-azure-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!