storage

package
v0.0.0-...-d9f4117 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2022 License: MIT Imports: 15 Imported by: 0

README

Please note, this package has been deprecated. Replacement can be found under github.com/Azure-Samples/azure-sdk-for-go-samples/tree/main/sdk. We strongly encourage you to upgrade to continue receiving updates. See Migration Guide for guidance on upgrading. Refer to our deprecation policy for more details.


services: storage platforms: go author: mcardosos,joshgav

Manage Azure Storage

This package demonstrates how to manage storage accounts and blobs with the Azure SDK for Go. You can use the SDK to create and update storage accounts, list all accounts in a subscription or resource group, list and regenerate keys, and delete accounts. You can also use the SDK to create and delete containers and the blobs they contain.

If you need it, get an Azure trial here.

On this page

Run this sample

  1. If necessary install Go.
  2. Clone this repository.
export PROJECT=github.com/Azure-Samples/azure-sdk-for-go-samples/services/storage
go get -u $PROJECT
cd ${GOPATH}/src/${PROJECT}
dep ensure
  1. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  2. Set the following environment variables based on the properties of this new service principal. Alternatively, fill in the variables in .env.tpl in this directory and rename that to .env.

EnvVar Value
AZURE_TENANT_ID your tenant id
AZURE_SUBSCRIPTION_ID your subscription ID
AZURE_CLIENT_ID service principal/application ID
AZURE_CLIENT_SECRET service principal/application secret
AZURE_RG_NAME name of new resource group
AZURE_LOCATION location for all resources
  1. Run the sample.
go test

What does example.go do?

Check storage account name availability

Check the validity and availability of a string as a storage account name.

result, err := storageAccountsClient.CheckNameAvailability(
  storage.AccountCheckNameAvailabilityParameters{
    Name: to.StringPtr(accountName),
    Type: to.StringPtr("Microsoft.Storage/storageAccounts")})
if err != nil {
  log.Fatalf("%s: %v", "storage account creation failed", err)
}
if *result.NameAvailable != true {
  log.Fatalf("%s: %v", "storage account name not available", err)
}

Create a new storage account
// CreateStorageAccount creates a new storage account.
func CreateStorageAccount() (<-chan storage.Account, <-chan error) {
	storageAccountsClient, _ := getStorageAccountsClient()

	result, err := storageAccountsClient.CheckNameAvailability(
		storage.AccountCheckNameAvailabilityParameters{
			Name: to.StringPtr(accountName),
			Type: to.StringPtr("Microsoft.Storage/storageAccounts")})
	if err != nil {
		log.Fatalf("%s: %v", "storage account creation failed", err)
	}
	if *result.NameAvailable != true {
		log.Fatalf("%s: %v", "storage account name not available", err)
	}

	return storageAccountsClient.Create(
		helpers.ResourceGroupName,
		accountName,
		storage.AccountCreateParameters{
			Sku: &storage.Sku{
				Name: storage.StandardLRS},
			Location: to.StringPtr(helpers.Location),
			AccountPropertiesCreateParameters: &storage.AccountPropertiesCreateParameters{}},
		nil /* cancel <-chan struct{} */)
}

Get the properties of a storage account
account, err := storageClient.GetProperties(groupName, accountName)

List storage accounts by resource group
listGroupAccounts, err := storageClient.ListByResourceGroup(groupName)
onErrorFail(err, "ListByResourceGroup failed")

for _, acc := range *listGroupAccounts.Value {
     fmt.Printf("\t%s\n", *acc.Name)
}

List storage accounts in subscription
listSubAccounts, err := storageClient.List()
onErrorFail(err, "List failed")

for _, acc := range *listSubAccounts.Value {
    fmt.Printf("\t%s\n", *acc.Name)
}

Get the storage account keys
keys, err := storageClient.ListKeys(groupName, accountName)
onErrorFail(err, "ListKeys failed")

