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?

Azuriteを使おう

Posted at

Azuriteを使おう

前準備

Azuriteをインストールする

  • VSCodeでVMに接続する
  • 左のペインの 田 みたいなアイコンをクリック
  • "Marketplace で拡張機能を..." の部分に「azurite」と入力して検索
    • Microsoft製のものをインストール

[公式資料]

微妙に気になるかもしれないところ

  • npmとかインストールしなくていいの?
    • VSCode Extensionでインストールする場合は不要らしい
    • まぁVSCodeがそもそもNode系だから
  • ローカル側に影響しない?

Azurite用の鍵を準備する

データ格納ディレクトリの作成

/azurite/* に必要なデータを格納します

cd /
sudo mkdir azurite
sudo chmod 777 azurite
cd azurite
mkdir local
mkdir httpskey
cd httpskey

鍵の作成

cd /azurite/httpskey
openssl req -newkey rsa:2048 -x509 -nodes -keyout azurite_key.pem -new -out azurite_cert.pem -sha256 -days 365 -addext "subjectAltName=IP:(VMに設定したIPアドレス)" -subj "/C=CO/ST=ST/L=LO/O=OR/OU=OU/CN=CN"

鍵管理ユーティリティのインストール

sudo apt-get install -y ca-certificates

鍵の登録

cd /azurite/httpskey
sudo cp azurite_cert.pem /usr/local/share/ca-certificates/azurite_cert.crt
sudo update-ca-certificates

[公式資料]

Azuriteを設定する

設定画面を開く

  • VScode左ペインの田みたいなアイコン
    • Azurite
      • ⚙ (歯車アイコン)
        • 設定

設定項目

  • [ユーザー][リモート][ワークスペース]

  • 複数あるタブのうち「リモート」を選ぶ

  • Azurite: Blob Host

    • VMに設定したIPアドレス
  • Azurite: Cert

    • /azurite/httpskey/azurite_cert.pem
  • Azurite: Key

    • /azurite/httpskey/azurite_key.pem
  • Azurite: Location

    • /azurite/local
  • Azurite: Oauth

    • basic
  • Azurite: Queue Host

    • VMに設定したIPアドレス
  • Azurite: Skip API Version Check

    • (チェックを入れる)
  • Azurite: Table Host

    • VMに設定したIPアドレス

Azuriteを起動する

  • 表示
    • コマンドパレット
      • Azurite: Start

Azuriteを終了する

  • 表示
    • コマンドパレット
      • Azurite: Close

Azure Storage ExplolerからAzuriteへ接続する

まずはAzure Storage Explolerをダウンロードとインストール

公式からダウンロード

https://azure.microsoft.com/ja-jp/products/storage/storage-explorer

インストール

  • インストールはデフォルト値で
  • UserかSystemかはどっちでも
    • 今回はSystemでインストール

SSL証明書のインポート

  • VSCodeでVMに接続
  • azurite_cert.pem をダウンロード
    • エクスプローラーペインを右クリック
    • ワークスペースにフォルダを追加
    • /azurite/httpskey/ を開く
    • しばらく待つとウィンドウの再読み込みを要求されるので再読み込み
    • azurite_cert.pem を右クリックしてダウンロード
  • Azure Storage Exploler を起動
    • 編集
      • SSL証明書
        • 証明書のインポート
          • azurite_cert.pem
            • 開く

Azuriteに接続する

Azuriteを起動

上の方にあるので詳細は割愛

Azure Storage Explolerの作業

  • 🔌 (コンセント)みたいなアイコンをクリック
    • ストレージアカウントまたはサービス
      • 接続文字列
        • 次へ
      • 表示名
        • なんか適当に
      • 接続文字列

        DefaultEndpointsProtocol=https;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=https://(VMに設定したIPアドレス):10000/devstoreaccount1;QueueEndpoint=https://(VMに設定したIPアドレス):10001/devstoreaccount1;TableEndpoint=https://(VMに設定したIPアドレス):10002/devstoreaccount1;
        
      • 接続

ときどき「ローカルにAzuriteがインストールされてないけどインストールするか?」と聞かれるが、別にインストールしなくていい

[公式資料]

PythonスクリプトからAzuriteへ接続

pip3のインストール

sudo apt install python3-pip

ライブラリのインストール

sudo pip install azure-storage-blob azure-identity --break-system-packages

Pythonスクリプトの作成

ファイル作成

touch blob.py

コード

from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

try:
    print("Azure Blob Storage Python quickstart sample")
    # Connection String
    connection_string = "DefaultEndpointsProtocol=https;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=https://(VMに設定したIPアドレス):10000/devstoreaccount1;"
    # Create the BlobServiceClient object
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    # Create a unique name for the container
    container_name = "mycontainer2025"
    # Create the container
    container_client = blob_service_client.create_container(container_name)

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

スクリプトを実行

python3 blob.py

実行するとmycontainer2025というコンテナができるのでAzure Storage Explolerとかで見に行く。

[公式資料]

[補足]

  • DefaultAzureCredential
    • PythonSDKのDefaultAzureCredentialは挙動がおかしい時がある
    • MSDNの資料の中にCredential関係の記述がない
      • DefaultAzureCredentialが見に行く場所に関する設定が見つからない
    • MSDNのコードを見る限り接続文字列でしかやってない
    • リモートエミュレーターのときはそもそも機能しないかもとの記述もある
    • 諦めて接続文字列でやるしかないっぽい
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?