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?

Microsoft Fabric ノートブックで Entra ID 認証を使用して Azure Storage Blob API を実行する

Posted at

はじめに

ショートカットやパイプラインなどでデータの連携は可能ですが、content-type が固定され、表示のさせ方の制御ができません。

本記事では Fabric Notebook で Azure Storage Blob SDK を使用して安全に認証しつつ content-type を指定する方法を記載します。

contet-type について

たとえば、pdf を表示させたい場合などは application/pdf とするなどのcontent-type を指定しないと、閲覧するのにダウンロード必須になったりします。

image.png

方式

content-type の指定には、Azure Blob の Python SDK からContentSettings を使用します。

また、資格情報ユーティリティを使用することで、 Storage サービスのアクセストークンを取得することができます

python

token = notebookutils.credentials.getToken("storage")

実践

ファイルはレイクハウスのものを使います。このcontent-type を application/vnd.openxmlformats-officedocument.presentationml.presentation で up します

image.png

  1. python ノートブックを起動します。

    image.png

  2. 以下のコードを実行します。

    python
    
    
    def publish_blob(account,container,target_path,source_path,content_type):
        
        from azure.storage.blob import BlobServiceClient , ContentSettings
        from azure.core.credentials import AccessToken
        from datetime import datetime, timedelta
    
        # トークン周り
        token_str = notebookutils.credentials.getToken("storage")
    
        class MyCredential:
            def get_token(self, *scopes, **kwargs):
                return AccessToken(token_str, int((datetime.utcnow() + timedelta(hours=1)).timestamp()))
        credential = MyCredential()
    
        # blob 操作
        blob_service = BlobServiceClient(
            account_url = f"https://{account}.blob.core.windows.net",
            credential=credential
        )
    
        blob = blob_service.get_blob_client(container, target_path)
    
        content_settings = ContentSettings(
            content_type=content_type
        )
        with open(source_path, "rb") as f:
            blob.upload_blob(
                f,
                overwrite=True,
                blob_type="BlockBlob",
                content_settings=content_settings
            )
        print(f"-----------------------------------------------------")
        print(f"upload_blob: {target_path}")
    
    filename = "fabric-november-2025-feature-summary.pptx"
    target_path = f"qiita/{filename}"
    source_path = f"/lakehouse/default/Files/pptx/{filename}"
    
    publish_blob(
        account = '<ストレージアカウント名>',
        container = '<コンテナ名>',
        target_path= target_path,
        source_path= source_path,
        content_type = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
    )
    
    

    image.png

  3. アップロードできました。
    image.png

  4. Blob の URL を開くと PowerPoint がブラウザで直接閲覧できます。
    image.png

Microsoft Fabric の更新情報を月次で自動でレポート化、pptx 生成をしています。ご活用ください

image.png

レポートリンク

以上です。参考になれば幸いです。

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?