Getting images in the Azure Blob Storage

隐身守侯 提交于 2019-12-11 08:22:09

问题


there are so many tutorials regarding uploading image files to the azure blob, i have successfully uploaded image file to a blob container and now my problem is how to get it, here is my code:

var bitmapImage = new BitmapImage();
bitmapImage.UriSource = new Uri("http://sweetapp.blob.core.windows.net/userprofiles/"+imagename,UriKind.RelativeOrAbsolute);
imgProf.Source = bitmapImage;

am I doing it wrong? or is something that i need to do before displaying it? any answers will be appreciated!

Update:

this is the code that worked for me

Upload:

            var credentials = new StorageCredentials("sweetapp","[my key]");
            var client = new CloudBlobClient(new Uri("http://sweetapp.blob.core.windows.net/"), credentials);   
            var container = client.GetContainerReference("userprofiles");
            await container.CreateIfNotExistsAsync(); 
            var perm = new BlobContainerPermissions();
            perm.PublicAccess = BlobContainerPublicAccessType.Blob;
            var blockBlob = container.GetBlockBlobReference(imgName);
            using (var fileStream = await file.OpenSequentialReadAsync())
            {
            await blockBlob.UploadFromStreamAsync(fileStream);
           }

Getting the image:

     var bitmapImage = new BitmapImage();
     bitmapImage.UriSource = new Uri("http://sweetapp.blob.core.windows.net/userprofiles/"+imagename,UriKind.RelativeOrAbsolute);
     imgProf.Source = bitmapImage;

回答1:


Please check the ACL on the blob container. I believe the error you're getting is because the container's ACL is set as Private (which is the default ACL). For your code above to work, the container's access level should be either Blob or Container. You may find this link useful for understanding various ACL options on blob containers: http://msdn.microsoft.com/en-us/library/windowsazure/dd179354.aspx.

Try the code below to get/set container's ACL:

    storageAccount = new CloudStorageAccount(new StorageCredentials("accountname", "accountkey"), false);
    var container = storageAccount.CreateCloudBlobClient().GetContainerReference("containername");
    //Get the container permission
    var permissions = container.GetPermissions();
    Console.WriteLine("Container's ACL is: " + permissions.PublicAccess);
    //Set the container permission to Blob
    container.SetPermissions(new BlobContainerPermissions()
    {
        PublicAccess = BlobContainerPublicAccessType.Blob,
    });



回答2:


Your codes seems fine. Try to listen to ImageFailed and ImageOpened event to make sure whether it really fails, or just take some time to load :

var bitmapImage = new BitmapImage();
........
bitmapImage.ImageOpened += (sender, args) =>
                       {
                           MessageBox.Show("Image opened");
                       };
bitmapImage.ImageFailed += (sender, args) =>
                       {
                           MessageBox.Show("Image failed");
                       };


来源:https://stackoverflow.com/questions/22014384/getting-images-in-the-azure-blob-storage

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