1

我需要将一个文件从 Azure Blob 存储复制到我的 Azure 托管网站的“内容”文件夹中,我正在努力完成这项工作!任何帮助将不胜感激。该代码在我的本地服务器上运行良好,但在 Azure 上托管时失败。

这是我的功能:


public bool CopyFromAzure(string myContainer, string fileName, string filePath)
    {
        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
           ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClient.GetContainerReference(myContainer);

        // Retrieve reference to a blob named "myblob".
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

        try
        {
            // Save blob contents to a file. 
            // --> here is where an error happens !! on System.IO.File.Create
            // The value of "filePath" is: F:\sitesroot\0\Content\tmp\file_2cfe0a3d-fa7a-4ab4-a665-cdebd90567d4.pdf

            using(FileStream fs= System.IO.File.Create(@filePath))
            {
                blockBlob.DownloadToStream(fs);
            }
        }
        catch (Exception e)
        {
            return false;
        }

        return true;
    }

问题:这可以链接到 Azure 或我的网站配置中的安全首选项吗?

谢谢

4

1 回答 1

2

问题终于解决了。由于某种原因,Azure 发行版的子文件夹(即:/Content/tmp)不允许写入权限。我通过简单地将我的文件写入项目的根目录来解决 pb 问题。

有效的代码:

            string filePath = HttpContext.Current.Server.MapPath("~/" + fileName);
            using (FileStream fs = System.IO.File.OpenWrite(filePath))
            {
                blockBlob.DownloadToStream(fs);
            }
于 2013-11-08T23:29:23.197 回答