{"id":72171,"date":"2017-08-11T17:10:41","date_gmt":"2017-08-11T17:10:41","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=72171"},"modified":"2021-02-23T19:24:57","modified_gmt":"2021-02-23T19:24:57","slug":"uploading-files-azure-data-lake-using-net-app","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/cloud\/azure\/uploading-files-azure-data-lake-using-net-app\/","title":{"rendered":"Uploading Files to Azure Data Lake Using a .NET App"},"content":{"rendered":"<p>Azure Data Lake allows you to rapidly store and ingest data simply whatever its form, and do any type of processing and analysis on it across platforms and languages. It is ideal for batch, streaming and interactive analytics because it provides a unified repository for diverse enterprise data requirements.<\/p>\n<p>There are many ways of uploading the files to Data Lake store, using Azure Data Factory Services, using Azure Data Lake Store .NET SDK, Java SDK, Node, REST API and so on. In this article, we will look into how to use the Azure Data Lake Store .NET SDK for uploading files from local system to Azure Data Lake store. To do this, you will need Visual studio 2015\/2017 and an Azure Subscription<\/p>\n<p>If you haven\u2019t already done so, the first step is to create a Data Lake Store in Azure to get started.<\/p>\n<h2>Register Azure App<\/h2>\n<p>All external application interactions are controlled or secured using an Azure Active Directory App. We will start off by creating a Azure Active Directory App for our Data Lake solution. Go to <a href=\"https:\/\/portal.azure.com\">https:\/\/portal.azure.com<\/a> and select \u2018<em>Azure Active Directory\u2019<\/em> from the left-side navigation bar.<\/p>\n<p>Select the \u2018<em>App registration\u2019<\/em> option from the Azure Active Directory blade. This will open a blade with list of registered apps and the option to register a new application. Select <strong>New application registration<\/strong> button on top of the blade.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"579\" height=\"191\" class=\"wp-image-72175\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/word-image-49.png\" \/><\/p>\n<p>This will open the \u2018<strong>Create\u2019<\/strong> blade to create a new app. Specify the name and Sign-on URL. Select the application type as \u2018<em>Web App\/API\u2019<\/em> or \u2018<em>Native<\/em>\u2019.The \u2018<em>Native<\/em>\u2019 term indicates that the application will be installed in the user device or computer. \u2018<em>Web App\/API\u2019<\/em> means the authentication is from a web-based application.<\/p>\n<p>Because we are planning to use a console application for uploading the files, we will add a dummy Sign-on URL such as http:\/\/appname.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"294\" height=\"277\" class=\"wp-image-72176\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/word-image-50.png\" \/><\/p>\n<h2>Grant Permission in Data Lake Store<\/h2>\n<p>This AD app will be used to authenticate the users to upload files from local system to Azure Data Lake store. This application will need appropriate permission to perform activities in the target resource, that is, the Data Lake store. We can provide the necessary permission to the App by adding the app under <strong>Access<\/strong> section of Data Lake Store.<\/p>\n<p>Navigate to the Data Lake Store and select the \u2018<em>Data Explorer\u2019<\/em> option.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"173\" class=\"wp-image-72177\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/word-image-51.png\" \/><\/p>\n<p>Select the \u2018<em>Access<\/em>\u2019 option from the Data Lake Store blade. You can select the specific folder and specify access at the folder level, if required. In case you receive a \u201c<strong>Forbidden<\/strong>\u201d exception at the time of file copy, that indicates that your app don\u2019t have enough permission to do the operation. Verify the app permissions at root level to fix the exception.<\/p>\n<p><em>Note the Examples folder in the Store list. We will be using this folder for uploading files from .NET code. <\/em><\/p>\n<p>The \u2018<em>Access\u2019<\/em> blade will display the allowed users and apps list along with the type of permission that each of the users or apps have for this particular folder. Click on the \u2018<em>Add\u2019<\/em> option at the top to grant permission for a new user or app.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"458\" height=\"216\" class=\"wp-image-72181\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/word-image-55.png\" \/><\/p>\n<p>Select the previously created app name from the \u2018<em>Select User or Group\u2019<\/em> blade and click <em>\u2019OK<\/em><strong>\u2019<\/strong>. You can search for a particular user or app by entering a few characters in the \u2018<em>Select\u2019<\/em> box; a filtered list of users or apps will be listed down.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"513\" height=\"212\" class=\"wp-image-72182\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/word-image-56.png\" \/><\/p>\n<p>Select the required permissions for specific apps from the Permissions list. Also, we can restrict the apps\u2019 access to a specific folder or sub folders. Access permission entry is for files and folders that control the access to that object. Default permission entry is for a folder that controls the access permissions associated with the child elements.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"565\" height=\"318\" class=\"wp-image-72184\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/word-image-58.png\" \/><\/p>\n<h2>File Upload Solution<\/h2>\n<p>Now we can create the solution to upload files from local system to Azure Data Lake Store. Open VS 2015\/2017 and create a new console application.<\/p>\n<p>Add the following two Nuget packages to the project.<\/p>\n<ul>\n<li>Microsoft.Azure.Management.DataLake.Store<\/li>\n<li>Microsoft.Rest.ClientRuntime.Azure.Authentication<\/li>\n<\/ul>\n<p>Right-click on the project and select \u2018<em>Manage Nuget Packages\u2026<\/em>\u2019 to add the required packages<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/moone.png\" \/><\/p>\n<p>Navigate to \u2018<em>Browse<\/em>\u2019 and enter the Nuget package name in the search box to find the latest version of the Nuget package. Install the selected package by clicking <em>\u2018Install\u2019<\/em>.<\/p>\n<p>I have used<\/p>\n<ul>\n<li>Microsoft.Azure.Management.DataLake.Store v2.2.0<\/li>\n<li>Microsoft.Rest.ClientRuntime.Azure.Authentication v2.3.1<\/li>\n<\/ul>\n<p>for this walk-through.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"604\" height=\"195\" class=\"wp-image-72191\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/word-image-64.png\" \/><\/p>\n<p>Before we start coding, we should collect the required data from Azure resources. We need details from the Azure Active Directory App for authentication. Navigate to the previously created AD App blade.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"358\" class=\"wp-image-72195\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/word-image-68.png\" \/><\/p>\n<p>Copy the Application ID mentioned in the App blade, which will be used as Client Id in the .NET application. Click on <em>\u2018Keys\u2019<\/em> to generate a new key or client secret.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"801\" height=\"294\" class=\"wp-image-72196\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/word-image-69.png\" \/><\/p>\n<p>Enter a key name as \u2018<em>Description\u2019<\/em> and select the expiry duration from the drop-down. Click on the \u2018<em>Save<\/em>\u2019 icon on top to generate the Key.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/twomo.png\" \/><\/p>\n<p>Copy the Key, which will be hidden after you leave this blade. This key will be used as a client secret in the .NET application.<\/p>\n<p>Now, go back to our .NET Console application and start coding. Declare the Data Lake Store Account Management Client and Data Lake Store File System Management Client objects.<\/p>\n<pre class=\"theme:vs2012 lang:c# decode:true\">private static DataLakeStoreAccountManagementClient adlsClient;\r\n  private static DataLakeStoreFileSystemManagementClient adlsFileSystemClient;<\/pre>\n<p>Declare the corresponding assembly<\/p>\n<pre class=\"theme:vs2012 lang:c# decode:true\">using Microsoft.Azure.Management.DataLake.Store;<\/pre>\n<p>Next, define the Data Lake Store property values. Select the newly created Data Lake store from the list of all resources and navigate to the Data Lake Store blade<\/p>\n<pre class=\"theme:vs2012 lang:c# decode:true\"><img loading=\"lazy\" decoding=\"async\" width=\"974\" height=\"253\" class=\"wp-image-72199\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/word-image-72.png\" \/>\r\n     adlsAccountName = \"adldatastore\";\r\n     subscriptionId = \"73d233254634e-6a55-22f2-xxxx-1f7baaaaab5fd\";<\/pre>\n<p>Create the authentication token using the Azure Active Directory App details. Open the Azure Activity Directory blade and select Domain names to capture the domain name.<\/p>\n<pre class=\"theme:vs2012 lang:c# decode:true\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"373\" class=\"wp-image-72200\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/word-image-73.png\" \/><\/pre>\n<p>You can get the domain name associated with the app from the App ID Uri mentioned under the App properties. App ID Uris start with the domain name only.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/08\/threemo.png\" \/><\/p>\n<pre class=\"theme:vs2012 lang:c# decode:true\">SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());\r\n              var domain = \"xxxxxxxxxxxlook.onmicrosoft.com\";  \r\n              var clientId = \"cb44a3434-xx07c-4yyy-8aaab-ffffffdeec8c\";  \r\n              var clientSecret = \"3TnHk86Jmk8fTMKnpt75s8KZnpbbpBQ1H30=\"; \r\n              var clientCredential = new ClientCredential(clientId, clientSecret);\r\n              var creds = ApplicationTokenProvider.LoginSilentAsync(domain, clientCredential).Result; <\/pre>\n<p>Include the required assembly references<\/p>\n<pre class=\"theme:vs2012 lang:c# decode:true\">using Microsoft.IdentityModel.Clients.ActiveDirectory;\r\n  using Microsoft.Rest.Azure.Authentication;\r\n  using System.Threading;<\/pre>\n<p>Now we can create the objects of Data Lake Store Account Management Client and Data Lake Store File System Management Client and assign a subscription id.<\/p>\n<pre class=\"theme:vs2012 lang:c# decode:true\">adlsClient = new DataLakeStoreAccountManagementClient(creds);\r\n  adlsFileSystemClient = new DataLakeStoreFileSystemManagementClient(creds);\r\n  adlsClient.SubscriptionId = subscriptionId;<\/pre>\n<p>Define the source and destination file paths. The source file path will be the physical file location in the server or local system. The Destination will be the Azure Data Lake Store folder with the target file name. It is not mandatory to upload the file with the same name. You can upload a file by specifying a new name.<\/p>\n<pre class=\"theme:vs2012 lang:c# decode:true\">string sourceFolderPath = @\"D:\\Agile\"; \r\n  string sourceFilePath = Path.Combine(sourceFolderPath, \"Hello.txt\");\r\n       \tstring dataLakeStoreFolderPath = \"\/Examples\/\";\r\n         string dataLakeStoreFilePath = Path.Combine(dataLakeStoreFolderPath, \"file.txt\");<\/pre>\n<p>This code will copy the Hello.txt file from D:\\Agile with a new name file.txt into the Azure Data Lake Store folder \/Example.<\/p>\n<p>We can copy the file to Data Lake Store using the <strong>UploadFile <\/strong>method.<\/p>\n<pre class=\"theme:vs2012 lang:c# decode:true\">adlsFileSystemClient.FileSystem.UploadFile(adlsAccountName, sourceFilePath, dataLakeStoreFilePath,1,false,true);<\/pre>\n<p>Parameters are:<\/p>\n<ul>\n<li>adlsAccountName \u2013 account name or the Data Lake Store name<\/li>\n<li>sourceFilePath \u2013 source file path<\/li>\n<li>dataLakeStoreFilePath \u2013 destination file path<\/li>\n<li>1 \u2013 how many threads needs to be used for upload. By default, it calculates based on file size<\/li>\n<li>False \u2013 indicate this upload process is a new one, not a continuation from previously paused upload<\/li>\n<li>True \u2013 Overwrite the file or not<\/li>\n<\/ul>\n<p>Here is the complete application code for uploading a file from the local system to the Azure Data Lake Store folder. You can customize this by adding <strong>Foreach<\/strong> or <strong>Parallel Foreach<\/strong> to upload multiple files at a time.<\/p>\n<pre class=\"theme:vs2012 lang:c# decode:true\">  class Program\r\n      {\r\n          private static DataLakeStoreAccountManagementClient adlsClient;\r\n          private static DataLakeStoreFileSystemManagementClient adlsFileSystemClient;\r\n          private static string adlsAccountName;   \r\n          private static string subscriptionId;\r\n          static void Main(string[] args)\r\n          {\r\n              adlsAccountName = \"adlsdatastore\";\r\n              subscriptionId = \"73d233254634e-6a55-22f2-xxxx-1f7baaaaab5fd\";\r\n                     \r\n              SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());\r\n              var domain = \"yyyyyyyyylook.onmicrosoft.com\";  \r\n              var clientId = \"cb88888254-xxxx-mmm-yyyy-07e3e986jh68c\";  \r\n              var clientSecret = \"3Ts4z+jkHfyhiMKJKiUi+85bhhbKZnpbbpBQ1H30=\"; \r\n              var clientCredential = new ClientCredential(clientId, clientSecret);\r\n              var creds = ApplicationTokenProvider.LoginSilentAsync(domain, clientCredential).Result;\r\n                          \r\n              adlsClient = new DataLakeStoreAccountManagementClient(creds);\r\n              adlsFileSystemClient = new DataLakeStoreFileSystemManagementClient(creds);\r\n              adlsClient.SubscriptionId = subscriptionId;\r\n              string sourceFolderPath = @\"D:\\Agile\"; \r\n              string sourceFilePath = Path.Combine(sourceFolderPath, \"Hello.txt\");\r\n              string dataLakeStoreFolderPath = \"\/Examples\/\";\r\n              string dataLakeStoreFilePath = Path.Combine(dataLakeStoreFolderPath, \"file.txt\");\r\n              adlsFileSystemClient.FileSystem.UploadFile(adlsAccountName, sourceFilePath, dataLakeStoreFilePath,1,false,true);\r\n              Console.Read();\r\n      }\r\n      }<\/pre>\n<h2>Reference<\/h2>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/data-lake-store\/data-lake-store-get-started-net-sdk\">https:\/\/docs.microsoft.com\/en-us\/azure\/data-lake-store\/data-lake-store-get-started-net-sdk<\/a><\/p>\n<h2>Conclusion<\/h2>\n<p>Azure SDKs support the automation of various redundant operations in Azure. One of the common requirements is to upload files from on-premise or local servers to Azure Data Lake Store at regular intervals. This will allow the enterprise analytics applications to derive meaningful insights from various enterprise data available in various formats, using Data Lake Analytics, and Azure HDInsight.\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Azure Data Lake Store is an extendable store of Cloud data in Azure. You can move data to and from Azure Data Lake Store via Azure data Factory or Azure SQL Database and connect to a variety of data sources. You can script upload files from on-premise or local servers to Azure Data Lake Store using the Azure Data Lake Store .NET SDK. Ambily KK shows how easy it is to get started by setting up a script or App to move data to Azure Data Lake Store.&hellip;<\/p>\n","protected":false},"author":8305,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[137091],"tags":[],"coauthors":[11284],"class_list":["post-72171","post","type-post","status-publish","format-standard","hentry","category-azure"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/72171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/8305"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=72171"}],"version-history":[{"count":6,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/72171\/revisions"}],"predecessor-version":[{"id":72237,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/72171\/revisions\/72237"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=72171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=72171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=72171"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=72171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}