[英]How to correctly import Azure SDK for Python 3.6
I am creating a flask app that can upload files to my azure blob.我正在创建一个烧瓶应用程序,可以将文件上传到我的 azure blob。 When I test the code locally on my python virtualenv, the app works perfectly.当我在 python virtualenv 上本地测试代码时,该应用程序运行良好。 When I create a docker container and push the image to Azure App services, container log shows below error:当我创建一个 docker 容器并将图像推送到 Azure App 服务时,容器日志显示以下错误:
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 ***
I have tried multiple solutions mentioned on Github issues and other similar stackoverflow queries.我已经尝试了 Github 问题和其他类似的 stackoverflow 查询中提到的多种解决方案。 The usual suggestions were to install azure-storage < 0.36, install azure==0.11.1 etc but none of them have solved my problem.通常的建议是安装 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 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
Can anyone help me understand the cause of this issue.谁能帮我理解这个问题的原因。
Edit: Adding my code for as requested in one of the comments:编辑:根据评论之一的要求添加我的代码:
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
is deprecated, do not use it but the specific version like these three: azure-storage
已弃用,请勿使用它,而是使用以下三个特定版本:
Do not use azure
as well, since it's just a meta-package that brings other and don't contain code.也不要使用azure
,因为它只是一个带有其他的元包,不包含代码。 There is no value to use it if you use directly service specific package like azure-storage-queue
(which is the recommendation)如果您直接使用特定于服务的包,如azure-storage-queue
(这是推荐的),则没有使用它的价值
(I work at MS in the SDK team) (我在 MS 的 SDK 团队工作)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.