Skip to content

Instantly share code, notes, and snippets.

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 jasonhancock/620ebeefa7b9f6654ae1bdee067b8c7d to your computer and use it in GitHub Desktop.
Save jasonhancock/620ebeefa7b9f6654ae1bdee067b8c7d to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"github.com/Azure/azure-sdk-for-go/storage"
"io"
"log"
"os"
)
func main() {
fmt.Println("hello world")
bc, err := storage.NewBasicClient("account name", "account key")
if err != nil {
panic(err)
}
blob := bc.GetBlobService()
response, err := blob.ListContainers(storage.ListContainersParameters{})
if err != nil {
panic(err)
}
fmt.Printf("%v\n", response)
for _, c := range response.Containers {
fmt.Println("%s\n", c.Name)
}
// Uploads a file as a block blob to azure
filepath := "input2.txt"
total := 100
for i := 0; i < total; i++ {
if i%10 == 0 {
fmt.Printf("Processing request %d of %d\n", i, total)
}
file, err := os.Open(filepath)
if err != nil {
log.Fatal(err)
}
fi, err := file.Stat()
if err != nil {
log.Fatal(err)
}
// Note: CreateBlockBlobFromReader only goes up to 64MiB
err = blob.CreateBlockBlobFromReader("container1", fmt.Sprintf("003/input%d.txt", i), uint64(fi.Size()), file, nil)
if err != nil {
panic(err)
}
file.Close()
}
panic(nil)
// List all blobs in a container?
nextmarker := ""
for page := 0; nextmarker != "" || page == 0; page++ {
fmt.Printf("Fetching page %d %s\n", page, nextmarker)
blobs, err := blob.ListBlobs("container1", storage.ListBlobsParameters{
Marker: nextmarker,
})
if err != nil {
panic(err)
}
nextmarker = blobs.NextMarker
fmt.Printf("nextmarker=%s\n", blobs.NextMarker)
//fmt.Printf("%v\n", blobs)
fmt.Printf("%s\n", blobs.Blobs[0].Name)
/*
for _, b := range blobs.Blobs {
fmt.Printf("%s %s %v\n", b.Name, b.Properties.BlobType, b.Properties)
}
*/
}
// Download a blob:
reader, err := blob.GetBlob("container1", "input1.txt")
if err != nil {
panic(err)
}
// Open file for writing
destfile, err := os.Create("./output.txt")
if err != nil {
log.Fatal(err)
}
defer destfile.Close()
dest := bufio.NewWriter(destfile)
_, err = io.Copy(dest, reader)
if err != nil {
panic(err)
}
reader.Close()
dest.Flush()
// Get blob properties - It appears the "BlobType" isn't returned in the block blobs, so have to manually fetch it
p, err := blob.GetBlobProperties("container1", "input1.txt")
fmt.Printf("%s\n", p.BlobType)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment