gpt4 book ai didi

python-3.x - 从 Azure Blob 存储、Flask Python、HTML/JS 检索并显示?

转载 作者:行者123 更新时间:2023-12-02 22:52:10 25 4
gpt4 key购买 nike

我有一个简单的 Flask Azure-webapp,我希望用户能够上传图像。然后图像存储在 Azure Blob 中。

import os, uuid
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

try:
print("Azure Blob storage v12 - Python quickstart sample")
# Quick start code goes here
except Exception as ex:
print('Exception:')
print(ex)

connect_str = "XXXXX"
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

container_name = 'kdprivatecontainer'
container_client = blob_service_client.get_container_client(container_name)

# List the blobs in the container
print("\nListing blobs...")
blob_list = container_client.list_blobs()
for blob in blob_list:
print("\t" + blob.name)

我可以通过这种方法看到文件的名称...如何将图像传递到前端网页,同时保持容器为“私有(private)”。

黑暗中的彻底刺探是:但显然不起作用:/

from flask import Flask, render_template
app = Flask(app)

@app.route("/showimg')
def showimage():
blobimage = blob[9].url????
return render_template('showimage.html, img=blobimage)

Any guidance is appreciated. :)

最佳答案

我非常同意@GauravMantri的回答,您需要使用函数generate_container_sasgenerate_blob_sas,为私有(private)容器中的图像生成带有sas token 的url,作为私有(private)容器中的blob > 适用于 Python 的 Azure Blob 存储 SDK v12。

此外,我认为您可能需要一些可以立即为您工作的示例代码,所以我为它工作。

这是我的示例代码。

from datetime import datetime, timedelta
from azure.storage.blob import generate_container_sas, ContainerSasPermissions

account_name = "<your account name>"
account_key = "<your account key>"
container_name = "<your container name, such as `test`>"

# using generate_container_sas
def get_img_url_with_container_sas_token(blob_name):
container_sas_token = generate_container_sas(
account_name=account_name,
container_name=container_name,
account_key=account_key,
permission=ContainerSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1)
)
blob_url_with_container_sas_token = f"https://{account_name}.blob.core.windows.net/{container_name}/{blob_name}?{container_sas_token}"
return blob_url_with_container_sas_token

from azure.storage.blob import generate_blob_sas, BlobSasPermissions

# using generate_blob_sas
def get_img_url_with_blob_sas_token(blob_name):
blob_sas_token = generate_blob_sas(
account_name=account_name,
container_name=container_name,
blob_name=blob_name,
account_key=account_key,
permission=ContainerSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1)
)
blob_url_with_blob_sas_token = f"https://{account_name}.blob.core.windows.net/{container_name}/{blob_name}?{blob_sas_token}"
return blob_url_with_blob_sas_token

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/showimg")
@app.route("/showimg/<blob_name>")
def hello_world(blob_name=None):
img_url_with_sas_token = get_img_url_with_blob_sas_token(blob_name)
# Or
# img_url_with_sas_token = get_img_url_with_container_sas_token(blob_name)
return render_template('showimage.html', img_url_with_sas_token=img_url_with_sas_token)

app.run()

我的 templates/showimage.html 非常简单,如下所示。

<html>
<head>
</head>
<body>
<img src="{{img_url_with_sas_token}}" />
</body>
</html>

我的test容器中有一个名为test_cat2.jpg的图片,我用它来进行测试,所以结果如下图。

enter image description here

关于python-3.x - 从 Azure Blob 存储、Flask Python、HTML/JS 检索并显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59944361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com