【问题标题】:UWP CloudBlob.DownloadFileAsync Access Denied errorUWP CloudBlob.DownloadFileAsync 访问被拒绝错误
【发布时间】:2023-03-20 05:48:01
【问题描述】:

我正在编写一个简单的 UWP 应用程序,其中用户将 InkCanvas 笔触信息发送到 Azure blockBlob,然后检索一些不同的 InkCanvas 笔触容器信息以呈现到画布。

我使用 StrokeContainer.saveAsync() 将 .ink 文件保存到 applicationData 本地文件夹到相同的位置和相同的文件名(它被每个事务替换),然后使用 CloudBlockBlob.uploadAsync() 上传它。

尝试从我的 Azure 服务器下载文件时出现问题 - 我收到“拒绝访问”错误。

 async private void loadInkCanvas(string name)
    {
        //load ink canvas
        //add strokes to the end of the name
        storageInfo.blockBlob = storageInfo.container.GetBlockBlobReference(name + "_strokes"); 
        //check to see if the strokes file exists
        if (await storageInfo.blockBlob.ExistsAsync){
            //then the stroke exists, we can load it in.
            StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
            StorageFile storageFile = await storageFolder.CreateFileAsync("ink.ink", CreationCollisionOption.ReplaceExisting);
            using (var outputStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                await storageInfo.blockBlob.DownloadToFileAsync(storageFile);//gives me the "Access Denied" error here

            }
        }
    }

任何帮助将不胜感激,我在网上发现的只是您不应该将直接路径放入目标位置,而是使用 ApplicationData.Current.LocalFolder 。

【问题讨论】:

标签: c# azure uwp


【解决方案1】:

DownloadToFileAsync 方法确实可以帮助您将文件流从 azure 存储读取到本地文件,您无需自己打开本地文件进行读取。在您的代码中,您打开文件流并使文件被占用,然后调用DownloadToFileAsync 方法尝试访问导致“访问被拒绝”异常的占用文件。

解决方法很简单,不用打开直接下载到本地文件,代码如下:

if (await blockBlob.ExistsAsync())
{
   //then the stroke exists, we can load it in.
    StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    StorageFile storageFile = await storageFolder.CreateFileAsync("ink.ink", CreationCollisionOption.ReplaceExisting);
    await blockBlob.DownloadToFileAsync(storageFile);    
}

如果你想自己读取文件流,你需要使用DownloadToStreamAsync方法而不是DownloadToFileAsync,如下:

if (await blockBlob.ExistsAsync())
{       
   StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
   StorageFile storageFile = await storageFolder.CreateFileAsync("ink.ink", CreationCollisionOption.ReplaceExisting);
   //await blockBlob.DownloadToFileAsync(storageFile);
   using (var outputStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
   {
       await blockBlob.DownloadToStreamAsync(outputStream.AsStreamForWrite());    
   }
}

【讨论】:

  • 谢谢!是的,我发现那是我的问题,我一定是不小心把它写在那里,错过了最明显的解决方案。
猜你喜欢
  • 2018-05-17
  • 2019-06-28
  • 2021-09-18
  • 2018-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-24
相关资源
最近更新 更多