【发布时间】:2018-03-18 15:57:22
【问题描述】:
我正在使用 python Azure sdk。当文件上传时,其内容类型更改为 application/octet-stream。
我想为 PNG 图像设置其默认内容类型,例如 image/png。
我正在使用以下方法上传文件put_block_blob_from_path()
有没有办法保留默认的内容类型?谢谢
【问题讨论】:
我正在使用 python Azure sdk。当文件上传时,其内容类型更改为 application/octet-stream。
我想为 PNG 图像设置其默认内容类型,例如 image/png。
我正在使用以下方法上传文件put_block_blob_from_path()
有没有办法保留默认的内容类型?谢谢
【问题讨论】:
如果未提供任何内容,Azure blob 将使用默认值“application/octet-stream”。为了获得正确的 mimetypes,这是我对我的烧瓶应用程序所做的:
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
mime_type = f.content_type
print (mime_type)
print (type(f))
try:
blob_service.create_blob_from_stream(container, f.filename, f,
content_settings=ContentSettings(content_type=mime_type))
mime_type 被传递给 ContentSettings 以获取上传到 azure blob 的文件的当前 mimetypes。
【讨论】:
您可以像这样显式设置 content-type 属性:
from azure.storage.blob import ContentSettings
block_blob_service.create_blob_from_path(
'mycontainer',
'myblockblob',
'mypngimage.png',
content_settings=ContentSettings(content_type='image/png')
)
来源: https://docs.microsoft.com/en-us/azure/storage/blobs/storage-python-how-to-use-blob-storage
【讨论】:
application/octet-stream。