简体   繁体   中英

Azure app service access file storage to copy content using webjob

I have an Azure app service that host a wordpress site. I want to write a console application that will copy files from the website (file storage) and paste them in a deployment slot. All of the online resources talk about "access keys" for connection to the file storage, but I do not see anything like this in the app service portal. Can I use deployment credentials or web deploy credentials to access these files?

According to your description, I suggest you could use webjob's file tigger to achieve your requirement.

Link: webjob-extension

You could use the file tigger to watch the file changes in your system file path, you could find the deployment slot's ftp credential, then use it to upload the file form production folder to the deployment slot by webjob's extension package.

More details, you could refer to follow image and codes:

1.Find the ftp credential and set password

在此处输入图片说明

Set username and password

在此处输入图片说明

2..Install the Microsoft.Azure.WebJobs.Extensions from nugget package manager and write the webjob method.

Codes like below:

Note: The default file path is D:/home/data, if your file inside your website folder, you need change its path as below.

   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();
    }

Function:

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();
        }

Result:

If you add file to the production's doc folder, the web job will copy it to deploymeny_solt's doc folder.

在此处输入图片说明

You could use the "Azure Site Replicator" extension. A slot is like another azure app service, so it should replicate between slots just fine.

In your deployment slot that you want everything copied to, download the Publish Settings from the overview tab by clicking "Get Publish Profile"

In your app service production slot go to Extensions and add the Site Replicator extension. Then after it is installed, click it and click 'Browse.' That will open a new window with the configuration options.

In the configuration window, upload the Publish Settings file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM