-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsample.py
More file actions
242 lines (198 loc) · 7.62 KB
/
sample.py
File metadata and controls
242 lines (198 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import base64
import sys
sys.path.append("..")
# #### set your private_key public_key, url_endpoint, url ### ##
private_key = "your_public_api_key"
public_key = "your_private_api_key"
url_endpoint = "https://ik.imagekit.io/your_imagekit_id/"
# dummy image url
url = "https://file-examples.com/wp-content/uploads/2017/10/file_example_JPG_100kB.jpg"
if __name__ == "__main__":
from imagekitio.client import ImageKit
imagekit = ImageKit(
private_key=private_key, public_key=public_key, url_endpoint=url_endpoint,
)
### The signed url generated for this file doesn't work using the Python SDK
upload = imagekit.upload_file(
file=open("sample.jpg", "rb"),
file_name="testing_upload_binary_signed_private.jpg",
options={
"response_fields": ["is_private_file", "tags"],
"is_private_file": False,
"folder" : "/testing-python-folder/",
"tags": ["abc", "def"]
},
)
print("-------------------------------------")
print("Upload with binary")
print("-------------------------------------")
print(upload, end="\n\n")
image_url = imagekit.url(
{
"path": upload['response']['filePath'],
"query_parameters": {"v": "123"},
"transformation": [{"height": "300", "width": "400"}],
"signed": True,
"expire_seconds": 3000,
}
)
print("-------------------------------------")
print("Signed url")
print("-------------------------------------")
print(image_url, end="\n\n")
# URL generation using image path and image hostname
image_url = imagekit.url(
{
"path": "default-image.jpg",
"url_endpoint": url_endpoint,
"transformation": [{"height": "300", "width": "400"}],
}
)
print("-------------------------------------")
print("Url using image path")
print("-------------------------------------")
print(image_url, end="\n\n")
# 2 Using full image URL
image_url = imagekit.url(
{
"src": url_endpoint.rstrip("/") + "/default-image.jpg",
"transformation": [{"height": "300", "width": "400"}],
}
)
print("-------------------------------------")
print("Url using src")
print("-------------------------------------")
print(image_url, end="\n\n")
image_url = imagekit.url(
{
"path": "/default-image.jpg",
"url_endpoint": "https://www.example.com",
"transformation": [{"height": "300", "width": "400"}, {"rotation": 90}],
"transformation_position": "query",
}
)
print("-------------------------------------")
print("Chained transformation")
print("-------------------------------------")
print(image_url, end="\n\n")
image_url = imagekit.url(
{
"src": url_endpoint.rstrip("/") + "/default-image.jpg",
"transformation": [
{
"format": "jpg",
"progressive": "true",
"effect_sharpen": "-",
"effect_contrast": "1",
}
],
}
)
print("-------------------------------------")
print("Sharpening and contrast transformation")
print("-------------------------------------")
print(image_url, end="\n\n")
list_files = imagekit.list_files({"skip": 0, "limit": 5})
bulk_ids = [
list_files["response"][3]["fileId"],
list_files["response"][4]["fileId"],
]
print("-------------------------------------")
print("List files")
print("-------------------------------------")
print(list_files, end="\n\n")
upload = imagekit.upload_file(
file=open("sample.jpg", "rb"),
file_name="testing-binary.jpg",
options={
"response_fields": ["is_private_file", "tags"],
"tags": ["abc", "def"],
"use_unique_file_name": False,
},
)
print("-------------------------------------")
print("Upload with binary")
print("-------------------------------------")
print(upload, end="\n\n")
file_id = upload["response"]["fileId"]
upload = imagekit.upload_file(
file=url,
file_name="testing-url.jpg",
options={
"response_fields": ["is_private_file"],
"is_private_file": False,
"tags": ["abc", "def"],
},
)
image_url = upload["response"]["url"]
print("-------------------------------------")
print("Upload with url")
print("-------------------------------------")
print(upload, end="\n\n")
with open("sample.jpg", mode="rb") as img:
imgstr = base64.b64encode(img.read())
upload_base64 = imagekit.upload_file(
file=imgstr,
file_name="testing-base64.jpg",
options={
"response_fields": ["is_private_file", "metadata", "tags"],
"is_private_file": False,
"tags": ["abc", "def"],
},
)
print("-------------------------------------")
print("Upload with base64")
print("-------------------------------------")
print(upload_base64, end="\n\n")
updated_detail = imagekit.update_file_details(
list_files["response"][0]["fileId"],
{"tags": None, "custom_coordinates": "10,10,100,100"},
)
print("-------------------------------------")
print("Update file details")
print("-------------------------------------")
print(updated_detail, end="\n\n")
details = imagekit.get_file_details(list_files["response"][0]["fileId"])
print("-------------------------------------")
print("Get file details")
print("-------------------------------------")
print(details, end="\n\n")
file_metadata = imagekit.get_file_metadata(list_files["response"][0]["fileId"])
print("-------------------------------------")
print("File metadata")
print("-------------------------------------")
print(file_metadata, end="\n\n")
delete = imagekit.delete_file(list_files["response"][1]["fileId"])
print("-------------------------------------")
print("Delete file")
print("-------------------------------------")
print(delete, end="\n\n")
purge_cache = imagekit.purge_file_cache(file_url=image_url)
print("-------------------------------------")
print("Purge cache")
print("-------------------------------------")
print(purge_cache, end="\n\n")
request_id = purge_cache["response"]["request_id"]
purge_cache_status = imagekit.get_purge_file_cache_status(request_id)
print("-------------------------------------")
print("Cache status")
print("-------------------------------------")
print(purge_cache_status, end="\n\n")
auth_params = imagekit.get_authentication_parameters()
print("-------------------------------------")
print("Auth params")
print("-------------------------------------")
print(auth_params, end="\n\n")
print("-------------------------------------")
print("Phash distance")
print("-------------------------------------")
print(imagekit.phash_distance("f06830ca9f1e3e90", "f06830ca9f1e3e90"), end="\n\n")
print("-------------------------------------")
print("Bulk file delete")
print("-------------------------------------")
print(imagekit.bulk_file_delete(bulk_ids), end="\n\n")
remote_file_url = upload["response"]["url"]
print("-------------------------------------")
print("Get metatdata via url")
print("-------------------------------------")
print(imagekit.get_remote_file_url_metadata(remote_file_url))