<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/azurestor-an-r-package-for-working-with-azure-storage/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.revolutionanalytics.com/2018/12/azurestor.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><em>by Hong Ooi, senior data scientist, Microsoft Azure</em></p>
<p>A few weeks ago, I introduced the <a rel="nofollow" href="https://blog.revolutionanalytics.com/2018/11/azurer-intro.html" target="_blank">AzureR</a> family of packages for working with Azure in R. Since then, I’ve also written articles on how to use <a rel="nofollow" href="https://blog.revolutionanalytics.com/2018/11/azurermr-azure-resource-manager.html" target="_blank">AzureRMR</a> to interact with Azure Resource Manager, how to use <a rel="nofollow" href="https://blog.revolutionanalytics.com/2018/12/azurevm.html" target="_blank">AzureVM</a> to manage virtual machines, and how to use <a rel="nofollow" href="https://blog.revolutionanalytics.com/2018/12/azurecontainers.html" target="_blank">AzureContainers</a> to deploy R functions with Azure Kubernetes Service. This article is the next in the series, and covers AzureStor: an interface to <a rel="nofollow" href="https://docs.microsoft.com/azure/storage/?WT.mc_id=Revolutions-blog-davidsmi" target="_blank">Azure storage</a>.</p>
<div>
<h2>The Resource Manager interface: creating and deleting storage accounts</h2>
<p><a rel="nofollow" href="https://github.com/cloudyr/AzureStor" target="_blank">AzureStor</a> implements an interface to Azure Resource Manager, which you can use manage storage accounts: creating them, retrieving them, deleting them, and so forth. This is done via the appropriate methods of the <code>az_resource_group</code> class. For example, the following code shows how you might create a new storage account from scratch.</p>
<div>
<pre><code><span>library</span>(AzureStor)

<span># get the resource group for the storage account</span>
rg &lt;- AzureRMR::az_rm$
    <span>new</span>(<span>tenant=</span><span>&quot;{tenant_id}&quot;</span>, <span>app=</span><span>&quot;{app_id}&quot;</span>, <span>password=</span><span>&quot;{password}&quot;</span>)$
    <span>get_subscription</span>(<span>&quot;{subscription_id}&quot;</span>)$
    <span>get_resource_group</span>(<span>&quot;myresourcegroup&quot;</span>)

<span># create the storage account</span>
<span># by default, this will be in the resource group's region</span>
rg$<span>create_storage_account</span>(<span>&quot;mynewstorage&quot;</span>)</code></pre>
</div>
<p>Without any options, this will create a storage account with the following parameters:</p>
<ul>
<li>General purpose account (all storage types supported)</li>
<li>Locally redundant storage (LRS) replication</li>
<li>Hot access tier (for blob storage)</li>
<li>HTTPS connection required for access</li>
</ul>
<p>You can change these by setting the arguments to <code>create_storage_account()</code>. For example, to create an account with geo-redundant storage replication and the default blob access tier set to “cool”:</p>
</div>
<div>
<pre><code>rg$<span>create_storage_account</span>(<span>&quot;myotherstorage&quot;</span>,
    <span>replication=</span><span>&quot;Standard_GRS&quot;</span>,
    <span>access_tier=</span><span>&quot;cool&quot;</span>)</code></pre>
</div>
<p>To retrieve an existing storage account, use the <code>get_storage_account()</code> method. Only the storage account name is required.</p>
<div>
<pre><code><span># retrieve one of the accounts created above</span>
stor2 &lt;- rg$<span>get_storage_account</span>(<span>&quot;myotherstorage&quot;</span>)</code></pre>
</div>
<p>Finally, to delete a storage account, you simply call its <code>delete()</code> method. Alternatively, you can call the <code>delete_storage_account()</code> method of the <code>az_resource_group</code> class, which will do the same thing. In both cases, AzureStor will prompt you for confirmation that you really want to delete the storage account.</p>
<div>
<pre><code>rg$<span>delete_storage_account</span>(<span>&quot;mynewstorage&quot;</span>)
stor2$<span>delete</span>() <span># if you have the storage account object</span></code></pre>
</div>
<div>
<h2>The client interface: working with storage</h2>
<div>
<h3>Storage endpoints</h3>
<p>Perhaps the more relevant part of AzureStor for most users is its client interface to storage. With this, you can upload and download files and blobs, create containers and shares, list files, and so on. Unlike the ARM interface, the client interface uses S3 classes. This is for a couple of reasons: it is more familiar to most R users, and it is consistent with most other data manipulation packages in R, in particular the <a rel="nofollow" href="https://tidyverse.org/" target="_blank">tidyverse</a>.</p>
<p>The starting point for client access is the <code>storage_endpoint</code> object, which stores information about the endpoint of a storage account: the URL that you use to access storage, along with any authentication information needed. The easiest way to obtain an endpoint object is via the storage account resource object’s <code>get_blob_endpoint()</code> and <code>get_file_endpoint()</code> methods:</p>
<div>
<pre><code><span># get the storage account object</span>
stor &lt;- AzureRMR::az_rm$
    <span>new</span>(<span>tenant=</span><span>&quot;{tenant_id}&quot;</span>, <span>app=</span><span>&quot;{app_id}&quot;</span>, <span>password=</span><span>&quot;{password}&quot;</span>)$
    <span>get_subscription</span>(<span>&quot;{subscription_id}&quot;</span>)$
    <span>get_resource_group</span>(<span>&quot;myresourcegroup&quot;</span>)$
    <span>get_storage_account</span>(<span>&quot;mynewstorage&quot;</span>)

stor$<span>get_blob_endpoint</span>()
<span># Azure blob storage endpoint</span>
<span># URL: https://mynewstorage.blob.core.windows.net/</span>
<span># Access key: </span></code></pre></div></div></div></div>