一个Azure的托管网站的文件夹中创建文件 [英] Create a file in a folder of an Azure-hosted website

查看:142
本文介绍了一个Azure的托管网站的文件夹中创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Azure的Blob存储的文件复制到我的Azure的内容文件夹中托管网站,而我努力使这项工作!任何帮助将非常AP preciated。
在code ++工程,我的本地服务器上的罚款,但在Azure托管时失败。

I need to copy a file from Azure Blob Storage to the "contents" folder of my Azure hosted website, and I am struggling to make this work ! Any help would be very much appreciated. The code works fine on my local server, but fails when hosted on 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;
    }


问:可以将此链接到安全preferences在Azure中或在我的网站配置


Question: can this be linked to Security preferences in Azure or in my website configuration?

Thxs

推荐答案

问题终于解决了。
出于某种原因,写权限不允许对Azure的分布的子文件夹(例如:/内容/ tmp目录)。我简单地写我的文件到项目的根解决了PB。

Problem finally solved. For some reason, write rights were not allowed on subfolders of the Azure distribution (ie: /Content/tmp). I solved the pb by simply writing my files to the root of the project.

在code的工作:

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

这篇关于一个Azure的托管网站的文件夹中创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