Getting Started with Azure Storage Resource Provider in Go

Mariana Cardoso 提供
上次更新时间:2017/7/11
在 GitHub 上编辑

This package demonstrates how to manage storage accounts with Azure Storage Resource Provider. The Storage Resource Provider is a client library for working with the storage accounts in your Azure subscription. Using the client library, you can create a new storage account, read its properties, list all storage accounts in a given subscription or resource group, read and regenerate the storage account keys, and delete a storage account.

If you don't have a Microsoft Azure subscription you can get a FREE trial account here.

On this page

Run this sample

  1. If you don't already have it, install Go.

  2. Clone the repository.

git clone https://github.com:Azure-Samples/app-service-web-go-manage.git
  1. Install the dependencies using glide.
cd app-service-web-go-manage
glide install
  1. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  2. Set the following environment variables using the information from the service principle that you created.

export AZURE_TENANT_ID={your tenant id}
export AZURE_CLIENT_ID={your client id}
export AZURE_CLIENT_SECRET={your client secret}
export AZURE_SUBSCRIPTION_ID={your subscription id}
> [AZURE.NOTE] On Windows, use `set` instead of `export`.
  1. Run the sample.
go run example.go

What does example.go do?

The sample checks storage account name availability, creates a new storage account, gets the storage account properties, lists the storage accounts in the subscription or resource group, lists the storage account keys, regenerates the storage account keys, updates the storage account SKU, and deletes the storage account.

Register the Storage Resource Provider

resourcesClient.Register(provider)

Check storage account name availability

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

result, err := storageClient.CheckNameAvailability(storage.AccountCheckNameAvailabilityParameters{
    Name: to.StringPtr(accountName),
    Type: to.StringPtr("Microsoft.Storage/storageAccounts")},
)
onErrorFail(err, "CheckNameAvailability failed")

if *result.NameAvailable == true {
    fmt.Printf("\t'%s' is available!\n", accountName)
} else {
     fmt.Printf("\t'%s' is not available :(\n\tReason: %s\n\tMessage: %s\n",
        accountName,
        result.Reason,
        *result.Message)
}

A storage account needs a resource group to be created.

groupClient.CreateOrUpdate(groupName, resources.Group{
    Location: to.StringPtr(location),
})

Create a new storage account

storageClient.Create(groupName, accountName, storage.AccountCreateParameters{
    Sku: &storage.Sku{
        Name: storage.StandardLRS,
    },
    Location: to.StringPtr(location),
    AccountPropertiesCreateParameters: &storage.AccountPropertiesCreateParameters{},
}, nil)

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)

At this point, the sample also deletes the resource group that it created.

groupsClient.Delete(groupName, nil)

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.