簡體   English   中英

如何正確導入適用於 Python 3.6 的 Azure SDK

[英]How to correctly import Azure SDK for Python 3.6

我正在創建一個燒瓶應用程序,可以將文件上傳到我的 azure blob。 當我在 python virtualenv 上本地測試代碼時,該應用程序運行良好。 當我創建一個 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 ***

我已經嘗試了 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已棄用,請勿使用它,而是使用以下三個特定版本:

也不要使用azure ,因為它只是一個帶有其他的元包,不包含代碼。 如果您直接使用特定於服務的包,如azure-storage-queue (這是推薦的),則沒有使用它的價值

(我在 MS 的 SDK 團隊工作)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM