【问题标题】:Write file to Azure Web App from Azure Function?从 Azure Function 将文件写入 Azure Web App?
【发布时间】:2023-03-19 17:13:01
【问题描述】:

Azure 函数是否可以将文件写入 Azure Web 应用程序?

目前,我正在让函数写入存储帐户,然后让 Web 应用从那里拉下文件,但如果我可以直接写入 Web 应用会容易得多。

【问题讨论】:

  • 您如何将文件作为流或以流形式输入 AF?
  • @StronglyTyped,有关于这个问题的更新吗?你能用我的代码上传文件吗?

标签: asp.net azure azure-functions azure-storage


【解决方案1】:

您只能使用ftpkudu rest api 访问网络文件。以下是我的测试代码将文件从函数上传到 azure web wwwroot 文件夹。您可以将此代码用作参考。

public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string filepath = context.FunctionDirectory + "\\test.txt";

            sendmail("ftpusername", "ftpuserpassword", "ftphost/site/wwwroot/test.txt",filepath);


        }

        public static void sendmail(String name, String pass,String ftpadd,String filepath) {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpadd);
            request.Method = WebRequestMethods.Ftp.UploadFile;


            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential(name, pass);

            // Copy the contents of the file to the request stream.
            byte[] fileContents;
            using (StreamReader sourceStream = new StreamReader(filepath))
            {
                fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            }

            request.ContentLength = fileContents.Length;

            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(fileContents, 0, fileContents.Length);
            }

            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                Console.WriteLine($"Upload File Complete, status {response.StatusDescription}");
            }
        }
    }

【讨论】:

    【解决方案2】:

    过去,可以将文件持久保存到 Web 应用本地存储。现在,微软说:

    虽然应用对其自己的临时本地具有完全的读/写权限 存储,该存储确实不打算由 应用程序代码。相反,目的是提供临时文件 用于 IIS 和 Web 应用程序框架的存储。应用服务也 将每个应用程序可用的临时本地存储量限制为 防止个别应用消耗过多的本地文件 存储。

    https://docs.microsoft.com/en-us/azure/app-service/operating-system-functionality

    如果这是一项昂贵的任务,您可以做的是包含 Redis Cache,并将文件发送到 Redis 并从 Web App 上拉取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-19
      • 2021-11-12
      • 2020-04-25
      • 2020-04-08
      • 2023-04-01
      • 1970-01-01
      • 2022-08-18
      • 2017-07-15
      相关资源
      最近更新 更多