【问题标题】:Getting images in the Azure Blob Storage在 Azure Blob 存储中获取图像
【发布时间】:2014-02-25 23:14:06
【问题描述】:

关于将图像文件上传到 azure blob 的教程有很多,我已成功将图像文件上传到 blob 容器,现在我的问题是如何获取它,这是我的代码:

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

我做错了吗?还是在显示之前我需要做些什么?任何答案将不胜感激!

更新:

这是对我有用的代码

上传:

            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);
           }

获取图片:

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

【问题讨论】:

  • 这种方法对我来说很合适。你遇到什么问题了吗?
  • 是的,图像没有显示,但是当我看到我的 blob 时。但是当我尝试直接查看它时,例如:sweetapp.blob.core.windows.net/userprofiles/killingZ.png 发生了一些错误,我做错了吗?顺便说一句,我已经更新了问题
  • 你忘记了await container.SetPermissionsAsync(perm);var blockBlob = container.GetBlockBlobReference(imgName);之前的代码行。

标签: c# xaml azure microsoft-metro


【解决方案1】:

请检查 blob 容器上的 ACL。我相信您遇到的错误是因为容器的 ACL 设置为 Private (这是默认 ACL)。为使您的上述代码正常工作,容器的访问级别应为BlobContainer。您可能会发现此链接有助于了解 blob 容器上的各种 ACL 选项:http://msdn.microsoft.com/en-us/library/windowsazure/dd179354.aspx

尝试下面的代码来获取/设置容器的 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,
    });

【讨论】:

  • 嗯,setpermissions 部分给了我这个异常:Microsoft.WindowsAzure.Storage.WrappedStorageException 请注意我正在使用 c# xaml 进行开发,谢谢!
  • 顺便感谢您的知识!你的代码把我带到这里:stackoverflow.com/questions/16571433/…
  • 我用控制台应用程序试了一下。让我看看如何使用 Windows 8 应用程序做到这一点。
【解决方案2】:

您的代码看起来不错。尝试收听ImageFailedImageOpened 事件以确定它是否真的失败,或者只是需要一些时间来加载:

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

【讨论】:

    猜你喜欢
    • 2019-04-03
    • 2020-10-04
    • 2019-12-31
    • 2020-10-08
    • 2018-07-11
    • 2018-05-07
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多