使用xamarin.forms将图像上载到windows azure

使用xamarin.forms将图像上载到windows azure,xamarin.forms,Xamarin.forms,我正在尝试制作一个简单的xamarin.forms应用程序,用于从手机图库中拾取图像并将I上载到azure存储服务。 这就是我所做的,但不幸的是我得到了错误消息 “{Microsoft.WindowsAzure.Storage.StorageException:服务器无法验证请求。” 从厨房代码中拾取图像: private MediaFile mediaFile; public ImageUpload () { InitializeComponent ();

我正在尝试制作一个简单的xamarin.forms应用程序,用于从手机图库中拾取图像并将I上载到azure存储服务。 这就是我所做的,但不幸的是我得到了错误消息 “{Microsoft.WindowsAzure.Storage.StorageException:服务器无法验证请求。”

从厨房代码中拾取图像:

 private MediaFile mediaFile;
    public ImageUpload ()
    {
        InitializeComponent ();
    }


    private async void Button_Clicked(object sender, EventArgs e)
    {
        await CrossMedia.Current.Initialize();
        if(CrossMedia.Current.IsPickPhotoSupported==false)
        {
            await DisplayAlert("No Pick", "No Available", "ok");
            return;
        }

        this.mediaFile = await CrossMedia.Current.PickPhotoAsync();
        if (this.mediaFile == null)
            return;

        //Show The path in the Xaml
        LocalImagPath.Text = this.mediaFile.Path;
        //Show the image in the Xaml
        MyImageFile.Source = ImageSource.FromStream(this.mediaFile.GetStream);



    }//end btnClikcked

    private async void uploadPhoto_Clicked(object sender, EventArgs e)
    {
        try
        {
            var imageName = await ImageManager.UploadImage(this.mediaFile);
        }
        catch(Exception ex)
        {
            ex.StackTrace.ToString();
        }


    }//end uploadPhoto_Clicked
这是ImageManger类: 类图像管理器 { 私有静态随机=新随机()

//将新图像上载到blob容器。
公共静态异步任务上载映像(MediaFile映像)
{
CloudBlobContainer容器=GetContainer();
//如果容器不存在,则创建该容器
//wait container.CreateIfNotExistsAsync();
//为新图像使用随机名称
var name=Guid.NewGuid().ToString();
//将映像上载到blob存储
CloudBlockBlob imageBlob=container.GetBlockBlobReference(名称);
imageBlob.Properties.ContentType=“image/jpeg”;
等待imageBlob.UploadFromFileAsync(image.Path);
返回名称;
}
//获取对用于存储图像的容器的引用
私有静态CloudBlobContainer GetContainer()
{
//解析WindowS Azure存储帐户的连接字符串
CloudStorageAccount=CloudStorageAccount.Parse(Configuration.StorageConnectionString);
CloudBlobClient=account.CreateCloudBlobClient();
//获取对图像容器的引用
CloudBlobContainer container=client.GetContainerReference(“bepartphoto”);
//设置权限
BlobContainerPermissions blopPerm=新BlobContainerPermissions();
blopPerm.PublicAccess=BlobContainerPublicAccessType.Container;
container.SetPermissionsAsync(blopPerm);
返回容器;
}//结束GetContainer
}//末级
    // Uploads a new image to a blob container.
    public static async Task<string> UploadImage(MediaFile image)
    {
        CloudBlobContainer container = GetContainer();

        // Creates the container if it does not exist
        //await container.CreateIfNotExistsAsync();

        // Uses a random name for the new images
        var name = Guid.NewGuid().ToString();


        // Uploads the image the blob storage
        CloudBlockBlob imageBlob = container.GetBlockBlobReference(name);
        imageBlob.Properties.ContentType = "image/jpeg";

        await imageBlob.UploadFromFileAsync(image.Path);


        return name;
    }




    // Gets a reference to the container for storing the images
    private static CloudBlobContainer GetContainer()
    {
        // Parses the connection string for the WindowS Azure Storage Account
        CloudStorageAccount account = CloudStorageAccount.Parse(Configuration.StorageConnectionString);
        CloudBlobClient client = account.CreateCloudBlobClient();

        // Gets a reference to the images container
        CloudBlobContainer container = client.GetContainerReference("bepartphoto");

        //Set The Permissions
        BlobContainerPermissions blopPerm = new BlobContainerPermissions();
        blopPerm.PublicAccess = BlobContainerPublicAccessType.Container;
        container.SetPermissionsAsync(blopPerm);

        return container;
    }//end GetContainer


}//end class