簡體   English   中英

在Azure Blob存儲中獲取圖像

[英]Getting images in the Azure Blob Storage

關於將圖像文件上傳到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;

您的代碼似乎不錯。 嘗試監聽ImageFailedImageOpened事件,以確保它是否真的失敗,或者只是花一些時間來加載:

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

請檢查Blob容器上的ACL 我相信您收到的錯誤是因為容器的ACL設置為“ Private (這是默認ACL)。 為了使以上代碼正常工作,容器的訪問級別應為BlobContainer 您可能會發現此鏈接對於了解Blob容器上的各種ACL選項很有用: http : //msdn.microsoft.com/zh-cn/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,
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM