4
4
<a href =" http://travis-ci.org/filestack/filestack-python " >
5
5
<img src="https://img.shields.io/travis/filestack/filestack-python.svg">
6
6
</a >
7
- <a href =" https://pypi.python.org/pypi/filestack-python/2.3.1 " >
7
+ <a href =" https://pypi.python.org/pypi/filestack-python " >
8
8
<img src="https://img.shields.io/pypi/v/filestack-python.svg">
9
9
</a >
10
+ <img src="https://img.shields.io/pypi/pyversions/filestack-python.svg">
10
11
</p >
11
12
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.
12
13
13
14
## Resources
14
15
15
- * [ API Reference] ( https://filestack.github.io/filestack-python )
16
+ To learn more about this SDK, please visit our API Reference
17
+
18
+ * [ API Reference] ( https://filestack-python.readthedocs.io )
16
19
17
20
## Installing
18
21
@@ -28,49 +31,53 @@ or directly from GitHub
28
31
pip install git+https://github.com/filestack/filestack-python.git
29
32
```
30
33
31
- ## Usage
34
+ ## Quickstart
32
35
33
36
The Filestack SDK allows you to upload and handle filelinks using two main classes: Client and Filelink.
34
37
35
- ### Uploading New File with Client
38
+ ### Uploading files with ` filestack. Client`
36
39
``` python
37
40
from filestack import Client
38
- client = Client(" <YOUR_API_KEY>" )
41
+ client = Client(' <YOUR_API_KEY>' )
39
42
40
- params = {' mimetype' : ' image/png' }
41
- new_filelink = client.upload(filepath = " path/to/file" , params = params)
43
+ new_filelink = client.upload(filepath = ' path/to/file' )
42
44
print (new_filelink.url)
43
45
```
44
- Uploading local files will use Filestack's multipart upload by default. To disable, just set the argument to false.
45
46
46
- ``` python
47
- new_filelink = client.upload(filepath = " path/to/file" , multipart = False )
48
- ```
49
47
#### Uploading files using Filestack Intelligent Ingestion
50
48
To upload files using Filestack Intelligent Ingestion, simply add ` intelligent=True ` argument
51
49
``` python
52
- new_filelink = client.upload(filepath = " path/to/file" , intelligent = True )
50
+ new_filelink = client.upload(filepath = ' path/to/file' , intelligent = True )
53
51
```
54
52
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).
55
53
56
- ### Create Filelink using Existing Handle
54
+ ### Working with Filelinks
55
+ Filelink objects can by created by uploading new files, or by initializing ` filestack.Filelink ` with already existing file handle
57
56
``` python
58
- from filestack import Filelink
59
- new_filelink = Filelink(" <YOUR_HANDLE>" )
57
+ from filestack import Filelink, Client
58
+
59
+ client = Client(' <APIKEY>' )
60
+ filelink = client.upload(filepath = ' path/to/file' )
61
+ filelink.url # 'https://cdn.filestackcontent.com/FILE_HANDLE
62
+
63
+ # work with previously uploaded file
64
+ filelink = Filelink(' FILE_HANDLE' )
60
65
```
61
66
62
67
### Basic Filelink Functions
63
68
64
- With a Filelink, you can download to a local path or get the content of a file. You can also delete or overwrite files if you have security enabled on your account .
69
+ With a Filelink, you can download to a local path or get the content of a file. You can also perform various transformations .
65
70
66
71
``` python
67
72
file_content = new_filelink.get_content()
68
73
69
- response = new_filelink.download(" /path/to/file" )
74
+ size_in_bytes = new_filelink.download(' /path/to/file' )
75
+
76
+ filelink.overwrite(filepath = ' path/to/new/file' )
70
77
71
- filelink.overwrite( filepath = " path/to/new/file " )
78
+ filelink.resize( width = 400 ).flip( )
72
79
73
- response = filelink.delete()
80
+ filelink.delete()
74
81
```
75
82
76
83
### Transformations
@@ -81,7 +88,7 @@ You can chain transformations on both Filelinks and external URLs. Storing trans
81
88
transform = client.transform_external(' http://<SOME_URL>' )
82
89
new_filelink = transform.resize(width = 500 , height = 500 ).flip().enhance().store()
83
90
84
- filelink = Filelink(" <YOUR_HANDLE" > )
91
+ filelink = Filelink(' <YOUR_HANDLE' > )
85
92
new_filelink = filelink.resize(width = 500 , height = 500 ).flip().enhance().store()
86
93
```
87
94
@@ -113,40 +120,55 @@ filelink = av_object.to_filelink()
113
120
114
121
### Security Objects
115
122
116
- Security is set on Client or Filelink classes upon instantiation.
123
+ Security is set on Client or Filelink classes upon instantiation and is used to sign all API calls .
117
124
118
125
``` python
119
- from filestack import security
126
+ from filestack import Security
120
127
121
- json_policy = {" expiry" : 253381964415 }
122
- security = security(json_policy , ' <YOUR_APP_SECRET>' )
123
- client = Client(" <YOUR_API_KEY" , security = security)
128
+ policy = {' expiry' : 253381964415 } # 'expiry' is the only required key
129
+ security = Security(policy , ' <YOUR_APP_SECRET>' )
130
+ client = Client(' <YOUR_API_KEY' , security = security)
124
131
125
132
# new Filelink object inherits security and will use for all calls
126
- new_filelink = client.upload(filepath = " path/to/file" )
133
+ new_filelink = client.upload(filepath = ' path/to/file' )
134
+
135
+ # you can also provide Security objects explicitly for some methods
136
+ size_in_bytes = filelink.download(security = security)
137
+ ```
138
+
139
+ You can also retrieve security details straight from the object:
140
+ ``` python
141
+ >> > policy = {' expiry' : 253381964415 , ' call' : [' read' ]}
142
+ >> > security = Security(policy, ' SECURITY-SECRET' )
143
+ >> > security.policy_b64
144
+ ' eyJjYWxsIjogWyJyZWFkIl0sICJleHBpcnkiOiAyNTMzODE5NjQ0MTV9'
145
+ >> > security.signature
146
+ ' f61fa1effb0638ab5b6e208d5d2fd9343f8557d8a0bf529c6d8542935f77bb3c'
127
147
```
128
148
129
149
### Webhook verification
130
150
131
- You can use ` Client .verify_webhook_signature` method to make sure that the webhooks you receive are sent by Filestack.
151
+ You can use ` filestack.helpers .verify_webhook_signature` method to make sure that the webhooks you receive are sent by Filestack.
132
152
133
153
``` python
134
- from filestack import Client
154
+ from filestack.helpers import verify_webhook_signature
135
155
136
156
# webhook_data is raw content you receive
137
157
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}'
138
158
139
- resp = Client.verify_webhook_signature(
140
- ' <YOUR_WEBHOOK_SECRET>' , webhook_data,
141
- {' FS-Signature' : ' <SIGNATURE-FROM-REQUEST-HEADERS>' , ' FS-Timestamp' : ' <TIMESTAMP-FROM-REQUEST-HEADERS>' }
159
+ result, details = verify_webhook_signature(
160
+ ' <YOUR_WEBHOOK_SECRET>' ,
161
+ webhook_data,
162
+ {
163
+ ' FS-Signature' : ' <SIGNATURE-FROM-REQUEST-HEADERS>' ,
164
+ ' FS-Timestamp' : ' <TIMESTAMP-FROM-REQUEST-HEADERS>'
165
+ }
142
166
)
143
167
144
- if not resp[' valid' ] and resp[' error' ] is None :
145
- raise Exception (' Webhook signature is invalid' )
146
- elif resp[' error' ]:
147
- print (' Please check input params: {} ' .format(resp[' error' ]))
148
- else :
168
+ if result is True :
149
169
print (' Webhook is valid and was generated by Filestack' )
170
+ else :
171
+ raise Exception (details[' error' ])
150
172
```
151
173
152
174
## Versioning
0 commit comments