0
1

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.

[AzureBlobStorage / python] ファイルをアップロードしたいだけなのに

Posted at

1. 概要

前提

  • python で開発作業をしている
  • アップロード先は、 Azure の blob storage service
  • 今回は csv をアップロードした
  • html など、他のファイルでも同様の方法でアップロードできるよ

発生した課題

Azure の BlobStorageService に python コードでファイルをアップロードしたかったのだけど、以下の記事の通りにしたところ、

下記エラー (など、他1件) が発生した。

databricks
from azure.storage.blob import BlockBlobService

# 出力
ImportError: cannot import name 'BlockBlobService' from 'azure.storage.blob' (/local_disk0/.ephemeral_nfs/envs/pythonEnv-0958e968-c466-4814-af6c-068d76bb71bb/lib/python3.10/site-packages/azure/storage/blob/__init__.py)

2. 環境

項目 version など
実行環境 Azure Databricks
python 3.10
azure-storage-blob 12.18.3

3. 調査経過

BlockBlobService について

stackoverflow の以下の記事によると BlockBlobService は古いバージョンにあったクラスらしい。 (2.1.0?)

自分が使っているバージョンを確認してみたところ、

databricks
%pip list | grep azure-storage-blob

# 出力
azure-storage-blob       12.18.3

上記の通り、バージョンに差がありすぎて、古いバージョンの azure-storage-blob をインストールする気にはなれなかった。

そこで、新しいバージョンでも使えるクラス (BlobServiceClient) で処理を書くことにした。

BlobServiceClient について

「概要」欄に貼った stackoverflow の記事では、 create_blob_from_text が使用されていたが、これまた古いメソッドであり、現在は使用できず、以下のようなエラーが発生した。

AttributeError: 'BlobServiceClient' object has no attribute 'create_blob_from_text'

BlobServiceClient.get_container_client メソッド

Azure公式の以下の記事より、 BlobServiceClient.get_container_client というメソッドを使えば BlobStorageService へとファイルをアップロードできることがわかった。

4. ソースコード

結果的にはこれで行けた。

databricks
# module install と restart
%pip install -q azure-storage-blob

dbutils.library.restartPython()
databricks
from azure.storage.blob import BlobServiceClient
import pandas as pd


# Create dummy data
data = [
    [1, 2, 3],
    [4, 5, 6],
    [8, 7, 9]
]
df = pd.DataFrame(data, columns=["col1", "col2", "col3"])
output = df.to_csv(index_label="idx", encoding = "utf-8")
print(output)


# Define Auth information, and so on ...
ACCOUNT_NAME: str = "<your account name>"
ACCOUNT_KEY: str = "<your account key>"
CONTAINER_NAME: str = "<your container name>"


# Initialize classes to interact with blob storage
connect_str = (
    'DefaultEndpointsProtocol=https;AccountName=' + ACCOUNT_NAME
    + ';AccountKey=' + ACCOUNT_KEY 
    + ';EndpointSuffix=core.windows.net'
)
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_client = blob_service_client.get_container_client(container=CONTAINER_NAME)


# Upload 
blob_client = container_client.upload_blob(
    name="sample_output.csv", data=output, overwrite=True
)

5. ひとこと

公式サイトはやはり大事。

でも、1発目で公式サイトを見るとげんなりさせられることが多いので、最初は初心者向けの (Qiita とかの) 記事を見て、少しずつ理解を深めるのがスムーズかな。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?