abhinav singh
abhinav singh

Reputation: 1104

How to correctly import Azure SDK for Python 3.6

I am creating a flask app that can upload files to my azure blob. When I test the code locally on my python virtualenv, the app works perfectly. When I create a docker container and push the image to Azure App services, container log shows below error:

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. The usual suggestions were to install azure-storage < 0.36, install azure==0.11.1 etc but none of them have solved my problem. 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)

Upvotes: 3

Views: 2113

Answers (1)

Laurent Mazuel
Laurent Mazuel

Reputation: 3546

azure-storage is deprecated, do not use it but the specific version like these three:

Do not use azure as well, since it's just a meta-package that brings other and don't contain code. There is no value to use it if you use directly service specific package like azure-storage-queue (which is the recommendation)

(I work at MS in the SDK team)

Upvotes: 2

Related Questions