Skip to content

Instantly share code, notes, and snippets.

@gauravdave01
Last active January 14, 2023 20:21
Show Gist options
  • Select an option

  • Save gauravdave01/97046fb686cfd6ad2629855264349ce1 to your computer and use it in GitHub Desktop.

Select an option

Save gauravdave01/97046fb686cfd6ad2629855264349ce1 to your computer and use it in GitHub Desktop.
Go code to print list of azure containers and blobs residing inside. #azure #go #golang #container #blob #ListContainers #ListBlobs #listing https://medium.com/@gauravdave01/working-with-azure-storage-using-go-dc5a8a668bc3
package main
import (
"context"
"fmt"
"log"
"net/url"
"os"
"github.com/Azure/azure-storage-blob-go/2016-05-31/azblob"
)
func handleErrors(err error) {
if err != nil {
if serr, ok := err.(azblob.StorageError); ok { // This error is a Service-specific
switch serr.ServiceCode() { // Compare serviceCode to ServiceCodeXxx constants
case azblob.ServiceCodeContainerAlreadyExists:
fmt.Println("Received 409. Container already exists")
return
}
}
log.Fatal(err)
}
}
func main() {
fmt.Printf("Azure Container: Blob Listing.\n")
// From the Azure portal, get your storage account name and key and set environment variables.
accountName, accountKey := os.Getenv("AZURE_STORAGE_ACCOUNT"), os.Getenv("AZURE_STORAGE_ACCESS_KEY")
if len(accountName) == 0 || len(accountKey) == 0 {
log.Fatal("Either the AZURE_STORAGE_ACCOUNT or AZURE_STORAGE_ACCESS_KEY environment variable is not set")
}
// Create a default request pipeline using your storage account name and account key.
credential := azblob.NewSharedKeyCredential(accountName, accountKey)
p := azblob.NewPipeline(credential, azblob.PipelineOptions{})
// From the Azure portal, get your storage account blob service URL endpoint.
azureURL, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", accountName))
// Create a ContainerURL object that wraps the container URL and a request
// pipeline to make requests.
serviceURL := azblob.NewServiceURL(*azureURL, p)
ctx := context.Background() // This uses a never-expiring context
// List the container(s)
for containerMarker := (azblob.Marker{}); containerMarker.NotDone(); {
listContainer, _ := serviceURL.ListContainers(ctx, containerMarker, azblob.ListContainersOptions{})
for _, containerObject := range listContainer.Containers {
containerName := containerObject.Name
containerURL, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net/%s", accountName, containerName))
// Container Name
fmt.Println("Container: ", containerName)
containerServiceURL := azblob.NewContainerURL(*containerURL, p)
// List the blobs in the container
for blobMarker := (azblob.Marker{}); blobMarker.NotDone(); {
// Get a result segment starting with the blob indicated by the current Marker.
listBlob, err := containerServiceURL.ListBlobs(ctx, blobMarker, azblob.ListBlobsOptions{})
handleErrors(err)
// ListBlobs returns the start of the next segment; you MUST use this to get
// the next segment (after processing the current result segment).
blobMarker = listBlob.NextMarker
// Process the blobs returned in this result segment (if the segment is empty, the loop body won't execute)
for _, blobInfo := range listBlob.Blobs.Blob {
// Blob Name
fmt.Print("Blob name: " + blobInfo.Name + "\n")
}
}
}
containerMarker = listContainer.NextMarker
}
}
/*
Run below command in your terminal:
export AZURE_STORAGE_ACCOUNT="azure-account-name"
export AZURE_STORAGE_ACCESS_KEY="B60wlbowEkr2v0mMKUFgSlAmUdWJgAVPr03EZVprpSdVvf77Mf3PFe6tpYU1xPA1f2BB9qrgUcwWgKeLdnkokT=="
Install Go Azure SDK:
go get github.com/Azure/azure-storage-blob-go/2016-05-31/azblob
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment