Giter VIP home page Giter VIP logo

osm1n / filestack-python Goto Github PK

View Code? Open in Web Editor NEW

This project forked from filestack/filestack-python

0.0 1.0 0.0 8.49 MB

Official Python SDK for Filestack - API and content management system that makes it easy to add powerful file uploading and transformation capabilities to any web or mobile application.

Home Page: https://www.filestack.com

License: Apache License 2.0

Python 100.00%

filestack-python's Introduction

Filestack Python

This is the official Python SDK for Filestack - API and content management system that makes it easy to add powerful file uploading and transformation capabilities to any web or mobile application.

Resources

To learn more about this SDK, please visit our API Reference

Installing

Install filestack with pip

pip install filestack-python

or directly from GitHub

pip install git+https://github.com/filestack/filestack-python.git

Quickstart

The Filestack SDK allows you to upload and handle filelinks using two main classes: Client and Filelink.

Uploading files with filestack.Client

from filestack import Client
client = Client('<YOUR_API_KEY>')

new_filelink = client.upload(filepath='path/to/file')
print(new_filelink.url)

Uploading files using Filestack Intelligent Ingestion

To upload files using Filestack Intelligent Ingestion, simply add intelligent=True argument

new_filelink = client.upload(filepath='path/to/file', intelligent=True)

FII always uses multipart uploads. In case of network issues, it will dynamically split file parts into smaller chunks (sacrificing upload speed in favour of upload reliability).

Working with Filelinks

Filelink objects can by created by uploading new files, or by initializing filestack.Filelink with already existing file handle

from filestack import Filelink, Client

client = Client('<APIKEY>')
filelink = client.upload(filepath='path/to/file')
filelink.url  # 'https://cdn.filestackcontent.com/FILE_HANDLE

# work with previously uploaded file
filelink = Filelink('FILE_HANDLE')

Basic Filelink Functions

With a Filelink, you can download to a local path or get the content of a file. You can also perform various transformations.

file_content = new_filelink.get_content()

size_in_bytes = new_filelink.download('/path/to/file')

filelink.overwrite(filepath='path/to/new/file')

filelink.resize(width=400).flip()

filelink.delete()

Transformations

You can chain transformations on both Filelinks and external URLs. Storing transformations will return a new Filelink object.

transform = client.transform_external('http://<SOME_URL>')
new_filelink = transform.resize(width=500, height=500).flip().enhance().store()

filelink = Filelink('<YOUR_HANDLE'>)
new_filelink = filelink.resize(width=500, height=500).flip().enhance().store()

You can also retrieve the transformation url at any point.

transform_candidate = client.transform_external('http://<SOME_URL>')
transform = transform_candidate.resize(width=500, height=500).flip().enhance()
print(transform.url)

Audio/Video Convert

Audio and video conversion works just like any transformation, except it returns an instance of class AudioVisual, which allows you to check the status of your video conversion, as well as get its UUID and timestamp.

av_object = filelink.av_convert(width=100, height=100)
while (av_object.status != 'completed'):
    print(av_object.status)
    print(av_object.uuid)
    print(av_object.timestamp)

The status property makes a call to the API to check its current status, and you can call to_filelink() once video is complete (this function checks its status first and will fail if not completed yet).

filelink = av_object.to_filelink()

Security Objects

Security is set on Client or Filelink classes upon instantiation and is used to sign all API calls.

from filestack import Security

policy = {'expiry': 253381964415}  # 'expiry' is the only required key
security = Security(policy, '<YOUR_APP_SECRET>')
client = Client('<YOUR_API_KEY', security=security)

# new Filelink object inherits security and will use for all calls
new_filelink = client.upload(filepath='path/to/file')

# you can also provide Security objects explicitly for some methods
size_in_bytes = filelink.download(security=security)

You can also retrieve security details straight from the object:

>>> policy = {'expiry': 253381964415, 'call': ['read']}
>>> security = Security(policy, 'SECURITY-SECRET')
>>> security.policy_b64
'eyJjYWxsIjogWyJyZWFkIl0sICJleHBpcnkiOiAyNTMzODE5NjQ0MTV9'
>>> security.signature
'f61fa1effb0638ab5b6e208d5d2fd9343f8557d8a0bf529c6d8542935f77bb3c'

Webhook verification

You can use filestack.helpers.verify_webhook_signature method to make sure that the webhooks you receive are sent by Filestack.

from filestack.helpers import verify_webhook_signature

# webhook_data is raw content you receive
webhook_data = b'{"action": "fp.upload", "text": {"container": "some-bucket", "url": "https://cdn.filestackcontent.com/Handle", "filename": "filename.png", "client": "Computer", "key": "key_filename.png", "type": "image/png", "size": 1000000}, "id": 50006}'

result, details = verify_webhook_signature(
    '<YOUR_WEBHOOK_SECRET>',
    webhook_data,
    {
      'FS-Signature': '<SIGNATURE-FROM-REQUEST-HEADERS>',
      'FS-Timestamp': '<TIMESTAMP-FROM-REQUEST-HEADERS>'
    }
)

if result is True:
    print('Webhook is valid and was generated by Filestack')
else:
    raise Exception(details['error'])

Versioning

Filestack Python SDK follows the Semantic Versioning.

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.