The Last Omen
The Last Omen

Reputation: 345

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:

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;

Upvotes: 1

Views: 7884

Answers (2)

Gaurav Mantri
Gaurav Mantri

Reputation: 136196

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

Upvotes: 1

har07
har07

Reputation: 89285

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

Upvotes: 1

Related Questions