Azure 应用服务访问文件存储以使用 webjob 复制内容

标签 azure azure-webjobs azure-web-app-service

我有一个托管 WordPress 网站的 Azure 应用服务。我想编写一个控制台应用程序,它将从网站(文件存储)复制文件并将它们粘贴到部署槽中。所有在线资源都谈到用于连接文件存储的“访问 key ”,但我在应用程序服务门户中没有看到类似的内容。我可以使用部署凭据或 Web 部署凭据来访问这些文件吗?

最佳答案

根据您的描述,我建议您可以使用webjob的file tigger来实现您的需求。

链接:webjob-extension

您可以使用文件tigger来观察系统文件路径中的文件变化,您可以找到部署槽的ftp凭证,然后使用它通过webjob的扩展包将文件从生产文件夹上传到部署槽。

更多详细信息,您可以引用以下图片和代码:

1.找到ftp凭证并设置密码

enter image description here

设置用户名和密码

enter image description here

2..从 nugget 包管理器安装 Microsoft.Azure.WebJobs.Extensions 并编写 webjob 方法。

代码如下:

注意:默认文件路径为D:/home/data,如果您的文件位于您的网站文件夹中,则需要更改其路径,如下所示。

   static void Main()
    {
        var config = new JobHostConfiguration();
        FilesConfiguration filesConfig = new FilesConfiguration();
        string home = Environment.GetEnvironmentVariable("HOME");
        if (!string.IsNullOrEmpty(home))
        {
            filesConfig.RootPath = Path.Combine(home, "site");
        }
        config.UseFiles(filesConfig);
        var host = new JobHost(config);
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }

功能:

public static void ImportFile(
                [FileTrigger(@"wwwroot\doc\{name}", "*.*", WatcherChangeTypes.Created | WatcherChangeTypes.Changed)] Stream file,
            FileSystemEventArgs fileTrigger,
            TextWriter log)
        {
            log.WriteLine(string.Format("Processed input file '{0}'!", fileTrigger.Name));
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(string.Format("ftp://yourftpurl.ftp.azurewebsites.windows.net/site/wwwroot/doc/{0}", fileTrigger.Name));
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(@"username", "password");

            Stream requestStream = request.GetRequestStream();
            file.CopyTo(requestStream);
            requestStream.Close();
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            log.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
            response.Close();
        }

结果:

如果您将文件添加到产品的 doc 文件夹中,Web 作业会将其复制到deploymeny_solt 的 doc 文件夹中。

enter image description here

关于Azure 应用服务访问文件存储以使用 webjob 复制内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42656243/

相关文章:

python - 将 python 模块添加到 Azure

用于部署 WebJobs 的 Azure ARM 模板

asp.net - Azure 网站部署任务失败

asp.net - 将 Windows 身份验证与 Azure WebApp/网站结合使用

c# - key 存储提供程序不能设置多次(始终使用 Azure Function 进行加密)

asp.net-mvc - Azure、Asp.Net 和 inproc session

azure - 通过 blob 存储帐户每日备份 Azure Sql Server

nuget - WebJobs 升级到最新的 Nuget 包。升级 app.config 以不匹配 WindwosAzure.Storage DLL 版本

Azure API应用程序代理生成错误

powershell - 使用 Powershell 将虚拟目录添加到 Azure 网站