Giter Club home page Giter Club logo

flask-azure-storage's Introduction

Flask-Azure-Storage PyPI version

A Flask extension that provides integration with Azure Storage

Table of Contents

Install

pip install Flask-Azure-Storage

Usage

Set the account credentials in your app.config:

AZURE_STORAGE_ACCOUNT_NAME = "your-account-name"
AZURE_STORAGE_ACCOUNT_KEY = "your-account-key"

Initialize the extension:

from flask import Flask
from flask.ext.azure_storage import FlaskAzureStorage

app = Flask(__name__)
azure_storage = FlaskAzureStorage(app)

Or, if you follow the Flask application factory pattern:

from flask import Flask
from flask.ext.azure_storage import FlaskAzureStorage

azure_storage = FlaskAzureStorage()

def create_app(config):
    app = Flask(__name__)
    app.config.from_object(config)
    # initialize azure storage on the app within create_app()
    azure_storage.init_app(app)

From the azure_storage object you can now access any of the following classes:

Attribute Class
azure_storage.account azure.storage.cloudstorageaccount.CloudStorageAccount
azure_storage.block_blob_service azure.storage.blob.blockblobservice.BlockBlobService
azure_storage.page_blob_service azure.storage.blob.pageblobservice.PageBlobService
azure_storage.append_blob_service azure.storage.blob.appendblobservice.AppendBlobService
azure_storage.queue_service azure.storage.queue.queueservice.QueueService
azure_storage.table_service azure.storage.table.tableservice.TableService
azure_storage.file_service azure.storage.file.fileservice.FileService

Examples

Create container

azure_storage.block_blob_service.create_container('container-name')

Delete container

azure_storage.block_blob_service.delete_container('container-name')

Upload a file

from azure.storage.blob import ContentSettings
azure_storage.block_blob_service.create_blob_from_path(container_name='container-name', blob_name='uploaded-file-name', file_path='/path/to/your/file.png', content_settings=ContentSettings(content_type='image'))

Delete a file

azure_storage.block_blob_service.delete_blob('container-name', 'uploaded-file-name')

Check if file exists

azure_storage.block_blob_service.exists('container-name', 'uploaded-file-name')

More examples

There are plenty more things you can do. For more examples, check out the Azure Storage SDK for Python samples

Seamless integration with Flask's static assets ('static' folder)

Automatically upload the static assets associated with a Flask application to Azure Storage.

You don't need to manually change your url_for calls, Flask-Azure-Storage will automatically target Azure where you call url_for('static', ...) in your Jinja themes.

This feature is based on flask-s3, intending to implement similar functionality based on the Azure Storage service. It is still under development so please report any issues.

To upload all your static files first set the following parameters in the app.config:

AZURE_STORAGE_ACCOUNT_NAME = "your-account-name"
AZURE_STORAGE_ACCOUNT_KEY = "your-account-key"
AZURE_STORAGE_CONTAINER_NAME = "your-container-name"  # make sure the container is created. Refer to the previous examples or to the Azure admin panel
AZURE_STORAGE_DOMAIN = 'your-account-base-domain'

Then you can call the method create_all from the python interpreter:

>>> from flask import current_app
>>> from flask.ext.azure_storage import create_all
>>> create_all(current_app)

Or, a better choice (if you use something like FLask-Script):

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)

@manager.command
def deploy_azure():
    from flask.ext.azure_storage import create_all
    create_all(app)

So now it is possible to simply call python manage.py deploy_azure to upload your assets.

flask-azure-storage's People

Contributors

alejoar avatar

Watchers

James Cloos avatar Alberto Galera avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.