Skip to content

PowerShell and Python Script Uploads #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
35 changes: 35 additions & 0 deletions Storage/Powershell/BulkRehydrateBlobsAllContainers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This script loops through each container in an Azure storage account and rehydrates any blob in the Archive tier to the Hot tier

#DISCLAIMER
#The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, owners of this repository or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including,
#without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages

# Initialize these variables with your values.
$rgName = ""
$accountName = ""

$ctx = (Get-AzStorageAccount -ResourceGroupName $rgName -Name $accountName).Context

$containers = Get-AzStorageContainer -Context $ctx

foreach ($container in $containers) {
$containerName = $container.Name
$blobCount = 0
$Token = $Null
$MaxReturn = 5000

do {
$Blobs = Get-AzStorageBlob -Context $ctx -Container $containerName -MaxCount $MaxReturn -ContinuationToken $Token
if ($Blobs -eq $Null) { break }
if ($Blobs.GetType().Name -eq "AzureStorageBlob") {
$Token = $Null
} else {
$Token = $Blobs[$Blobs.Count - 1].ContinuationToken
}
$Blobs | ForEach-Object {
if ($_.BlobType -eq "BlockBlob" -and $_.AccessTier -eq "Archive") {
$_.BlobClient.SetAccessTier("Hot", $null, "Standard")
}
}
} while ($Token -ne $Null)
}
50 changes: 50 additions & 0 deletions Storage/Powershell/CountArchived+RehydratingBlobsAllContainers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# This script loops through each container in an Azure storage account and outputs the count of blobs in the arcive tier and how many are currently being rehydrated in each container and in the entire account

#DISCLAIMER
#The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, owners of this repository or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including,
#without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages

# Initialize these variables with your values.
$rgName = ""
$accountName = ""

$ctx = (Get-AzStorageAccount -ResourceGroupName $rgName -Name $accountName).Context

$containers = Get-AzStorageContainer -Context $ctx

$totalArchiveBlobCount = 0
$totalRehydratingBlobCount = 0

