我正在创建一个 Flask 应用程序,可以将文件上传到我的 azure blob。当我在 python virtualenv 上本地测试代码时,该应用程序运行良好。当我创建 Docker 容器并将镜像推送到 Azure 应用程序服务时,容器日志显示以下错误:
2019-01-12T08:02:55.541803195Z Python version: 3.6.5 (default, Aug 22 2018, 14:20:40) [GCC 6.4.0]
2019-01-12T08:02:55.593272447Z *** Python threads support is disabled. You can enable it with --enable-threads ***
2019-01-12T08:02:55.604159596Z Python main interpreter initialized at 0x557d000b7f40
2019-01-12T08:02:55.604196797Z uWSGI running as root, you can use --uid/--gid/--chroot options
2019-01-12T08:02:55.604302601Z *** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
2019-01-12T08:02:55.604306901Z your server socket listen backlog is limited to 100 connections
2019-01-12T08:02:55.604310901Z your mercy for graceful operations on workers is 60 seconds
2019-01-12T08:02:55.604314901Z mapped 1239640 bytes (1210 KB) for 16 cores
2019-01-12T08:02:55.604318801Z *** Operational MODE: preforking ***
2019-01-12T08:02:56.273272972Z Traceback (most recent call last):
2019-01-12T08:02:56.273720386Z File "./main.py", line 4, in <module>
2019-01-12T08:02:56.274117299Z from azure.storage.blob import BlockBlobService
2019-01-12T08:02:56.274378307Z ModuleNotFoundError: No module named 'azure'
2019-01-12T08:02:56.274618915Z unable to load app 0 (mountpoint='') (callable not found or import error)
2019-01-12T08:02:56.274863423Z *** no app loaded. GAME OVER ***
我已经尝试了 Github 问题和其他类似的 stackoverflow 查询中提到的多种解决方案。通常的建议是安装 azure-storage < 0.36、安装 azure==0.11.1 等,但它们都没有解决我的问题。 https://github.com/Microsoft/AzureNotebooks/issues/460 https://github.com/Azure/azure-sdk-for-python/issues/3623
#azure==0.11.1
#azure==2.0.0
click==6.7
Flask==0.12.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
Werkzeug==0.14.1
#azure-common==1.1.3
#azure-nspkg==1.0.0
azure-storage==0.31.0
futures==3.0.5
python-dateutil==2.5.3
requests>=2.20.0
six==1.10.0
谁能帮我理解这个问题的原因。
编辑:根据评论之一的要求添加我的代码:
import os
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename
from azure.storage.blob import BlockBlobService
from azure.storage import CloudStorageAccount
from azure.storage.blob import ContentSettings
import string, random, requests
import traceback
import mimetypes
app = Flask(__name__, instance_relative_config=True)
account = 'fileupoader' # Azure account name
key = 'some_key' # Azure Storage account access key
container = 'files'
blob_service = BlockBlobService(account_name=account, account_key=key)
#print (blob_service)
@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))
except Exception as e:
print (str(e))
pass
ref = 'https://'+ account + '.dfs.core.windows.net/' + container + '/' + f.filename
print (ref)
return '''
<!doctype html>
<title>File Link</title>
<h1>Uploaded File Link</h1>
<p>''' + ref + '''</p>
<img src="'''+ ref +'''">
'''
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
'''
def id_generator(size=32, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
if __name__ == '__main__':
app.run(debug=True)
最佳答案
azure-storage
已弃用,不要使用它,但具体版本如下三个:
- https://pypi.org/project/azure-storage-queue/
- https://pypi.org/project/azure-storage-file/
- https://pypi.org/project/azure-storage-blob/
也不要使用azure
,因为它只是一个元包,带来了其他内容并且不包含代码。如果您直接使用服务特定的包,例如azure-storage-queue
(这是推荐)
(我在 MS SDK 团队工作)
关于python - 如何正确导入 Azure SDK for Python 3.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54157875/