Create Storage Account And Upload, Download File Through PowerShell

In this article, we will see how to get the Storage Key and regenerate the Storage Key.

PowerShell

Azure PowerShell is a set of modules, which provide cmdlets to manage Windows Azure by Windows PowerShell. Cmdlets is used to create, deploy and manage Services through Azure platform. PowerShell is used for the Azure Management Portal, such as creating and configuring Cloud Services, virtual machines, virtual networks and Web apps etc.

Requirement

Step 1: Install PowerShell

Visit the links, given below:

Follow the steps and select PowerShell from the downloader.

Step 2: Add your Azure Account to the local PowerShell Environment.

Open the PowerShell in administrator mode and type the Script given below:

Add-AzureAccount

Add-AzureAccount

It will ask you to sign in to your Microsoft Azure Account.

Sign

After signing in, you will get all the subscription lists.

list

Step 3: Set a default Azure Subscription

We are going to use a 3f subscription from my account. Hence, please choose your subscription which you have and replace with <”SubscriptionId”> in the script.

Select-AzureSubscription -SubscriptionId <”SubscriptionId”>

Subscription

Step 4: Create new Storage Account

By using this script, we are going to create empty Storage account.

We need two parameters for this script- one is “Storage Account Name” and second is “Location”.

Replace <”StorageAccountName”> and <”Location”> with your parameter.

New-AzureStorageAccount –StorageAccountName <”StorageAccountName”> -Location <”Location”>

Storage Account

Storage Account

You can see your portal Storage Account is created.

Storage Account

Step 5: Create a New Container

Create new Container in the Storage Account. Replace Container Name with <” ContainerName”> and give the permission “off”.

New-AzureStorageContainer -Name <”ContainerName”> -Permission Off

Permission

  • Container: Provides full read access to a container and its blobs. Clients can enumerate blobs in the container through anonymous requests, but cannot enumerate the containers in the storage account.

  • Blob: Provides read access to blob data throughout a container through anonymous request,s but does not provide access to container data. Clients cannot enumerate the blobs in the container by using anonymous request.

  • Off: Restricts access to only the storage account owner.

    Permission

Step 6: Upload Blob into a Container

Upload blob with Image file into the container.

Replace <”StorageAccountName”> and <” SourceFolderPath”>

Set-AzureStorageBlobContent -Container <”ContainerName”> -File <” SourceFolderPath”>

Upload

Upload

Step 7: Download the Blob from the Container

We are downloading Blob from the container and storing it in a specific path.

Hence, first we will get reference to a list of blobs in a container by using the below script.

$blobs = Get-AzureStorageBlob -Container <”ContainerName”>

Create the destination Directory,

New-Item -Path <”DestinationFolderPath”> -ItemType Directory -Force

Directory

download

Here, you can see we have created the local directory.

Finally, here we are downloading the blob into the local destination directory.

$blobs | Get-AzureStorageBlobContent –Destination <”DestinationFolderPath”>

directory

directory
Here, you can see the file is downloaded.

Here is your Final Code,

Add-AzureAccount
Select-AzureSubscription -SubscriptionId <”SubscriptionId”> –Default
New-AzureStorageAccount –StorageAccountName <”StorageAccountName”> -Location <”Location”>
Set-AzureSubscription -CurrentStorageAccountName <”StorageAccountName”> -SubscriptionId <”SubscriptionId”>
New-AzureStorageContainer -Name <”ContainerName”> -Permission Off
Set-AzureStorageBlobContent -Container <”ContainerName”> -File <”SourceFolderPath”>
$blobs = Get-AzureStorageBlob -Container <”ContainerName”>
New-Item -Path <”DestinationFolderPath”> -ItemType Directory -Force
$blobs | Get-AzureStorageBlobContent –Destination <”DestinationFolderPath”>

How to Get & Regenerate the Azure Storage Account Key

Get Storage Account Key

Get-AzureStorageKey -StorageAccountName <"StorageAccountName">

Account Key

You can separate the key for primary and secondary.

(Get-AzureStorageKey -StorageAccountName <”StorageAccountName”>).Primary

primary

(Get-AzureStorageKey -StorageAccountName <”StorageAccountName”>).Secondary

Secondary

In the images, shown above, I have kept the first four letters of Key.

Regenerate Storage Account Key

New-AzureStorageKey -StorageAccountName <”StorageAccountName”> -KeyType “Primary”

Account Key

Here, you can see I have only changed the primary key and the secondary key is still the same.

New-AzureStorageKey -StorageAccountName <”StorageAccountName”> -KeyType “Secondary”

Account Key

Here, both the keys are changed and you can compare them.

Reference


Similar Articles