<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Python &#8211; Prologika</title>
	<atom:link href="https://prologika.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>https://prologika.com</link>
	<description>Business Intelligence Consulting and Training in Atlanta</description>
	<lastBuildDate>Thu, 13 Aug 2020 17:50:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.3</generator>
	<item>
		<title>Uploading Files to ADLS Gen2 with Python and Service Principal Authentication</title>
		<link>https://prologika.com/uploading-files-to-adls-gen2-with-python-and-service-principal-authentication/</link>
					<comments>https://prologika.com/uploading-files-to-adls-gen2-with-python-and-service-principal-authentication/#respond</comments>
		
		<dc:creator><![CDATA[Prologika - Teo Lachev]]></dc:creator>
		<pubDate>Thu, 13 Aug 2020 17:50:52 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Azure Data Lake]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://prologika.com/?p=7369</guid>

					<description><![CDATA[I had an integration challenge recently. I set up Azure Data Lake Storage for a client and one of their customers want to use Python to automate the file upload [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>I had an integration challenge recently. I set up Azure Data Lake Storage for a client and one of their customers want to use Python to automate the file upload from MacOS (yep, it must be Mac). They found the command line <a href="https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10">azcopy</a> not to be automatable enough. So, I whipped the following Python code out. I configured service principal authentication to restrict access to a specific blob container instead of using Shared Access Policies which require PowerShell configuration with Gen 2. The comments below should be sufficient to understand the code.</p>
<div>
<div><span style="color: green; font-family: Consolas; font-size: 10pt;">###install dependencies</span></div>
<div><span style="color: green; font-family: Consolas; font-size: 10pt;"># install Azure CLI https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest</span></div>
<div><span style="color: green; font-family: Consolas; font-size: 10pt;">#pip install azure-identity</span></div>
<div><span style="color: green; font-family: Consolas; font-size: 10pt;">#pip install azure-storage-blob</span></div>
<div><span style="color: green; font-family: Consolas; font-size: 10pt;"># upgrade or install pywin32 to build 282 to avoid error &#8220;DLL load failed: %1 is not a valid Win32 application&#8221; while importing azure.identity</span></div>
<div><span style="color: green; font-family: Consolas; font-size: 10pt;"># pip install pywin32 –upgrade</span></div>
</div>
<p style="background: white;"><span style="color: green; font-family: Consolas; font-size: 10pt;"># IMPORTANT! set the four environment (bash) variables as per https://docs.microsoft.com/en-us/azure/developer/python/configure-local-development-environment?tabs=cmd</span></p>
<p style="background: white;"><span style="color: green; font-family: Consolas; font-size: 10pt;"># Note that AZURE_SUBSCRIPTION_ID is enclosed with double quotes while the rest are not<br />
</span></p>
<p style="background: white;"><span style="color: #af00db; font-family: Consolas; font-size: 10pt;">import<span style="color: black;"> os<br />
</span></span></p>
<p style="background: white;"><span style="color: #af00db; font-family: Consolas; font-size: 10pt;">from<span style="color: black;"> azure.storage.blob <span style="color: #af00db;">import<span style="color: black;"> BlobClient</span></span></span></span></p>
<p style="background: white;"><span style="color: #af00db; font-family: Consolas; font-size: 10pt;">from<span style="color: black;"> azure.identity <span style="color: #af00db;">import<span style="color: black;"> DefaultAzureCredential<br />
</span></span></span></span></p>
<p style="background: white;"><span style="color: black; font-family: Consolas; font-size: 10pt;">storage_url = <span style="color: #a31515;">&#8220;https://mmadls01.blob.core.windows.net&#8221; <span style="color: green;"># mmadls01 is the storage account name <span style="color: black;"><br />
</span></span></span></span></p>
<p style="background: white;"><span style="color: black; font-family: Consolas; font-size: 10pt;">credential = DefaultAzureCredential() <span style="color: green;"># This will look up env variables to determine the auth mechanism. In this case, it will use service principal authentication <span style="color: black;"><br />
</span></span></span></p>
<p style="background: white;"><span style="color: green; font-family: Consolas; font-size: 10pt;"># Create the client object using the storage URL and the credential<span style="color: black;"><br />
</span></span></p>
<p style="background: white;"><span style="color: black; font-family: Consolas; font-size: 10pt;">blob_client = BlobClient(storage_url, <span style="color: #001080;">container_name<span style="color: black;">=<span style="color: #a31515;">&#8220;maintenance/in&#8221;<span style="color: black;">, <span style="color: #001080;">blob_name<span style="color: black;">=<span style="color: #a31515;">&#8220;sample-blob.txt&#8221;<span style="color: black;">, <span style="color: #001080;">credential<span style="color: black;">=credential) <span style="color: green;"># &#8220;maintenance&#8221; is the container, &#8220;in&#8221; is a folder in that container<span style="color: black;"><br />
</span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p style="background: white;"><span style="color: green; font-family: Consolas; font-size: 10pt;"># Open a local file and upload its contents to Blob Storage<span style="color: black;"><br />
</span></span></p>
<p style="background: white;"><span style="color: #af00db; font-family: Consolas; font-size: 10pt;">with<span style="color: black;"> <span style="color: #795e26;">open<span style="color: black;">(<span style="color: #a31515;">&#8220;./sample-source.txt&#8221;<span style="color: black;">, <span style="color: #a31515;">&#8220;rb&#8221;<span style="color: black;">) <span style="color: #af00db;">as<span style="color: black;"> data:<br />
</span></span></span></span></span></span></span></span></span></span></p>
<p style="background: white;"><span style="color: black; font-family: Consolas; font-size: 10pt;">    blob_client.upload_blob(data)<br />
</span></p>
<p style="background: white;"><img decoding="async" src="https://prologika.com/wp-content/uploads/2020/08/081320_1739_UploadingFi1.png" alt="" /><span style="color: black; font-family: Consolas; font-size: 10pt;"><br />
</span></p>
<p style="background: white;">
]]></content:encoded>
					
					<wfw:commentRss>https://prologika.com/uploading-files-to-adls-gen2-with-python-and-service-principal-authentication/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
