func main() { cnt := containerPrefix + helpers.RandString(32-len(containerPrefix)) cli := helpers.GetStorageClient(accountName, accountKey).GetBlobService() // Create a container. ok, err := cli.CreateContainerIfNotExists(cnt, storage.ContainerAccessTypePrivate) if !ok { fmt.Printf("Failed to create container '%s': %s\n", cnt, err.Error()) return } // Make sure not to clobber up the storage account. defer func() { fmt.Printf("Deleting '%s' and all its contents\n", cnt) cli.DeleteContainer(cnt) }() fmt.Printf("Successfully created '%s'\n", cnt) // Create an empty blob blob := blobPrefix + helpers.RandString(32-len(blobPrefix)) if err := cli.CreateBlockBlob(cnt, blob); err != nil { fmt.Printf("Failed to create blob '%s' in '%s': %s\n", blob, cnt, err.Error()) return } fmt.Printf("Successfully created '%s'\n", blob) }
func main() { if len(os.Args) < 2 { fmt.Printf("usage: blob03 blob-name\n") return } blob := os.Args[1] cli := helpers.GetStorageClient(accountName, accountKey).GetBlobService() // Create a container. _, err := cli.CreateContainerIfNotExists(cnt, storage.ContainerAccessTypeBlob) if err != nil { fmt.Printf("ERROR: Failed to create container '%s': %s\n", cnt, err.Error()) return } fmt.Printf("Successfully created '%s'\n", cnt) // First, create an empty page blob if err := cli.PutPageBlob(cnt, blob, blobSize); err != nil { fmt.Printf("Failed to create '%s' in '%s': %s\n", blob, cnt, err.Error()) return } // Then, write some pages. We will write pages 0, 1, and 2, leaving five pages blank. data := helpers.RandBytes(1536) if err := writePage(cli, blob, 0, 511, data[0:512]); err != nil { return } if err := writePage(cli, blob, 512, 1023, data[512:1024]); err != nil { return } if err := writePage(cli, blob, 1024, 1535, data[1024:1536]); err != nil { return } // Now that we have written the pages, let's check what's been stored. url := cli.GetBlobURL(cnt, blob) // The first three pages should correspond to what was written. if err := validate(cli, blob, 0, 1535, data[0:1536]); err != nil { fmt.Printf("Failed validation of %s: %s\n", url, err.Error()) return } // The next five pages should be all zeroes. if err := validate(cli, blob, 1536, blobSize-1, make([]byte, blobSize)); err != nil { fmt.Printf("Failed validation of %s: %s\n", url, err.Error()) return } fmt.Printf("Successfully created, wrote to, and read a page blob at '%s\n", url) // Next, let's try clearing a page, that is, setting it to all zeroes. if err := clearPage(cli, blob, 512, 1023); err != nil { return } // Now that we have written the pages, let's check what's been stored. // The first and third pages should correspond to what was written. if err := validate(cli, blob, 0, 511, data[0:512]); err != nil { fmt.Printf("Failed validation of %s: %s\n", url, err.Error()) return } // The second page should be all zeroes. if err := validate(cli, blob, 512, 1023, make([]byte, 1024)); err != nil { fmt.Printf("Failed validation of %s: %s\n", url, err.Error()) return } if err := validate(cli, blob, 1024, 1535, data[0:1536]); err != nil { fmt.Printf("Failed validation of %s: %s\n", url, err.Error()) return } // The last five pages should still be all zeroes. if err := validate(cli, blob, 1536, blobSize-1, make([]byte, blobSize)); err != nil { fmt.Printf("Failed validation of %s: %s\n", url, err.Error()) return } // We can check which pages have data by getting the valid page ranges. ranges, err := cli.GetPageRanges(cnt, blob) if err != nil { fmt.Printf("Failed to get page ranges for %s: %s\n", url, err.Error()) return } fmt.Printf("Successfully cleared a page at '%s\n", url) for _, rng := range ranges.PageList { fmt.Printf("[%d,%d]\n", rng.Start, rng.End) } pl := ranges.PageList if len(pl) != 2 || pl[0].Start != 0 || pl[0].End != 511 || pl[1].Start != 1024 || pl[1].End != 1535 { fmt.Printf("Incorrect page list returned\n") return } fmt.Printf("Successfully checked page ranges of '%s\n", url) }
func main() { if len(os.Args) < 3 { fmt.Printf("usage: blob02 file-name blob-name\n") return } fileName := os.Args[1] blob := os.Args[2] cli := helpers.GetStorageClient(accountName, accountKey).GetBlobService() // Create a container. _, err := cli.CreateContainerIfNotExists(cnt, storage.ContainerAccessTypeBlob) if err != nil { fmt.Printf("ERROR: Failed to create container '%s': %s\n", cnt, err.Error()) return } fmt.Printf("Successfully created '%s'\n", cnt) // Open the file f, err := os.Open(fileName) if err != nil { fmt.Printf("ERROR: Unable to locate or open file at %s (%s)\n", fileName, err.Error()) return } defer f.Close() // Get the file info, so we can know its size fileInfo, err := f.Stat() if err != nil { fmt.Printf("ERROR: Unable to retrieve info on file at %s (%s)\n", fileName, err.Error()) return } props := storage.BlobProperties{ContentType: imageJPG} // Create the blob from the file. Also, pass in a properties block so that the // content type may be set. if err := cli.CreateBlockBlobFromReader(cnt, blob, uint64(fileInfo.Size()), f, &props); err != nil { fmt.Printf("Failed to create '%s' in '%s': %s\n", blob, cnt, err.Error()) return } // We'll want the url of the blob url := cli.GetBlobURL(cnt, blob) fmt.Printf("Successfully uploaded file to '%s'\n", url) if err := cli.SetBlobProperties(cnt, blob, props); err != nil { fmt.Printf("Failed to set properties for '%s': %s\n", url, err.Error()) return } fmt.Printf("Successfully set properties for '%s'\n", url) // Just to make sure, let's see what the properties on the server! stored, err := cli.GetBlobProperties(cnt, blob) if err != nil { fmt.Printf("Failed to retrieve blob properties for '%s': %s\n", url, err.Error()) return } fmt.Printf("Stored properties: %s\n", helpers.ToJSON(*stored)) }