How to list Azure Storage Containers and Blobs
Asked Answered
T

5

14

I'm new to Azure Storage and I think I may be misunderstanding a few of the concepts.

I'd like to list all my Storage Containers and Blobs using PowerShell.

I can list all my Storage Accounts using the following code:

Get-AzureStorageAccount | Select StorageAccountName, GeoPrimaryLocation

Each of the Storage Accounts has a Container. How do I get it? I don't see a command that lists Containers. There is a Get-AzureStorageContainer command, but it doesn't take a Storage Account as input.

What am I missing?

-- Edit --

I see that I can do the following:

$context = New-AzureStorageContext -StorageAccountName myStorageAccount -StorageAccountKey xxx
Get-AzureStorageContainer -Context $context
Get-AzureStorageBlob -Context $context -Container myContainer

Why is the context required?

Tyishatyke answered 28/10, 2015 at 1:58 Comment(3)
msdn.microsoft.com/en-us/library/dn806407.aspx - from what I read, context is not required. What do you get if you just enter the get commands?Does
I get an error if I omit the context. Get-AzureStorageContainer : BadRequest: The name is not a valid storage account name. Storage account names must be between 3 and 24 characters in length and use numbers and lower-case letters only. At C:\Users\MatthewFitzmaurice\Documents\Scripts\Move VHDs.ps1:7 char:1 + Get-AzureStorageContainer -Name vhds + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Get-AzureStorageContainer], CloudException + FullyQualifiedErrorId : CloudException,Microsoft.WindowsAzure.Commands.Storage.Blob.Cmdlet.GetAzureStorageContainerCTyishatyke
Looks like the context is similar to credentials.Does
H
15

Not sure if this is what you want, but I am able to list containers using New-AzureStorageContext and Get-AzureStorageContainers.

$ctx = New-AzureStorageContext -StorageAccountName <name> -StorageAccountKey <key>

Get-AzureStorageContainer -Context $ctx
Hartle answered 28/10, 2015 at 2:19 Comment(0)
C
15

With the new Az module, you need to do the following

Import-Module Az
$azStorageAccountName = "" # Name of your storage account 
$azStorageAccountKey = "" # Access key for your storage account
$azContainerName = "" # Container name to list your blobs
$azResourceGroupName = "" # Resource group name where storage account lives

$connectionContext = (Get-AzStorageAccount -ResourceGroupName $azResourceGroupName -AccountName $azStorageAccountName).Context
# Get a list of containers in a storage account
Get-AzStorageContainer -Name $azContainerName -Context $connectionContext | Select Name
# Get a list of blobs in a container 
Get-AzStorageBlob -Container $azContainerName -Context $connectionContext | Select Name
Counterproductive answered 3/3, 2020 at 0:19 Comment(2)
This is perfect. Just a quick change to the command to list all containers in a storage account Get-AzStorageContainer -Context $connectionContextLevitation
Shouldn't it be Get-AzStorageContainer -Name $azContainerName -Context $connectionContext | Select Namethat is $azContainerName rather than $azStorageAccountName?Laporte
B
3

List all containers

Get-AzureStorageContainer

List all blobs in a container.

Get-AzureStorageBlob -Container $ContainerName

There is also a complete Getting Started Guide for PowerShell and Azure Storage which you can find here. Azure Storage PowerShell Getting Started

Borecole answered 28/10, 2015 at 5:34 Comment(0)
W
2

This is how I did it within ARM:

function Get-StorageContainer
{
    param
    (
        [string]$StorageAccountName
    )

    $StorageAccounts = Get-AzureRmStorageAccount

    $selectedStorageAccount = $StorageAccounts | where-object{$_.StorageAccountName -eq $StorageAccountName}
    $key1 = (Get-AzureRmStorageAccountKey -ResourceGroupName $selectedStorageAccount.ResourceGroupName -name $selectedStorageAccount.StorageAccountName)[0].value

    $storageContext = New-AzureStorageContext -StorageAccountName $selectedStorageAccount.StorageAccountName -StorageAccountKey $key1
    $storageContainer = Get-AzureStorageContainer -Context $storageContext
    $storageContainer
}

Get-StorageContainer -StorageAccountName storageaccount
Weaken answered 28/6, 2016 at 22:5 Comment(1)
What does New-AzureStorageContext do in this example? I just can't figure it out and it is not in the main documentation of Azure storage account. Microsoft manages to create random complexity everywhere.Underset
A
0
Connect-AzAccount

Write-Host "Gathering container Public access level information...`n"
[System.Collections.ArrayList]$saUsage = New-Object -TypeName System.Collections.ArrayList

$subscriptions = Get-AzSubscription <# -subscriptionid "6f3ecdd5-6sff85-4sss9b-a9ec-0a7sds2ad6" #>

foreach ($subscription in $subscriptions)  {
    Write-Host $subscription.Id
    Set-AzContext -SubscriptionObject $subscription

    $storageAccounts = Get-AzStorageAccount

    foreach ($storageAccount in $storageAccounts) {
        #list containers
        $storages = Get-AzStorageAccount -ResourceGroupName $storageAccount.ResourceGroupName -StorageAccountName $storageAccount.StorageAccountName

        $ctx = $storageAccount.Context

        $containers = Get-AzStorageContainer -Context $ctx

        foreach($container in $containers){
            $StorageAccountDetails = [ordered]@{
                SubscriptionName = $subscription.Name
                StorageAccount = $storageAccount.StorageAccountName
                ContainerName = $container.Name
                ContainerPublicAccess = $container.PublicAccess
            }
            $saUsage.add((New-Object psobject -Property $StorageAccountDetails)) | Out-Null  
            Write-Host "object written to list"
        }
    }
}
$saUsage | Export-Csv -Path C:\Containers_Access_Level.csv -NoTypeInformation
Astonied answered 7/2, 2023 at 9:31 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Cohe

© 2022 - 2024 — McMap. All rights reserved.