{"id":678,"date":"2018-10-22T11:12:19","date_gmt":"2018-10-22T09:12:19","guid":{"rendered":"http:\/\/cwiok.pl\/?p=678"},"modified":"2018-10-22T11:12:19","modified_gmt":"2018-10-22T09:12:19","slug":"usuwanie-plikow-z-ftp-przy-pomocy-azure-data-factory-podejscie-drugie","status":"publish","type":"post","link":"https:\/\/cwiok.pl\/index.php\/pl\/2018\/10\/22\/usuwanie-plikow-z-ftp-przy-pomocy-azure-data-factory-podejscie-drugie\/","title":{"rendered":"Usuwanie plik\u00f3w z FTP przy pomocy Azure Data Factory. Podej\u015bcie drugie."},"content":{"rendered":"<p>Ze wzgl\u0119du na popularno\u015b\u0107 postu, postanowi\u0142em zrobi\u0107 drugie podej\u015bcie do usuwania plik\u00f3w z FTP przy pomocy pipeline&#8217;u Data Factory. W mojej ocenie poni\u017csze rozwi\u0105zane jest lepsze, \u0142atwiejsze do debugowania i zapewnia wi\u0119ksz\u0105 kontrol\u0119.<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/this-is-azuredatafactory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-675\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/this-is-azuredatafactory.png\" alt=\"\" width=\"1500\" height=\"653\" srcset=\"https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/this-is-azuredatafactory.png 1500w, https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/this-is-azuredatafactory-300x131.png 300w, https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/this-is-azuredatafactory-768x334.png 768w, https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/this-is-azuredatafactory-1024x446.png 1024w\" sizes=\"auto, (max-width: 1500px) 100vw, 1500px\" \/><\/a><\/div>\n<p>Najpierw, przeczytaj <a href=\"http:\/\/cwiok.pl\/index.php\/pl\/2018\/10\/15\/tworzenie-azure-key-vault\/\">m\u00f3j artyku\u0142 o Key Vault<\/a>. Jest to istotna wiedza, poniewa\u017c b\u0119dziesz tam przechowywa\u0142 po\u015bwiadczenia do serwera FTP. Stw\u00f3rz Key Vault i dwa sekrety &#8211; jeden dla loginu i jeden dla has\u0142a. Mo\u017cesz te\u017c przechowa\u0107 tam adres IP serwera.<\/p>\n<p>Nast\u0119pnie, stw\u00f3rz now\u0105 Azure Function:<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-12.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-12.png\" alt=\"image\" width=\"941\" height=\"653\" border=\"0\" \/><\/a><\/div>\n<p>&nbsp;<\/p>\n<p>Wybierz v2:<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-13.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-13.png\" alt=\"image\" width=\"724\" height=\"415\" border=\"0\" \/><\/a><\/div>\n<p>Nast\u0119pnie wklej poni\u017cszy kod do rozwi\u0105zania:<\/p>\n<pre class=\"toolbar:2 wrap:true lang:c# decode:true \">using System;\r\nusing System.IO;\r\nusing System.Threading.Tasks;\r\nusing Microsoft.AspNetCore.Mvc;\r\nusing Microsoft.Azure.WebJobs;\r\nusing Microsoft.Azure.WebJobs.Extensions.Http;\r\nusing Microsoft.AspNetCore.Http;\r\nusing Microsoft.Extensions.Logging;\r\nusing Newtonsoft.Json;\r\nusing System.Collections.Generic;\r\nusing System.Net;\r\nusing Microsoft.Azure.Services.AppAuthentication;\r\nusing Microsoft.Azure.KeyVault;\r\n\r\nnamespace DeleteFromFTP_Function\r\n{\r\n     public static class DeleteFromFTP\r\n     {\r\n         [FunctionName(\"DeleteFromFTP\")]\r\n         public static async Task&lt;IActionResult&gt; Run(\r\n             [HttpTrigger(AuthorizationLevel.Function, \"get\", \"post\", Route = null)] HttpRequest req,\r\n             ILogger log)\r\n         {\r\n             log.LogInformation(\"C# HTTP trigger function processed a request.\");\r\n\r\n            var azureServiceTokenProvider = new AzureServiceTokenProvider();\r\n             var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));\r\n             string Login = kv.GetSecretAsync(Environment.GetEnvironmentVariable(\"FTPLogin\")).Result.Value;\r\n             string Password = kv.GetSecretAsync(Environment.GetEnvironmentVariable(\"FTPPassword\")).Result.Value;\r\n\r\n           \r\n            \r\n             string Path = req.Query[\"Path\"];\r\n             string IP = req.Query[\"IP\"];\r\n           \r\n             string requestBody = await new StreamReader(req.Body).ReadToEndAsync();\r\n             dynamic data = JsonConvert.DeserializeObject(requestBody);\r\n             Path = Path ?? data?.Path;\r\n             IP = IP ?? data?.IP;\r\n\r\n\r\n             DeleteFTPDirectory(Path + \"\/\", IP, Login, Password);\r\n             return (ActionResult)new OkObjectResult(data);\r\n\r\n           \r\n\r\n               \r\n         }\r\n         public static List&lt;string&gt; DirectoryListing(string Path, string ServerAdress, string Login, string Password)\r\n         {\r\n             FtpWebRequest request = (FtpWebRequest)WebRequest.Create(\"ftp:\/\/\" + ServerAdress + Path);\r\n             request.Credentials = new NetworkCredential(Login, Password);\r\n\r\n            request.Method = WebRequestMethods.Ftp.ListDirectory;\r\n\r\n            FtpWebResponse response = (FtpWebResponse)request.GetResponse();\r\n             Stream responseStream = response.GetResponseStream();\r\n             StreamReader reader = new StreamReader(responseStream);\r\n\r\n            List&lt;string&gt; result = new List&lt;string&gt;();\r\n\r\n            while (!reader.EndOfStream)\r\n             {\r\n                 result.Add(reader.ReadLine());\r\n             }\r\n\r\n            reader.Close();\r\n             response.Close();\r\n\r\n            return result;\r\n         }\r\n         public static void DeleteFTPFile(string Path, string ServerAdress, string Login, string Password)\r\n         {\r\n             FtpWebRequest clsRequest = (System.Net.FtpWebRequest)WebRequest.Create(\"ftp:\/\/\" + ServerAdress + Path);\r\n             clsRequest.Credentials = new System.Net.NetworkCredential(Login, Password);\r\n\r\n            clsRequest.Method = WebRequestMethods.Ftp.DeleteFile;\r\n\r\n            string result = string.Empty;\r\n             FtpWebResponse response = (FtpWebResponse)clsRequest.GetResponse();\r\n             long size = response.ContentLength;\r\n             Stream datastream = response.GetResponseStream();\r\n             StreamReader sr = new StreamReader(datastream);\r\n             result = sr.ReadToEnd();\r\n             sr.Close();\r\n             datastream.Close();\r\n             response.Close();\r\n         }\r\n         public static void DeleteFTPDirectory(string Path, string ServerAdress, string Login, string Password)\r\n         {\r\n             FtpWebRequest clsRequest = (System.Net.FtpWebRequest)WebRequest.Create(\"ftp:\/\/\" + ServerAdress + Path);\r\n             clsRequest.Credentials = new System.Net.NetworkCredential(Login, Password);\r\n\r\n            List&lt;string&gt; filesList = DirectoryListing(Path, ServerAdress, Login, Password);\r\n\r\n            foreach (string file in filesList)\r\n             {\r\n                 DeleteFTPFile(Path + file, ServerAdress, Login, Password);\r\n             }\r\n\r\n\r\n         }\r\n     }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Zauwa\u017c, \u017ce login i has\u0142o s\u0105 pobierane z Key Vault:<\/p>\n<pre class=\"toolbar:2 wrap:true lang:c# decode:true \">            string Login = kv.GetSecretAsync(Environment.GetEnvironmentVariable(\"FTPLogin\")).Result.Value;\r\n            string Password = kv.GetSecretAsync(Environment.GetEnvironmentVariable(\"FTPPassword\")).Result.Value;<\/pre>\n<p>&nbsp;<\/p>\n<p>Adresy URL sekret\u00f3w s\u0105 w zmiennych \u015brodowiskowych, kt\u00f3re mo\u017cesz znale\u017a\u0107 w ustawieniach aplikacji:<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-14.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-14.png\" alt=\"image\" width=\"566\" height=\"364\" border=\"0\" \/><\/a><\/div>\n<p>&nbsp;<\/p>\n<p>W &#8220;applications settings&#8221;:<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-15.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-15.png\" alt=\"image\" width=\"349\" height=\"274\" border=\"0\" \/><\/a><\/div>\n<p>&nbsp;<\/p>\n<p>Nast\u0119pny krok to publikacja funkcji. Mo\u017cesz to zrobi\u0107 z poziomu Visual Studio klikaj\u0105c prawym na projekt:<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-16.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-16.png\" alt=\"image\" width=\"485\" height=\"181\" border=\"0\" \/><\/a><\/div>\n<p>Przy publikacji wybierz Azure Function, do kt\u00f3rej chcesz wys\u0142a\u0107 swoja funkcj\u0119 lub stw\u00f3rz now\u0105. <strong>Pami\u0119taj, \u017ceby zarejestrowa\u0107 swoj\u0105 aplikacj\u0119 w <\/strong><strong>Azure Active Directory i doda\u0107 j\u0105 do access policies w Key Vault.<\/strong><\/p>\n<p>Po publikacji, przejd\u017a do Azure Portal i przetestuj swoj\u0105 funkcj\u0119. Oczekuje pliku JSON w takim formacie:<\/p>\n<pre class=\"toolbar:2 wrap:true lang:default decode:true \">{\r\n\r\n\"Path\":\"&lt;Path to the folder you want to clear&gt;\",\r\n\r\n\"IP\":\"&lt;IP of your FTP&gt;\"\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Je\u015bli nie dzia\u0142a, mo\u017cesz to naprawi\u0107 tutaj.<\/p>\n<p>Kolejnym krokiem jest wywo\u0142anie zapytania POST z poziomu Data Factory. Aby to zrobi\u0107, u\u017cyjesz web activity. Najpierw, skopiuj URL funkcji. Aby go zdoby\u0107, przejd\u017a do g\u0142\u00f3wnej menu funkcji i kliknij na &lt;\/&gt; Get Function URL:<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-17.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-21.png\" alt=\"image\" width=\"644\" height=\"61\" border=\"0\" \/><\/a><\/div>\n<p>&nbsp;<\/p>\n<p>Skopiuj adres URL:<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-20.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-22.png\" alt=\"image\" width=\"646\" height=\"152\" border=\"0\" \/><\/a><\/div>\n<p>&nbsp;<\/p>\n<p>Jeste\u015b gotowy, \u017ceby przej\u015b\u0107 do Data Factory. Stw\u00f3rz web activity:<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-21.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-23.png\" alt=\"image\" width=\"204\" height=\"229\" border=\"0\" \/><\/a><\/div>\n<p>&nbsp;<\/p>\n<p>Nast\u0119pnie dodaj adres URL i Body w odpowiednim formacie.Mo\u017cesz nawet doda\u0107 elementy dynamiczne:<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image-22.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border: 0px currentcolor; display: inline; background-image: none;\" title=\"image\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/image_thumb-24.png\" alt=\"image\" width=\"559\" height=\"349\" border=\"0\" \/><\/a><\/div>\n<p>I to tyle! Teraz mo\u017cesz wywo\u0142a\u0107 funkcj\u0119 i sprawdzi\u0107 jak dzia\u0142a.<\/p>\n<p>Micha\u0142<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ze wzgl\u0119du na popularno\u015b\u0107 postu, postanowi\u0142em zrobic drugie podej\u015bcie do usuwania plik\u00f3w z FTP przy pomocy pipeline&#8217;u Data Factory. W mojej ocenie poni\u017csze rozwi\u0105zane jest lepsze, \u0142atwiejsze do debugowania i zapewnia wi\u0119ksz\u0105 kontrol\u0119.<\/p>\n<div align=\"center\"><a href=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/this-is-azuredatafactory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-675\" src=\"http:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/this-is-azuredatafactory.png\" alt=\"\" width=\"1500\" height=\"653\" srcset=\"https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/this-is-azuredatafactory.png 1500w, https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/this-is-azuredatafactory-300x131.png 300w, https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/this-is-azuredatafactory-768x334.png 768w, https:\/\/cwiok.pl\/wp-content\/uploads\/2018\/10\/this-is-azuredatafactory-1024x446.png 1024w\" sizes=\"auto, (max-width: 1500px) 100vw, 1500px\" \/><\/a><\/div>\n<div class=\"tech_read_more\"><a href=\"https:\/\/cwiok.pl\/index.php\/pl\/2018\/10\/22\/usuwanie-plikow-z-ftp-przy-pomocy-azure-data-factory-podejscie-drugie\/\">Read More<\/a><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-678","post","type-post","status-publish","format-standard","hentry","category-azure-pl"],"_links":{"self":[{"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/posts\/678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/comments?post=678"}],"version-history":[{"count":0,"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/posts\/678\/revisions"}],"wp:attachment":[{"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/media?parent=678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/categories?post=678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cwiok.pl\/index.php\/wp-json\/wp\/v2\/tags?post=678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}