UPDATE:
Note that my issue was a bit different: i needed to rename the files in a blob when downloading them with AzCopy because they had no extension. Probably this script can be reversed to achieve your goal!
I could not find a way to make that work using azcopy, so I realized a powershell script that connects to my storage, downloads the items inside a 'Download' folder (located in the same script location) and renames them by adding the extension.
Note that this scenario was easy to handle as I knew that every file only required to add the .pdf
extension.
I will try to improve this script and include a better solution to handle different data types if I can figure it out!
1-You must download Powershell 7.*
2-Install the Azure Modules
for Powershell
by changing the ExecutionPolicy
for CurrentUser
then install the module (it may take a while, you can add the -Verbose
parameter to have more info:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
3-Customize this script and save it with .ps1
extension:
## Input Parameters
param ($subscriptionId="", ## Your subscriptionId here, if not specified "Connect-AzAccount" will use the first available subscription.
$resourceGroupName="your-resource-name",
$storageAccName="your-storage-account-name",
$storageContainer="your-storage-container",
$itemsCount=-1) ## Amount of items to download. If -1 (default value) or 0 it will download everything inside the container.
$downloadPath=".\Download"
$downloadLocation="Download"
## Connect to Azure Account and set the Context
if ($subscriptionId -ne "")
{
Connect-AzAccount -Subscription $subscriptionId
}
else
{
Connect-AzAccount
}
Function BlobCycleAndRename
{
param([array]$Contents)
foreach($content in $Contents)
{
$itemName=$content.Name
## Download the blob content
Get-AzStorageBlobContent -Container $container.Name -Context $ctx -Blob $itemName -Destination $destination -Force
## Rename the items, if an item with the same name already exists, it is removed first.
if (Test-Path ($destination+"\"+($itemName+".your-extension-here")))
{
Remove-Item -Path ($destination+"\"+($itemName+".your-extension-here"))
}
Rename-Item -Path ($destination+"\"+$itemName) -NewName ($itemName+".your-extension-here") -Force
}
}
## Function to download all blob contents
Function DownloadBlobContents
{
Write-Host -ForegroundColor Green "Download blob contents from storage container.."
## Get the storage account
$storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName
## Get the storage account context
$ctx=$storageAcc.Context
## Get the containers
$container=Get-AzStorageContainer -Context $ctx -Name $storageContainer
## check if folder exists
$folderPath=$downloadPath+"\"+$container.Name
$destination=$downloadLocation+"\"+$container.Name
$folderExists=Test-Path -Path $folderPath
if($folderExists)
{
Write-Host -ForegroundColor Magenta $container.Name "- folder exists"
## Get the blob contents from the container
$blobContent
if ($itemsCount -le 0)
{
$blobContents=Get-AzStorageBlob -Container $container.Name -Context $ctx
}
else
{
$blobContents=Get-AzStorageBlob -Container $container.Name -Context $ctx -MaxCount $itemsCount
}
## Download and rename blobs
BlobCycleAndRename -Contents $blobContents
}
else
{
Write-Host -ForegroundColor Magenta $container.Name "- folder does not exist"
## Create the new folder
New-Item -ItemType Directory -Path $folderPath
## Get the blob contents from the container
$blobContents
if ($itemsCount -le 0)
{
$blobContents=Get-AzStorageBlob -Container $container.Name -Context $ctx
}
else
{
$blobContents=Get-AzStorageBlob -Container $container.Name -Context $ctx -MaxCount $itemsCount
}
## Download and rename blobs
BlobCycleAndRename -Contents $blobContents
}
}
DownloadBlobContents
## Disconnect from Azure Account
Disconnect-AzAccount
Some credits go to: https://www.c-sharpcorner.com/blogs/how-to-download-blob-contents-from-azure-storage-account-using-powershell
PREVIOUS POST
Note that my issue is a bit different (it's "reversed" somehow): I need to set a file extension when downloading files from AzCopy, because they have been uploaded with no extension.
I am answering to @Kolky here, but I have not enough rep to directly add comments!
As pointed out, the MIMETypeMapping page seems to not exist anymore, however I was still able to find something useful.
You have to set the AZCOPY_CONTENT_TYPE_MAP Environment variable and provide it a json config file where you can provide the desired mapping type.
"MIMETypeMapping": {
".323": "text/h323",
".aaf": "application/octet-stream",
".aca": "application/octet-stream",
".accdb": "application/msaccess",
}
You can access the configured AzCopy Environment variables by using the command AzCopy env (or /path/AzCopy.exe).
I am still trying to figure out if I am doing something wrong, as this seems to not be working, but this can still be helpful to someone! :)
A couple of links for reference:
https://github.com/Azure/azure-storage-azcopy/wiki/Custom-mime-mapping
https://learn.microsoft.com/it-it/azure/storage/common/storage-ref-azcopy-configuration-settings#AZCOPY_CONTENT_TYPE_MAP
https://github.com/Azure/azure-storage-azcopy/issues/136