Skip to content

Instantly share code, notes, and snippets.

@duffney
Created April 25, 2022 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duffney/ab582a53b2bf72f48dee6f942abfd860 to your computer and use it in GitHub Desktop.
Save duffney/ab582a53b2bf72f48dee6f942abfd860 to your computer and use it in GitHub Desktop.
Azure Blob Cli using the flags package
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)
func getClient(blob, container string) azblob.BlockBlobClient {
storageAccount := os.Getenv("AZURE_STORAGE_ACCOUNT_NAME")
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatal("Invalid credentials with error:" + err.Error())
}
blobUrl := fmt.Sprintf("https://%s.blob.core.windows.net/%s/%s", storageAccount, container, blob)
client, err := azblob.NewBlockBlobClient(blobUrl, cred, nil)
if err != nil {
log.Fatal("Unable to create blob client")
}
return client
}
func upload(path, container string) {
content, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println(err)
}
_, blobName := filepath.Split(path)
client := getClient(blobName, container)
_, err = client.UploadBufferToBlockBlob(context.TODO(), content, azblob.HighLevelUploadToBlockBlobOption{})
if err != nil {
log.Fatal("Failure to upload to blob:", err)
}
}
func download(name, container, destination string) {
client := getClient(name, container)
destFile, err := os.Create(destination)
if err != nil {
panic(err)
}
_ = client.DownloadBlobToFile(context.TODO(), 0, 0, destFile, azblob.HighLevelDownloadFromBlobOptions{})
}
func list(container string) {
storageAccount := os.Getenv("AZURE_STORAGE_ACCOUNT_NAME")
containerUrl := fmt.Sprintf("https://%s.blob.core.windows.net/%s/", storageAccount, container)
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatal("Invalid credentials with error:" + err.Error())
}
client, err := azblob.NewContainerClient(containerUrl, cred, nil)
if err != nil {
panic(err)
}
pager := client.ListBlobsFlat(nil)
for pager.NextPage(context.TODO()) {
resp := pager.PageResponse()
for _, v := range resp.ContainerListBlobFlatSegmentResult.Segment.BlobItems {
fmt.Println(*v.Name)
//TODO: add results to a var and return the variable
}
}
}
func delete(name, container string) (azblob.BlobDeleteResponse, error) {
client := getClient(name, container)
return client.Delete(context.TODO(), nil)
}
func main() {
uploadCmd := flag.NewFlagSet("upload", flag.ExitOnError)
uploadPath := uploadCmd.String("path", "", "Path to file")
uploadContainer := uploadCmd.String("container", "", "Name of Azure storage container")
downloadCmd := flag.NewFlagSet("download", flag.ExitOnError)
downloadBlobName := downloadCmd.String("name", "", "name of azure blob to download")
downloadContainerName := downloadCmd.String("container", "", "name of azure storage container")
downloadFile := downloadCmd.String("file", "", "destination path for file")
listCmd := flag.NewFlagSet("list", flag.ContinueOnError)
listContainerName := listCmd.String("container", "", "name of azure storage container")
deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
deleteBlobName := deleteCmd.String("name", "", "name of azure blob to delete")
deleteContainerName := deleteCmd.String("container", "", "name of azure storage container")
if len(os.Args) < 2 {
fmt.Println("expected a subcommand")
os.Exit(1)
}
switch os.Args[1] {
case "upload":
uploadCmd.Parse(os.Args[2:])
upload(*uploadPath, *uploadContainer)
case "download":
downloadCmd.Parse(os.Args[2:])
download(*downloadBlobName, *downloadContainerName, *downloadFile)
case "list":
listCmd.Parse(os.Args[2:])
list((*listContainerName))
case "delete":
deleteCmd.Parse(os.Args[2:])
delete(*deleteBlobName, *deleteContainerName)
default:
fmt.Println("expected a subcommand")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment