如何为 azure blob 创建/设置新元数据并使用 Python API 获取元数据?

Cel*_*lik 7 python azure azure-storage azure-blob-storage

我有这样的元数据:

    Container: mycontainer
        Object: myblob
  Content Type: application/octet-stream Charset=UTF-8
Content Length: 26
          ETag: "0x8D18C1E18C0CF2C"
Run Code Online (Sandbox Code Playgroud)

我想添加更多元数据,例如:

     Meta Century: Nineteenth
       Meta Author: Mustafa
Run Code Online (Sandbox Code Playgroud)

添加后应该是:

     Container: azure
        Object: myblob
  Content Type: application/octet-stream Charset=UTF-8
Content Length: 26
          ETag: "0x8D18C1E18C0CF2C"
  Meta Century: Nineteenth
   Meta Author: Mustafa
Run Code Online (Sandbox Code Playgroud)

我需要设置获取这些元数据的函数?

Cel*_*lik 11

Python代码设置获取容器”中天蓝色“ blob ”的元数据:

from azure.storage import BlobService
blob_service = BlobService(account_name='myaccount', account_key='mykey')

# set metadata to "myblob" of container "mycontainer"
blob_service.set_blob_metadata(container_name="mycontainer",
                               blob_name="myblob",
                               x_ms_meta_name_values={"Meta Century":"Nineteenth","Meta Author":"Mustafa"})

#get metadata of "myblob" of container "mycontainer"
metadata = blob_service.get_blob_metadata(container_name="mycontainer",blob_name="myblob")
print metadata
Run Code Online (Sandbox Code Playgroud)

上面的代码“仅”返回用户通过使用设置的元数据

blob_service.set_blob_metadata(....)行代码。

如果您需要内容类型或其他元数据(标头),您应该使用:

blob_service.get_blob("mycontainer", "myblob").__dict__["properties"]
Run Code Online (Sandbox Code Playgroud)