SAS
generate_blob_sas

azure.storage.blob package

How can I generate an Azure blob SAS URL in Python?
I am trying to generate blob SAS URLs on the fly using the azure-storage-blob package. This solution only works if you have the now-deprecated azure-storage pac...

Retrieval and display from Azure Blob Storage, to Flask Python, to HTML/JS?
I have a simple Flask Azure-webapp which I would like the user to be able to upload images. The images are then stored in an Azure Blob.
import os, uuid
from a...
結局テストの書き方が役立ちそう
https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/storage/azure-storage-blob/tests/test_common_blob.py
使ってみた
事前準備
Azurite 起動
wslにて
user1@amebb:~/acre/blob$ mkdir azurite_data
user1@amebb:~/acre/blob$ mkdir azurite_log
user1@amebb:~/acre/blob$ azurite --silent --location azurite_data --debug azurite_log/debug0504.log
Azurite Blob service is starting at http://127.0.0.1:10000
Azurite Blob service is successfully listening at http://127.0.0.1:10000
Azurite Queue service is starting at http://127.0.0.1:10001
Azurite Queue service is successfully listening at http://127.0.0.1:10001
Azurite Table service is starting at http://127.0.0.1:10002
Azurite Table service is successfully listening at http://127.0.0.1:10002
Azure Storage Explorerで画像アップロード

Windows10側で Azure Storage Explorer を起動し、
左ペイン > ローカルで接続済み > ストレージアカウント > エミュレータ 既定のポート(Key) > Blob Containers > 右クリックしコンテナを作成 にて
コンテナを作成。名前の例 container0504 (ソースコードで使う)
右ペイン > jpg画像をアップロードしておく。画像例 tabe-2023images.jpg (ソースコードで使う)
ソースコード with FastAPI
GitHub - hotchrome2/azure-blob-sas0505
Contribute to hotchrome2/azure-blob-sas0505 development by creating an account on GitHub.
user1@amebb:~/acre/camp$ tree -L 2
.
├── Pipfile
├── Pipfile.lock
├── __init__.py
├── __pycache__
│ └── main.cpython-39.pyc
├── db
│ ├── __init__.py
│ └── blobdb.py
└── src
├── __init__.py
└── main.py
3 directories, 8 files
user1@amebb:~/acre/camp$ cat Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
fastapi = {extras = ["all"], version = "*"}
azure-storage-blob = "*"
[dev-packages]
[requires]
python_version = "3.9"
user1@amebb:~/acre/camp$ cat db/blobdb.py
# import os
from datetime import datetime, timedelta
from azure.storage.blob import (
BlobServiceClient, BlobSasPermissions, BlobClient, ContainerClient, __version__, generate_blob_sas
)
class BlobDB:
def try_azure_blob(self):
# connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
# Blob service にのみ接続する場合、接続文字列は次のようになります。
# DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;
connect_str = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;"
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_name = "container0504"
blob_name = "tabe-2023images.jpg"
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
# if blob_client.exists():
# return "Exist."
# return "False."
token = generate_blob_sas(
blob_client.account_name,
blob_client.container_name,
blob_client.blob_name,
snapshot=blob_client.snapshot,
account_key=blob_client.credential.account_key,
permission=BlobSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1),
)
sas_blob = BlobClient.from_blob_url(blob_client.url, credential=token)
return sas_blob.url
user1@amebb:~/acre/camp$ cat src/main.py
from fastapi import FastAPI
from db.blobdb import BlobDB
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/blobdb/")
def try_blob():
blobdb = BlobDB()
b_ver_str = blobdb.try_azure_blob()
return {"version": b_ver_str}
user1@amebb:~/acre/camp$ pipenv run uvicorn src.main:app --reload
ブラウザで http://127.0.0.1:8000/blobdb/
レスポンスはURL文字列なのでそのURLをブラウザで見ると画像が表示された。よし。
コメント