且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取图像在Azure Blob存储

更新时间:2023-02-08 23:27:29

请查看 ACL 的BLOB容器上。我相信你得到的错误是因为容器的ACL设置为私人(这是默认的ACL)。为了您的code以上的工作,容器的访问级别应该是的Blob 集装箱。你会发现这个链接有用的理解上的blob容器各种ACL选项: HTTP: //msdn.microsoft.com/en-us/library/windowsazure/dd179354.aspx

尝试下面的code获取/设置容器的ACL:

  storageAccount =新CloudStorageAccount(新StorageCredentials(ACCOUNTNAME,accountkey),FALSE);
    变种容器= storageAccount.CreateCloudBlobClient()GetContainerReference(容器名称);
    //获取容器权限
    VAR的权限= container.GetPermissions();
    Console.WriteLine(容器的ACL是:+ permissions.PublicAccess);
    //设置容器权限BLOB
    container.SetPermissions(新BlobContainerPermissions()
    {
        PublicAccess = BlobContainerPublicAccessType.Blob,
    });

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;

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