简体   繁体   English

在Azure Blob存储中获取图像

[英]Getting images in the Azure Blob Storage

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: 关于将图像文件上传到azure blob的教程太多了,我已经成功将图像文件上传到blob容器,现在我的问题是如何获取它,这是我的代码:

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;

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 : 尝试监听ImageFailedImageOpened事件,以确保它是否真的失败,或者只是花一些时间来加载:

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

Please check the ACL on the blob container. 请检查Blob容器上的ACL I believe the error you're getting is because the container's ACL is set as Private (which is the default ACL). 我相信您收到的错误是因为容器的ACL设置为“ Private (这是默认ACL)。 For your code above to work, the container's access level should be either Blob or Container . 为了使以上代码正常工作,容器的访问级别应为BlobContainer You may find this link useful for understanding various ACL options on blob containers: http://msdn.microsoft.com/en-us/library/windowsazure/dd179354.aspx . 您可能会发现此链接对于了解Blob容器上的各种ACL选项很有用: http : //msdn.microsoft.com/zh-cn/library/windowsazure/dd179354.aspx

Try the code below to get/set container's ACL: 尝试以下代码获取/设置容器的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,
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM