LoginSignup
3
2

More than 5 years have passed since last update.

OAuthアクセストークンを使ってAzure Storageにアクセスする

Posted at

概要

OAuthアクセストークンを使ってMicrosoft Azureにアクセスするでは Swagger で生成したクライアントで OAuth アクセストークンを利用する方法を紹介したが,
本記事では,Azure Storage SDK for Go を利用する方法を紹介する.

API アクセスキー

Azure Storage SDK のクライアントはアクセストークンではなく,アクセスキーを必要とする.
このアクセスキーは Storage API ではなく,Storage Accounts API 経由で取得する.
また,Storage API は Azure Storage SDK からアクセスできるが,Storage Accounts API はアクセスできないので,
例によって Swagger を使って Azure REST API Specifications からバインディングを用意する必要がある.

swagger generate client \
    -f https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-storage/2016-12-01/swagger/storage.json \
    -t storage

Storage Accounts API

生成されたソースコードを用いて,先ずは Storage Accounts API のクライアントを作成する NewStorageAccountsClient 関数を定義する.

import (
    "context"

    httptransport "github.com/go-openapi/runtime/client"
    "github.com/go-openapi/strfmt"

    // 以下は Swagger にて生成されたソースコードに合わせて修正する
    client "github.com/jkawamoto/roadie/cloud/azure/storage/client"
    "github.com/jkawamoto/roadie/cloud/azure/storage/client/storage_accounts"
)

const (
    StorageAPIVersion = "2016-12-01"
)


func NewStorageAccountsClient(ctx context.Context, token string) *storage_accounts.Client{

    manager := client.NewHTTPClient(strfmt.NewFormats())
    switch transport := manager.Transport.(type) {
    case *httptransport.Runtime:
        transport.DefaultAuthentication = httptransport.BearerToken(token)
        manager.StorageAccounts.SetTransport(transport)
    }
    return manager.StorageAccounts

}

次に,得られたクライアントの StorageAccountsListKeys メソッドを使ってアクセスキーを取得する GetStorageKey 関数を定義する.
またこの関数はアクセスキーが見つからない場合 StorageAccountsRegenerateKey 関数を使って生成する.

const (
    // サブスクリプション ID
    SubscriptionID = ""
    // リソースグループ名
    ResourceGroupName = ""
)

func GetStorageKey(ctx context.Context, cli *storage_accounts.Client, account string) (key string, err error) {

    keyList, err := cli.StorageAccountsListKeys(
        storage_accounts.NewStorageAccountsListKeysParamsWithContext(ctx).
            WithAPIVersion(StorageAPIVersion).
            WithSubscriptionID(SubscriptionID).
            WithResourceGroupName(ResourceGroupName).
            WithAccountName(account)) // Storage アカウント名

    if err != nil || len(keyList.Payload.Keys) == 0 {
        res, err := cli.StorageAccountsRegenerateKey(
            storage_accounts.NewStorageAccountsRegenerateKeyParamsWithContext(ctx).
                WithAPIVersion(StorageAPIVersion).
                WithSubscriptionID(SubscriptionID).
                WithResourceGroupName(ResourceGroupName).
                WithAccountName(account))

        if err != nil {
            return "", err
        }
        key = res.Payload.Keys[0].Value
    } else {
        key = keyList.Payload.Keys[0].Value
    }

    return

}

アクセスキーを使った Storage API のアクセス

最後に,得られたアクセスキーを使って Storage API のクライアントを作成する.

import (
    storage "github.com/Azure/azure-storage-go"
)

func NewStorageClient(account, key string) (storage.Client, error) {

    return storage.NewBasicClient(account, key)

}
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2