【问题标题】:Create a file in a folder of an Azure-hosted website在 Azure 托管网站的文件夹中创建文件
【发布时间】:2013-11-08 08:19:05
【问题描述】:

我需要将文件从 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 或我的网站配置中的安全首选项?

谢谢

【问题讨论】:

    标签: asp.net-mvc file-upload azure azure-storage


    【解决方案1】:

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

    有效的代码:

                string filePath = HttpContext.Current.Server.MapPath("~/" + fileName);
                using (FileStream fs = System.IO.File.OpenWrite(filePath))
                {
                    blockBlob.DownloadToStream(fs);
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-19
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多