Write-Output `n

foreach ($container in $containers) {
$containerName = $container.Name
$blobCount = 0
$rehydratingBlobCount = 0
$Token = $Null
$MaxReturn = 5000

do {
$Blobs = Get-AzStorageBlob -Context $ctx -Container $containerName -MaxCount $MaxReturn -ContinuationToken $Token
if ($Blobs -eq $Null) { break }
if ($Blobs.GetType().Name -eq "AzureStorageBlob") {
$Token = $Null
} else {
$Token = $Blobs[$Blobs.Count - 1].ContinuationToken
}
$Blobs | ForEach-Object {
$Blobs.ICloudBlob.FetchAttributes()
if ($_.BlobType -eq "BlockBlob" -and $_.AccessTier -eq "Archive") {
$blobCount++
$totalArchiveBlobCount++
}if ($_.BlobType -eq "BlockBlob" -and $_.ICloudBlob.Properties.RehydrationStatus -ne $null) {
$rehydratingBlobCount++
$totalRehydratingBlobCount++
}
}
} while ($Token -ne $Null)
Write-Output "Container: $containerName | Archived Blobs: $blobCount | Rehydrating Blobs: $rehydratingBlobCount"
}

Write-Output "Total Archived Blobs: $totalArchiveBlobCount"
Write-Output "Total Archived Blobs Currently Being Rehydrated: $totalRehydratingBlobCount"
33 changes: 33 additions & 0 deletions Storage/Powershell/DeletePreviousBlobVersions.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#This script uses an SAS token to authenticate against an Azure storage account and deletes all previous blob versions inside of a designated container

#DISCLAIMER
#The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, owners of this repository or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including,
#without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages

#Docs
#https://learn.microsoft.com/en-us/powershell/module/az.storage/remove-azstorageblob?view=azps-13.3.0&tryIt=true&source=docs#example-4-remove-a-single-blob-version
#https://learn.microsoft.com/en-us/rest/api/storageservices/create-service-sas#permissions-for-a-directory-container-or-blob

#Needs account level SAS for x permission to delete previous versions

# Parameters – replace these with your actual values
$storageAccountName = ""
$containerName = ""
$sasToken = ""

# Create a storage context using the SAS token
$context = New-AzStorageContext -StorageAccountName $storageAccountName -SasToken $sasToken

# Retrieve all blobs, including their versions
$blobs = Get-AzStorageBlob -Container $containerName -Context $context -IncludeVersion

foreach ($blob in $blobs) {
# Check if the blob object indicates whether it's the current version.
if ($blob.IsLatestVersion -eq $true) {
Write-Host "Skipping current version of blob: $($blob.Name)"
}
else {
Write-Host "Deleting blob version: $($blob.Name) | Version ID: $($blob.VersionId)"
Remove-AzStorageBlob -Container $containerName -Blob $blob.Name -Context $context -VersionId $blob.VersionId
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#This script authenticates against an Azure storage account using an Entra token uses the REST API to permanently deletes all soft delete blobs in the archive tier in any container

# ====================================================================================
# DISABLE SOFT DELETE FEATURE ON STORAGE ACCOUNT BEFORE RUNNING THIS SCRIPT
# You can reenable Soft Delete featurs after running this script, if needed.
# By default, the script will only list the total count of soft delete blobs in the archive tier.
# Paremeter $PERMANENT_DELETE needs to be changed from 'LIST' to 'DELETE' to permanently delete blobs.
# ====================================================================================
# DISCLAMER : please note that this script is to be considered as a sample and is provided as is with no warranties express or implied, even more considering this is about deleting data.
# This script should be tested in a dev environment before using in Production.
# You can use or change this script at you own risk.
# ====================================================================================

#Authentication
Connect-AzAccount

# Define the storage account and resource group
$storageAccountName = ""
$resourceGroup = ""
$PERMANENT_DELETE = "LIST"

# Get the storage account context
$context = (Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName).Context

# Get the access token
$token = Get-AzAccessToken -ResourceUrl "https://storage.azure.com/"

# List all containers in the storage account
$containers = Get-AzStorageContainer -Context $context

# Initialize counters
$blobCount = 0
$undeletedCount = 0
$deletedCount = 0

foreach ($container in $containers) {
# List all blobs in the container, including deleted ones
$blobs = Get-AzStorageBlob -Container $container.Name -Context $context -IncludeDeleted

foreach ($blob in $blobs) {
# Apply the condition to filter blobs
if ($PERMANENT_DELETE -eq "DELETE" -and $blob.BlobType -eq "BlockBlob" -and $blob.AccessTier -eq "Archive" -and $blob.IsDeleted -eq $true) {
# Construct the URI for the undelete operation
$undeleteUri = "https://" + $blob.BlobClient.Uri.Host + $blob.BlobClient.Uri.AbsolutePath + "?comp=undelete"

# Set the headers with the authorization token
$headers = @{
'Authorization' = "Bearer $($token.Token)"
'x-ms-version' = '2021-12-02' # Specify the latest storage API version
'x-ms-date' = (Get-Date).ToUniversalTime().ToString("R")
'Content-Length' = '0' # Required for PUT operations without a body
}

# Try to undelete the blob
try {
$res = Invoke-RestMethod -Method "Put" -Uri $undeleteUri -Headers $headers
$undeletedCount++
} catch {
Write-Warning -Message "Failed to undelete blob in container $($container.Name): $($_.Exception.Message)" -ErrorAction Stop
}

# Construct the URI for the delete operation
$deleteUri = "https://" + $blob.BlobClient.Uri.Host + $blob.BlobClient.Uri.AbsolutePath

# Try to delete the blob
try {
$res = Invoke-RestMethod -Method "Delete" -Uri $deleteUri -Headers $headers
$deletedCount++
} catch {
Write-Warning -Message "Failed to delete blob in container $($container.Name): $($_.Exception.Message)" -ErrorAction Stop
}
}if ($PERMANENT_DELETE -eq "LIST" -and $blob.BlobType -eq "BlockBlob" -and $blob.AccessTier -eq "Archive" -and $blob.IsDeleted -eq $true){
$blobCount++
}
}
}

if ($PERMANENT_DELETE -eq "LIST"){
Write-Output "Soft deleted blobs in archive tier: $blobCount"
} if ($PERMANENT_DELETE -eq "DELETE"){
Write-Output "Total undeleted blobs: $undeletedCount"
Write-Output "Total deleted blobs: $deletedCount"
}
39 changes: 39 additions & 0 deletions Storage/Powershell/UndeleteBlob_RESTAPI.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#This script authenticates against an Azure storage account using Entra token and uses the REST API to perform an undelete operation against a soft delete blob

#DISCLAIMER
#The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, owners of this repository or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including,
#without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages

#Authorization
Connect-AzAccount

# Define the storage account, container, and blob name
$storageAccountName = ""
$containerName = ""
$blobName = ""
$resourceGroup = ""

# Get the blob client
$blob = Get-AzStorageBlob -Container $containerName -Blob $blobName -Context (Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName).Context -IncludeDeleted

# Get the access token
$token = Get-AzAccessToken -ResourceUrl "https://storage.azure.com/"

# Construct the URI for the undelete operation
$uri = "https://" + $blob.BlobClient.Uri.Host + $blob.BlobClient.Uri.AbsolutePath + "?comp=undelete"

# Set the headers with the authorization token
$headers = @{
'Authorization' = "Bearer $($token.Token)"
'x-ms-version' = '2021-12-02' # Specify the latest storage API version
'x-ms-date' = (Get-Date).ToUniversalTime().ToString("R")
'Content-Length' = '0' # Required for PUT operations without a body
}

# Try to undelete the blob
try {
$res = Invoke-RestMethod -Method "Put" -Uri $uri -Headers $headers
Write-Host "Blob undeleted successfully."
} catch {
Write-Warning -Message "$($_.Exception.Message)" -ErrorAction Stop
}
30 changes: 30 additions & 0 deletions Storage/Python/UploadTestBlobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This script authenticates against an Azure storage account using an access key connection string and creates a designated amount of test blobs (Default 10000) in a designated container

#DISCLAIMER
#The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, owners of this repository or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including,
#without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import os

# Input your access key connection string and container name
connection_string = ""
container_name = ""

# Initialize the BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container_name)

# Create the container if it doesn't exist
try:
container_client.create_container()
except Exception as e:
print(f"Container already exists: {e}")

# Upload blobs
for i in range(10000): # Adjust the range for the number of blobs you need
blob_name = f"test_blob_{i}.txt"
blob_client = container_client.get_blob_client(blob_name)
blob_content = f"This is test blob number {i}"
blob_client.upload_blob(blob_content)
print(f"Uploaded {blob_name}")