fmt.Printf("'%s' storage account keys\n", accountName)
for _, key := range *keys.Keys {
    fmt.Printf("\tKey name: %s\n\tValue: %s...\n\tPermissions: %s\n",
        *key.KeyName,
        (*key.Value)[:5],
        key.Permissions)
    fmt.Println("\t----------------")
}

Regenerate a storage account key
keys, err = storageClient.RegenerateKey(groupName, accountName, storage.AccountRegenerateKeyParameters{
    KeyName: (*keys.Keys)[0].KeyName},
)

Update the storage account

Just like all resources, storage accounts can be updated.

storageClient.Update(groupName, accountName, storage.AccountUpdateParameters{
    Tags: &map[string]*string{
        "who rocks": to.StringPtr("golang"),
        "where":     to.StringPtr("on azure")},
})

List usage
usageList, err := usageClient.List()
onErrorFail(err, "List failed")

for _, usage := range *usageList.Value {
    fmt.Printf("\t%v: %v / %v\n", *usage.Name.Value, *usage.CurrentValue, *usage.Limit)
}

Delete storage account
storageClient.Delete(groupName, accountName)

More information

Please refer to Azure SDK for Go for more information.


This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Documentation

Overview

Example (AppendBlobOperations)
var accountName = testAccountName
var accountGroupName = testAccountGroupName
var containerName = generateName("test-appendblobc")
var blobName = generateName("test-appendblob")
var err error

ctx, cancel := context.WithTimeout(context.Background(), 600*time.Second)
defer cancel()

