0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

概要

ストレージアカウント情報の取得やコンテナの確認方法等を備忘録として超簡単にまとめてみました。

実行環境

macOS Monterey 12.3.1
python 3.8.12
azure cli 2.41.0

Azure へのログイン

ローカルのターミナルから Azureログインします。

$ az login --tenant [tenantId]
$ az account set --subscription [subscriptionName]

ストレージアカウント情報

ストレージアカウントは既に作成されているものとします。

# ストレージアカウントのリスト
$ az storage account list -g [ResourceGroupName] -o table
AccessTier    AllowBlobPublicAccess    CreationTime                      EnableHttpsTrafficOnly    IsHnsEnabled    Kind       Location    MinimumTlsVersion    Name          PrimaryLocation    ProvisioningState    ResourceGroup     StatusOfPrimary
------------  -----------------------  --------------------------------  ------------------------  --------------  ---------  ----------  -------------------  --------------------  -----------------  -------------------  -------------------  -----------------
Hot           True                     2021-12-04T16:11:49.809981+00:00  True                      True            StorageV2  japaneast   TLS1_0               [StorageAccountName]  japaneast          Succeeded            [ResourceGroupName]  available


# ストレージアカウントのキー
$ az storage account keys list --account-name [StorageAccountName] --resource-group [ResourceGroupName] --output table
CreationTime                      KeyName    Permissions    Value
--------------------------------  ---------  -------------  ------------------------
2021-12-04T16:11:49.888094+00:00  key1       FULL           54GH・・・省略・・・qqAA==
2021-12-04T16:11:49.888094+00:00  key2       FULL           GjBc・・・省略・・・FNng==


# ストレージアカウントの接続文字列
$ az storage account show-connection-string --name [StorageAccountName] --resource-group [ResourceGroupName] -o table
ConnectionString
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DefaultEndpointsProtocol=https;EndpointSuffix=core.windows.net;AccountName=[StorageAccountName];AccountKey=[StorageAccountKey1]


# ストレージアカウントのID
$ az storage account show --name [StorageAccountName] --resource-group [ResourceGroupName] --query id
"/subscriptions/[SubscriptionId]/resourceGroups/[ResourceGroupName]/providers/Microsoft.Storage/storageAccounts/[StorageAccountName]"

コンテナ情報

# コンテナのリスト
$ az storage container list --account-name [StorageAccountName] -o table      
Name     Lease Status    Last Modified
-------  --------------  -------------------------
app                      2022-02-16T07:25:53+00:00
tfstate                  2021-12-04T16:12:31+00:00


# コンテナの作成
$ az storage container create --account-name [StorageAccountName] --name partnercenter       
{
  "created": true
}


# コンテナのリストの再確認
$ az storage container list --account-name [StorageAccountName] -o table              
Name           Lease Status    Last Modified
-------------  --------------  -------------------------
app                            2022-02-16T07:25:53+00:00
partnercenter                  2022-10-20T06:02:51+00:00
tfstate                        2021-12-04T16:12:31+00:00

BLOB情報

# BLOBのリスト
$ az storage blob list --account-name [StorageAccountName] --container-name tfstate -o table
Name                                Blob Type    Blob Tier    Length    Content Type      Last Modified              Snapshot
----------------------------------  -----------  -----------  --------  ----------------  -------------------------  ----------
Databricks01                        BlockBlob    Hot                                      2021-12-04T16:45:45+00:00
Databricks01/terraform.tfstate      BlockBlob    Hot          156       application/json  2021-12-06T13:50:36+00:00
githubactions001                    BlockBlob    Hot                                      2022-09-12T14:34:57+00:00
githubactions001/terraform.tfstate  BlockBlob    Hot          156       application/json  2022-09-12T16:41:56+00:00
test01                              BlockBlob    Hot                                      2021-12-05T10:26:47+00:00
test01/terraform.tfstate            BlockBlob    Hot          156       application/json  2022-01-18T05:45:12+00:00
tftest001                           BlockBlob    Hot                                      2022-09-12T12:27:40+00:00
tftest001/terraform.tfstate         BlockBlob    Hot          156       application/json  2022-09-12T12:47:19+00:00

PythonプログラムでBLOBの確認

実行するプログラム

blob_list.py
from azure.storage.blob import ContainerClient

# 接続文字列
CONNECTION_STRING = [az storage account show-connection-string...]
# コンテナ名
CONTAINER_NAME = "tfstate"

try:
    container = ContainerClient.from_connection_string(conn_str=CONNECTION_STRING, container_name=CONTAINER_NAME)
    blob_list = container.list_blobs()
    for blob in blob_list:
        print("Size:{}   \t Name:{} ".format(blob.size, blob.name))

except Exception as ex:
    print('Exception:')
    print(ex)

プログラムの実行

$ python blob_list.py
Size:0   	 Name:Databricks01 
Size:156   	 Name:Databricks01/terraform.tfstate 
Size:0   	 Name:githubactions001 
Size:156   	 Name:githubactions001/terraform.tfstate 
Size:0   	 Name:test01 
Size:156   	 Name:test01/terraform.tfstate 
Size:0   	 Name:tftest001 
Size:156   	 Name:tftest001/terraform.tfstate 

まとめ

これで CLI と Pythonプログラム から Blob Storage を管理できそうです。

参考記事

以下の記事を参考にさせていただきました。感謝申し上げます。
PythonでAzure Blob Storageを管理する

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?