_, err = CreateContainer(ctx, accountName, accountGroupName, containerName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("created container")

_, err = CreateAppendBlob(ctx, accountName, accountGroupName, containerName, blobName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("created append blob")

blocks := []string{"Hello", "World!", "Hello", "Galaxy!"}
for _, block := range blocks {
	err = AppendToBlob(ctx, accountName, accountGroupName, containerName, blobName, block)
	if err != nil {
		util.LogAndPanic(err)
	}
	util.PrintAndLog("appended data to blob")
}

blob, err := GetBlob(ctx, accountName, accountGroupName, containerName, blobName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("got blob")
util.PrintAndLog(blob)
Output:

created container
created append blob
appended data to blob
appended data to blob
appended data to blob
appended data to blob
got blob
HelloWorld!HelloGalaxy!
Example (BlobObjectReplicationPolicy)
// create two object replication policies on a blob storage account.
// each rule applies to separate source/destination containers.
objRepClient := getObjRepClient()
policy, err := objRepClient.CreateOrUpdate(context.Background(), testAccountGroupName, testAccountName, "default", storage.ObjectReplicationPolicy{
	ObjectReplicationPolicyProperties: &storage.ObjectReplicationPolicyProperties{
		SourceAccount:      to.StringPtr("source-account"),
		DestinationAccount: to.StringPtr("destination-account"),
		Rules: &[]storage.ObjectReplicationPolicyRule{
			{
				RuleID:               to.StringPtr("prefix-match-rule"),
				SourceContainer:      to.StringPtr("some source container"),
				DestinationContainer: to.StringPtr("some destination container"),
				Filters: &storage.ObjectReplicationPolicyFilter{
					// only replicate blobs with the prefix "foo"
					PrefixMatch: &[]string{"foo"},
				},
			},
			{
				RuleID:               to.StringPtr("creation-time-rule"),
				SourceContainer:      to.StringPtr("another source container"),
				DestinationContainer: to.StringPtr("another destination container"),
				Filters: &storage.ObjectReplicationPolicyFilter{
					// only replicate blobs created after this time
					MinCreationTime: to.StringPtr("2021-03-01T13:30:00Z"),
				},
			},
		},
	},
})
if err != nil {
	util.LogAndPanic(err)
}
// display the ID of the policy that was created
util.PrintAndLog(*policy.PolicyID)
Output:

Example (BlobSetServiceProperties)
// retrieves the current blob services settings and modifies them
blobClient := getBlobClient()
props, err := blobClient.GetServiceProperties(context.Background(), testAccountGroupName, testAccountName)
if err != nil {
	util.LogAndPanic(err)
}
// enable blob versioning
props.IsVersioningEnabled = to.BoolPtr(true)
_, err = blobClient.SetServiceProperties(context.Background(), testAccountGroupName, testAccountName, props)
if err != nil {
	util.LogAndPanic(err)
}
Output:

Example (BlockBlobOperations)
var accountName = testAccountName
var accountGroupName = testAccountGroupName
var containerName = generateName("test-blockblobc")
var blobName = generateName("test-blockblob")
var err error

ctx, cancel := context.WithTimeout(context.Background(), 600*time.Second)
defer cancel()

_, err = CreateContainer(ctx, accountName, accountGroupName, containerName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("created container")

_, err = CreateBlockBlob(ctx, accountName, accountGroupName, containerName, blobName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("created blob")

blocks := []string{"Hello", "World!", "Hello", "Galaxy!"}
for i, block := range blocks {
	err = PutBlockOnBlob(ctx, accountName, accountGroupName, containerName, blobName, block, i)
	if err != nil {
		util.LogAndPanic(err)
	}
	util.PrintAndLog(fmt.Sprintf("put block %d", i))
}

list, err := GetUncommitedBlocks(ctx, accountName, accountGroupName, containerName, blobName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog(fmt.Sprintf(
	"list of uncommitted blocks has %d elements",
	len(list.UncommittedBlocks)))

err = CommitBlocks(ctx, accountName, accountGroupName, containerName, blobName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("committed blocks")

blob, err := GetBlob(ctx, accountName, accountGroupName, containerName, blobName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("downloaded blob")
util.PrintAndLog(blob)
Output:

created container
created blob
put block 0
put block 1
put block 2
put block 3
list of uncommitted blocks has 4 elements
committed blocks
downloaded blob
HelloWorld!HelloGalaxy!
Example (ContainerAndBlobs)
var accountName = testAccountName
var accountGroupName = testAccountGroupName
var containerName = generateName("test-blobc")
var err error

ctx, cancel := context.WithTimeout(context.Background(), 600*time.Second)
defer cancel()

_, err = CreateContainer(ctx, accountName, accountGroupName, containerName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("created container")

for i := 0; i < 3; i++ {
	blobName := fmt.Sprintf("test-blob%d", i)
	_, err = CreateBlockBlob(ctx, accountName, accountGroupName, containerName, blobName)
	if err != nil {
		util.LogAndPanic(err)
	}
	util.PrintAndLog(fmt.Sprintf("created test-blob%d", i))
}

list, err := ListBlobs(ctx, accountName, accountGroupName, containerName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog(fmt.Sprintf("listed %d blobs", len(list.Segment.BlobItems)))
Output:

created container
created test-blob0
created test-blob1
created test-blob2
listed 3 blobs
Example (PageBlobOperations)
var accountName = testAccountName
var accountGroupName = testAccountGroupName
var containerName = generateName("test-pageblobc")
var blobName = generateName("test-pageblob")
var err error

ctx, cancel := context.WithTimeout(context.Background(), 600*time.Second)
defer cancel()

_, err = CreateContainer(ctx, accountName, accountGroupName, containerName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("created container")

pages := []string{"Hello", "World!", "Hello", "Galaxy!"}
_, err = CreatePageBlob(ctx, accountName, accountGroupName, containerName, blobName, len(pages))
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("created page blob")

for i, page := range pages {
	err = PutPage(ctx, accountName, accountGroupName, containerName, blobName, page, i)
	if err != nil {
		util.LogAndPanic(err)
	}
	util.PrintAndLog(fmt.Sprintf("put page %d", i))
}

_, err = GetBlob(ctx, accountName, accountGroupName, containerName, blobName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("downloaded blob")
// empty bytes are in fact mixed in between the strings
// so although this appears to emit `HelloWorld!HelloGalaxy!`
// it doesn't match the expected output
// TODO: find a better way to test
// util.PrintAndLog(string(blob))

var pageToClear int = 2
err = ClearPage(ctx, accountName, accountGroupName, containerName, blobName, pageToClear)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog(fmt.Sprintf("cleared page %d", pageToClear))

_, err = GetPageRanges(ctx, accountName, accountGroupName, containerName, blobName, len(pages))
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("got page ranges")
Output:

created container
created page blob
put page 0
put page 1
put page 2
put page 3
downloaded blob
cleared page 2
got page ranges
Example (StorageAccountOperations)
var groupName = testAccountGroupName
var accountName = testAccountName

ctx, cancel := context.WithTimeout(context.Background(), 600*time.Second)
defer cancel()
// don't cleanup yet so dataplane tests can use account
// defer resources.Cleanup(ctx)

_, err := resources.CreateGroup(ctx, groupName)
if err != nil {
	util.LogAndPanic(err)
}

_, err = resources.RegisterProvider(ctx, "Microsoft.Storage")
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("registered resource provider")

result, err := CheckAccountNameAvailability(ctx, accountName)
log.Printf("[%T]: %+v\n", result, result)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("checked for account availability")

var errOuter, errInner error // see next comment
_, errOuter = CreateStorageAccount(ctx, accountName, groupName)
if errOuter != nil {
	// this could be because we've already created it, and a way to check
	// that is to try to get it, so that's what we do here. if we can get
	// it we pretend we created it so tests can proceed. if we can't get
	// it, we don't want to confuse the tester and return the error for the
	// Get, so we return the orignial error from the Create.
	_, errInner = GetStorageAccount(ctx, accountName, groupName)
	if errInner != nil {
		util.PrintAndLog(errOuter.Error())
	}
}
util.PrintAndLog("created storage account")

_, err = GetStorageAccount(ctx, accountName, groupName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("got storage account details")

_, err = UpdateAccount(ctx, accountName, groupName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("updated storage account")

_, err = ListAccountsByResourceGroup(ctx, groupName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("listed storage accounts in resource group")

_, err = ListAccountsBySubscription(ctx)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("listed storage accounts in subscription")

_, err = GetAccountKeys(ctx, accountName, groupName)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("get storage account keys")

_, err = RegenerateAccountKey(ctx, accountName, groupName, 1)
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("regenerated second storage account key")

_, err = ListUsage(ctx, config.Location())
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("listed usage")
Output:

registered resource provider
checked for account availability
created storage account
got storage account details
updated storage account
listed storage accounts in resource group
listed storage accounts in subscription
get storage account keys
regenerated second storage account key
listed usage

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendToBlob

func AppendToBlob(ctx context.Context, accountName, accountGroupName, containerName, blobName, message string) error

AppendToBlob appends new data to the specified append blob

func CheckAccountNameAvailability

func CheckAccountNameAvailability(ctx context.Context, accountName string) (storage.CheckNameAvailabilityResult, error)

CheckAccountNameAvailability checks if the storage account name is available. Storage account names must be unique across Azure and meet other requirements.

func ClearPage

func ClearPage(ctx context.Context, accountName, accountGroupName, containerName, blobName string, pageNumber int) error

ClearPage clears the specified page in the page blob

func CommitBlocks

func CommitBlocks(ctx context.Context, accountName, accountGroupName, containerName, blobName string) error

CommitBlocks commits the uncommitted blocks to the blob

func CreateAppendBlob

func CreateAppendBlob(ctx context.Context, accountName, accountGroupName, containerName, blobName string) (azblob.AppendBlobURL, error)

CreateAppendBlob creates an empty append blob

func CreateBlockBlob

func CreateBlockBlob(ctx context.Context, accountName, accountGroupName, containerName, blobName string) (azblob.BlockBlobURL, error)

CreateBlockBlob creates a new block blob

func CreateContainer

func CreateContainer(ctx context.Context, accountName, accountGroupName, containerName string) (azblob.ContainerURL, error)

CreateContainer creates a new container with the specified name in the specified account

func CreatePageBlob

func CreatePageBlob(ctx context.Context, accountName, accountGroupName, containerName, blobName string, pages int) (azblob.PageBlobURL, error)

CreatePageBlob creates a new test blob in the container specified.

func CreateStorageAccount

func CreateStorageAccount(ctx context.Context, accountName, accountGroupName string) (storage.Account, error)

CreateStorageAccount starts creation of a new storage account and waits for the account to be created.

func DeleteContainer

func DeleteContainer(ctx context.Context, accountName, accountGroupName, containerName string) error

DeleteContainer deletes the named container.

func DeleteStorageAccount

func DeleteStorageAccount(ctx context.Context, accountName, accountGroupName string) (autorest.Response, error)

DeleteStorageAccount deletes an existing storate account

func GetAccountKeys

func GetAccountKeys(ctx context.Context, accountName, accountGroupName string) (storage.AccountListKeysResult, error)

GetAccountKeys gets the storage account keys

func GetBlob

func GetBlob(ctx context.Context, accountName, accountGroupName, containerName, blobName string) (string, error)

GetBlob downloads the specified blob contents

func GetContainer

func GetContainer(ctx context.Context, accountName, accountGroupName, containerName string) (azblob.ContainerURL, error)

GetContainer gets info about an existing container.

func GetPageRanges

func GetPageRanges(ctx context.Context, accountName, accountGroupName, containerName, blobName string, pages int) (*azblob.PageList, error)

GetPageRanges gets a list of valid page ranges in the page blob

func GetStorageAccount

func GetStorageAccount(ctx context.Context, accountName, accountGroupName string) (storage.Account, error)

GetStorageAccount gets details on the specified storage account

func GetUncommitedBlocks

func GetUncommitedBlocks(ctx context.Context, accountName, accountGroupName, containerName, blobName string) (*azblob.BlockList, error)

GetUncommitedBlocks gets a list of uncommited blobs

func ListAccountsByResourceGroup

func ListAccountsByResourceGroup(ctx context.Context, groupName string) (storage.AccountListResult, error)

ListAccountsByResourceGroup lists storage accounts by resource group.

func ListAccountsBySubscription

func ListAccountsBySubscription(ctx context.Context) (storage.AccountListResultIterator, error)

ListAccountsBySubscription lists storage accounts by subscription.

func ListBlobs

func ListBlobs(ctx context.Context, accountName, accountGroupName, containerName string) (*azblob.ListBlobsFlatSegmentResponse, error)

ListBlobs lists blobs on the specified container

func ListUsage

func ListUsage(ctx context.Context, location string) (storage.UsageListResult, error)

ListUsage gets the usage count and limits for the resources in the subscription based on location

func PutBlockOnBlob

func PutBlockOnBlob(ctx context.Context, accountName, accountGroupName, containerName, blobName, message string, blockNum int) error

PutBlockOnBlob adds a block to a block blob. It does not commit the block.

func PutPage

func PutPage(ctx context.Context, accountName, accountGroupName, containerName, blobName, page string, pages int) error

PutPage adds a page to the page blob TODO: page should be []byte

func RegenerateAccountKey

func RegenerateAccountKey(ctx context.Context, accountName, accountGroupName string, key int) (storage.AccountListKeysResult, error)

RegenerateAccountKey regenerates the selected storage account key. `key` can be 0 or 1.

func UpdateAccount

func UpdateAccount(ctx context.Context, accountName, accountGroupName string) (storage.Account, error)

UpdateAccount updates a storage account by adding tags

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL