{{ message }}
Repository navigation
Expand file tree
/
Copy pathstorage.py
More file actions
847 lines (760 loc) · 33.9 KB
/
Copy pathstorage.py
File metadata and controls
847 lines (760 loc) · 33.9 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
from ..service import Service
from urllib.parse import quote
from typing import Any, Dict, List, Optional, Union
from ..exception import AppwriteException
from appwrite.utils.deprecated import deprecated
from ..models.bucket_list import BucketList
from ..enums.compression import Compression
from ..models.bucket import Bucket
from ..models.file_list import FileList
from ..input_file import InputFile
from ..models.file import File
from ..enums.image_gravity import ImageGravity
from ..enums.image_format import ImageFormat
class Storage(Service):
def __init__(self, client) -> None:
super(Storage, self).__init__(client)
def list_buckets(
self,
queries: Optional[List[str]] = None,
search: Optional[str] = None,
total: Optional[bool] = None,
) -> BucketList:
"""
Get a list of all the storage buckets. You can use the query params to filter your results.
Parameters
----------
queries : Optional[List[str]]
Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of 100 queries are allowed, each 4096 characters long. You may filter on the following attributes: enabled, name, fileSecurity, maximumFileSize, encryption, antivirus, transformations, stripMetadata
search : Optional[str]
Search term to filter your list results. Max length: 256 chars.
total : Optional[bool]
When set to false, the total count returned will be 0 and will not be calculated.
Returns
-------
BucketList
API response as a typed Pydantic model
Raises
------
AppwriteException
If API request fails
"""
api_path = '/storage/buckets'
api_params = {}
if queries is not None:
api_params['queries'] = self._normalize_value(queries)
if search is not None:
api_params['search'] = self._normalize_value(search)
if total is not None:
api_params['total'] = self._normalize_value(total)
response = self.client.call(
'get',
api_path,
{
'X-Appwrite-Project': self.client.get_config('project'),
'accept': 'application/json',
},
api_params,
)
return self._parse_response(response, model=BucketList)
def create_bucket(
self,
bucket_id: str,
name: str,
permissions: Optional[List[str]] = None,
file_security: Optional[bool] = None,
enabled: Optional[bool] = None,
maximum_file_size: Optional[float] = None,
allowed_file_extensions: Optional[List[str]] = None,
compression: Optional[Compression] = None,
encryption: Optional[bool] = None,
antivirus: Optional[bool] = None,
transformations: Optional[bool] = None,
description: Optional[str] = None,
strip_metadata: Optional[bool] = None,
) -> Bucket:
"""
Create a new storage bucket.
Parameters
----------
bucket_id : str
Unique Id. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can't start with a special char. Max length is 36 chars.
name : str
Bucket name
permissions : Optional[List[str]]
An array of permission strings. By default, no user is granted with any permissions. [Learn more about permissions](https://appwrite.io/docs/permissions).
file_security : Optional[bool]
Enables configuring permissions for individual file. A user needs one of file or bucket level permissions to access a file. [Learn more about permissions](https://appwrite.io/docs/permissions).
enabled : Optional[bool]
Is bucket enabled? When set to 'disabled', users cannot access the files in this bucket but Server SDKs with and API key can still access the bucket. No files are lost when this is toggled.
maximum_file_size : Optional[float]
Maximum file size allowed in bytes. Maximum allowed value is 5GB.
allowed_file_extensions : Optional[List[str]]
Allowed file extensions. Maximum of 100 extensions are allowed, each 64 characters long.
compression : Optional[Compression]
Compression algorithm chosen for compression. Can be one of none, [gzip](https://en.wikipedia.org/wiki/Gzip), or [zstd](https://en.wikipedia.org/wiki/Zstd), For file size above 20MB compression is skipped even if it's enabled
encryption : Optional[bool]
Is encryption enabled? For file size above 20MB encryption is skipped even if it's enabled
antivirus : Optional[bool]
Is virus scanning enabled? For file size above 20MB AntiVirus scanning is skipped even if it's enabled
transformations : Optional[bool]
Are image transformations enabled?
description : Optional[str]
Optional description for the bucket.
strip_metadata : Optional[bool]
Remove metadata such as location, device and capture time from uploaded images. The color profile and orientation are kept. For file size above 20MB metadata is not removed even if it's enabled.
Returns
-------
Bucket
API response as a typed Pydantic model
Raises
------
AppwriteException
If API request fails
"""
api_path = '/storage/buckets'
api_params = {}
if bucket_id is None:
raise AppwriteException('Missing required parameter: "bucket_id"')
if name is None:
raise AppwriteException('Missing required parameter: "name"')
api_params['bucketId'] = self._normalize_value(bucket_id)
api_params['name'] = self._normalize_value(name)
if permissions is not None:
api_params['permissions'] = self._normalize_value(permissions)
if file_security is not None:
api_params['fileSecurity'] = self._normalize_value(file_security)
if enabled is not None:
api_params['enabled'] = self._normalize_value(enabled)
if maximum_file_size is not None:
api_params['maximumFileSize'] = self._normalize_value(maximum_file_size)
if allowed_file_extensions is not None:
api_params['allowedFileExtensions'] = self._normalize_value(allowed_file_extensions)
if compression is not None:
api_params['compression'] = self._normalize_value(compression)
if encryption is not None:
api_params['encryption'] = self._normalize_value(encryption)
if antivirus is not None:
api_params['antivirus'] = self._normalize_value(antivirus)
if transformations is not None:
api_params['transformations'] = self._normalize_value(transformations)
if description is not None:
api_params['description'] = self._normalize_value(description)
if strip_metadata is not None:
api_params['stripMetadata'] = self._normalize_value(strip_metadata)
response = self.client.call(
'post',
api_path,
{
'X-Appwrite-Project': self.client.get_config('project'),
'content-type': 'application/json',
'accept': 'application/json',
},
api_params,
)
return self._parse_response(response, model=Bucket)
def get_bucket(
self,
bucket_id: str,
) -> Bucket:
"""
Get a storage bucket by its unique ID. This endpoint response returns a JSON object with the storage bucket metadata.
Parameters
----------
bucket_id : str
Bucket unique ID.
Returns
-------
Bucket
API response as a typed Pydantic model
Raises
------
AppwriteException
If API request fails
"""
api_path = '/storage/buckets/{bucketId}'
api_params = {}
if bucket_id is None or bucket_id == '':
raise AppwriteException('Missing required parameter: "bucket_id"')
api_path = api_path.replace('{bucketId}', str(self._normalize_value(bucket_id)))
response = self.client.call(
'get',
api_path,
{
'X-Appwrite-Project': self.client.get_config('project'),
'accept': 'application/json',
},
api_params,
)
return self._parse_response(response, model=Bucket)
def update_bucket(
self,
bucket_id: str,
name: str,
permissions: Optional[List[str]] = None,
file_security: Optional[bool] = None,
enabled: Optional[bool] = None,
maximum_file_size: Optional[float] = None,
allowed_file_extensions: Optional[List[str]] = None,
compression: Optional[Compression] = None,
encryption: Optional[bool] = None,
antivirus: Optional[bool] = None,
transformations: Optional[bool] = None,
description: Optional[str] = None,
strip_metadata: Optional[bool] = None,
) -> Bucket:
"""
Update a storage bucket by its unique ID.
Parameters
----------
bucket_id : str
Bucket unique ID.
name : str
Bucket name
permissions : Optional[List[str]]
An array of permission strings. By default, the current permissions are inherited. [Learn more about permissions](https://appwrite.io/docs/permissions).
file_security : Optional[bool]
Enables configuring permissions for individual file. A user needs one of file or bucket level permissions to access a file. [Learn more about permissions](https://appwrite.io/docs/permissions).
enabled : Optional[bool]
Is bucket enabled? When set to 'disabled', users cannot access the files in this bucket but Server SDKs with and API key can still access the bucket. No files are lost when this is toggled.
maximum_file_size : Optional[float]
Maximum file size allowed in bytes. Maximum allowed value is 5GB.
allowed_file_extensions : Optional[List[str]]
Allowed file extensions. Maximum of 100 extensions are allowed, each 64 characters long.
compression : Optional[Compression]
Compression algorithm chosen for compression. Can be one of none, [gzip](https://en.wikipedia.org/wiki/Gzip), or [zstd](https://en.wikipedia.org/wiki/Zstd), For file size above 20MB compression is skipped even if it's enabled
encryption : Optional[bool]
Is encryption enabled? For file size above 20MB encryption is skipped even if it's enabled
antivirus : Optional[bool]
Is virus scanning enabled? For file size above 20MB AntiVirus scanning is skipped even if it's enabled
transformations : Optional[bool]
Are image transformations enabled?
description : Optional[str]
Optional description for the bucket.
strip_metadata : Optional[bool]
Remove metadata such as location, device and capture time from uploaded images. The color profile and orientation are kept. For file size above 20MB metadata is not removed even if it's enabled. By default, the current setting is kept.
Returns
-------
Bucket
API response as a typed Pydantic model
Raises
------
AppwriteException
If API request fails
"""
api_path = '/storage/buckets/{bucketId}'
api_params = {}
if bucket_id is None or bucket_id == '':
raise AppwriteException('Missing required parameter: "bucket_id"')
if name is None:
raise AppwriteException('Missing required parameter: "name"')
api_path = api_path.replace('{bucketId}', str(self._normalize_value(bucket_id)))
api_params['name'] = self._normalize_value(name)
if permissions is not None:
api_params['permissions'] = self._normalize_value(permissions)
if file_security is not None:
api_params['fileSecurity'] = self._normalize_value(file_security)
if enabled is not None:
api_params['enabled'] = self._normalize_value(enabled)
if maximum_file_size is not None:
api_params['maximumFileSize'] = self._normalize_value(maximum_file_size)
if allowed_file_extensions is not None:
api_params['allowedFileExtensions'] = self._normalize_value(allowed_file_extensions)
if compression is not None:
api_params['compression'] = self._normalize_value(compression)
if encryption is not None:
api_params['encryption'] = self._normalize_value(encryption)
if antivirus is not None:
api_params['antivirus'] = self._normalize_value(antivirus)
if transformations is not None:
api_params['transformations'] = self._normalize_value(transformations)
if description is not None:
api_params['description'] = self._normalize_value(description)
if strip_metadata is not None:
api_params['stripMetadata'] = self._normalize_value(strip_metadata)
response = self.client.call(
'put',
api_path,
{
'X-Appwrite-Project': self.client.get_config('project'),
'content-type': 'application/json',
'accept': 'application/json',
},
api_params,
)
return self._parse_response(response, model=Bucket)
def delete_bucket(
self,
bucket_id: str,
) -> Dict[str, Any]:
"""
Delete a storage bucket by its unique ID.
Parameters
----------
bucket_id : str
Bucket unique ID.
Returns
-------
Dict[str, Any]
API response as a dictionary
Raises
------
AppwriteException
If API request fails
"""
api_path = '/storage/buckets/{bucketId}'
api_params = {}
if bucket_id is None or bucket_id == '':
raise AppwriteException('Missing required parameter: "bucket_id"')
api_path = api_path.replace('{bucketId}', str(self._normalize_value(bucket_id)))
response = self.client.call(
'delete',
api_path,
{
'X-Appwrite-Project': self.client.get_config('project'),
'content-type': 'application/json',
'accept': 'application/json',
},
api_params,
)
return response
def list_files(
self,
bucket_id: str,
queries: Optional[List[str]] = None,
search: Optional[str] = None,
total: Optional[bool] = None,
) -> FileList:
"""
Get a list of all the user files. You can use the query params to filter your results.
Parameters
----------
bucket_id : str
Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).
queries : Optional[List[str]]
Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of 100 queries are allowed, each 4096 characters long. You may filter on the following attributes: name, folder, signature, mimeType, sizeOriginal, chunksTotal, chunksUploaded
search : Optional[str]
Search term to filter your list results. Max length: 256 chars.
total : Optional[bool]
When set to false, the total count returned will be 0 and will not be calculated.
Returns
-------
FileList
API response as a typed Pydantic model
Raises
------
AppwriteException
If API request fails
"""
api_path = '/storage/buckets/{bucketId}/files'
api_params = {}
if bucket_id is None or bucket_id == '':
raise AppwriteException('Missing required parameter: "bucket_id"')
api_path = api_path.replace('{bucketId}', str(self._normalize_value(bucket_id)))
if queries is not None:
api_params['queries'] = self._normalize_value(queries)
if search is not None:
api_params['search'] = self._normalize_value(search)
if total is not None:
api_params['total'] = self._normalize_value(total)
response = self.client.call(
'get',
api_path,
{
'X-Appwrite-Project': self.client.get_config('project'),
'accept': 'application/json',
},
api_params,
)
return self._parse_response(response, model=FileList)
def create_file(
self,
bucket_id: str,
file_id: str,
file: InputFile,
permissions: Optional[List[str]] = None,
folder: Optional[str] = None,
on_progress=None,
) -> File:
"""
Create a new file. Before using this route, you should create a new bucket resource using either a [server integration](https://appwrite.io/docs/server/storage#storageCreateBucket) API or directly from your Appwrite console.
Larger files should be uploaded using multiple requests with the [content-range](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range) header to send a partial request with a maximum supported chunk of `5MB`. The `content-range` header values should always be in bytes.
When the first request is sent, the server will return the **File** object, and the subsequent part request must include the file's **id** in `x-appwrite-id` header to allow the server to know that the partial upload is for the existing file and not for a new one.
If you're creating a new file using one of the Appwrite SDKs, all the chunking logic will be managed by the SDK internally.
Parameters
----------
bucket_id : str
Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).
file_id : str
File ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can't start with a special char. Max length is 36 chars.
file : InputFile
Binary file. Appwrite SDKs provide helpers to handle file input. [Learn about file input](https://appwrite.io/docs/products/storage/upload-download#input-file).
permissions : Optional[List[str]]
An array of permission strings. By default, only the current user is granted all permissions. [Learn more about permissions](https://appwrite.io/docs/permissions).
folder : Optional[str]
Virtual folder to place the file in, for example "photos/2026". Nest folders with `/`. Defaults to the bucket root.
on_progress : callable, optional
Optional callback for upload progress
Returns
-------
File
API response as a typed Pydantic model
Raises
------
AppwriteException
If API request fails
"""
api_path = '/storage/buckets/{bucketId}/files'
api_params = {}
if bucket_id is None or bucket_id == '':
raise AppwriteException('Missing required parameter: "bucket_id"')
if file_id is None:
raise AppwriteException('Missing required parameter: "file_id"')
if file is None:
raise AppwriteException('Missing required parameter: "file"')
api_path = api_path.replace('{bucketId}', str(self._normalize_value(bucket_id)))
api_params['fileId'] = self._normalize_value(file_id)
api_params['file'] = self._normalize_value(file)
if permissions is not None:
api_params['permissions'] = self._normalize_value(permissions)
if folder is not None:
api_params['folder'] = self._normalize_value(folder)
param_name = 'file'
upload_id = ''
upload_id = file_id
response = self.client.chunked_upload(
api_path,
{
'X-Appwrite-Project': self.client.get_config('project'),
'content-type': 'multipart/form-data',
'accept': 'application/json',
},
api_params,
param_name,
on_progress,
upload_id,
method='post',
)
return self._parse_response(response, model=File)
def get_file(
self,
bucket_id: str,
file_id: str,
) -> File:
"""
Get a file by its unique ID. This endpoint response returns a JSON object with the file metadata.
Parameters
----------
bucket_id : str
Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).
file_id : str
File ID.
Returns
-------
File
API response as a typed Pydantic model
Raises
------
AppwriteException
If API request fails
"""
api_path = '/storage/buckets/{bucketId}/files/{fileId}'
api_params = {}
if bucket_id is None or bucket_id == '':
raise AppwriteException('Missing required parameter: "bucket_id"')
if file_id is None or file_id == '':
raise AppwriteException('Missing required parameter: "file_id"')
api_path = api_path.replace('{bucketId}', str(self._normalize_value(bucket_id)))
api_path = api_path.replace('{fileId}', str(self._normalize_value(file_id)))
response = self.client.call(
'get',
api_path,
{
'X-Appwrite-Project': self.client.get_config('project'),
'accept': 'application/json',
},
api_params,
)
return self._parse_response(response, model=File)
def update_file(
self,
bucket_id: str,
file_id: str,
name: Optional[str] = None,
permissions: Optional[List[str]] = None,
) -> File:
"""
Update a file by its unique ID. Only users with write permissions have access to update this resource.
Parameters
----------
bucket_id : str
Bucket unique ID.
file_id : str
File ID.
name : Optional[str]
File name.
permissions : Optional[List[str]]
An array of permission strings. By default, the current permissions are inherited. [Learn more about permissions](https://appwrite.io/docs/permissions).
Returns
-------
File
API response as a typed Pydantic model
Raises
------
AppwriteException
If API request fails
"""
api_path = '/storage/buckets/{bucketId}/files/{fileId}'
api_params = {}
if bucket_id is None or bucket_id == '':
raise AppwriteException('Missing required parameter: "bucket_id"')
if file_id is None or file_id == '':
raise AppwriteException('Missing required parameter: "file_id"')
api_path = api_path.replace('{bucketId}', str(self._normalize_value(bucket_id)))
api_path = api_path.replace('{fileId}', str(self._normalize_value(file_id)))
if name is not None:
api_params['name'] = self._normalize_value(name)
if permissions is not None:
api_params['permissions'] = self._normalize_value(permissions)
response = self.client.call(
'put',
api_path,
{
'X-Appwrite-Project': self.client.get_config('project'),
'content-type': 'application/json',
'accept': 'application/json',
},
api_params,
)
return self._parse_response(response, model=File)
def delete_file(
self,
bucket_id: str,
file_id: str,
) -> Dict[str, Any]:
"""
Delete a file by its unique ID. Only users with write permissions have access to delete this resource.
Parameters
----------
bucket_id : str
Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).
file_id : str
File ID.
Returns
-------
Dict[str, Any]
API response as a dictionary
Raises
------
AppwriteException
If API request fails
"""
api_path = '/storage/buckets/{bucketId}/files/{fileId}'
api_params = {}
if bucket_id is None or bucket_id == '':
raise AppwriteException('Missing required parameter: "bucket_id"')
if file_id is None or file_id == '':
raise AppwriteException('Missing required parameter: "file_id"')
api_path = api_path.replace('{bucketId}', str(self._normalize_value(bucket_id)))
api_path = api_path.replace('{fileId}', str(self._normalize_value(file_id)))
response = self.client.call(
'delete',
api_path,
{
'X-Appwrite-Project': self.client.get_config('project'),
'content-type': 'application/json',
'accept': 'application/json',
},
api_params,
)
return response
def get_file_download(
self,
bucket_id: str,
file_id: str,
token: Optional[str] = None,
) -> bytes:
"""
Get a file content by its unique ID. The endpoint response return with a 'Content-Disposition: attachment' header that tells the browser to start downloading the file to user downloads directory.
Parameters
----------
bucket_id : str
Storage bucket ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).
file_id : str
File ID.
token : Optional[str]
File token for accessing this file.
Returns
-------
bytes
Response as bytes
Raises
------
AppwriteException
If API request fails
"""
api_path = '/storage/buckets/{bucketId}/files/{fileId}/download'
api_params = {}
if bucket_id is None or bucket_id == '':
raise AppwriteException('Missing required parameter: "bucket_id"')
if file_id is None or file_id == '':
raise AppwriteException('Missing required parameter: "file_id"')
api_path = api_path.replace('{bucketId}', str(self._normalize_value(bucket_id)))
api_path = api_path.replace('{fileId}', str(self._normalize_value(file_id)))
if token is not None:
api_params['token'] = self._normalize_value(token)
response = self.client.call(
'get',
api_path,
{
'X-Appwrite-Project': self.client.get_config('project'),
'accept': '*/*',
},
api_params,
)
return response
def get_file_preview(
self,
bucket_id: str,
file_id: str,
width: Optional[float] = None,
height: Optional[float] = None,
gravity: Optional[ImageGravity] = None,
quality: Optional[float] = None,
border_width: Optional[float] = None,
border_color: Optional[str] = None,
border_radius: Optional[float] = None,
opacity: Optional[float] = None,
rotation: Optional[float] = None,
background: Optional[str] = None,
output: Optional[ImageFormat] = None,
token: Optional[str] = None,
) -> bytes:
"""
Get a file preview image. Currently, this method supports preview for image files (jpg, png, and gif), other supported formats, like pdf, docs, slides, and spreadsheets, will return the file icon image. You can also pass query string arguments for cutting and resizing your preview image. Preview is supported only for image files smaller than 10MB.
Parameters
----------
bucket_id : str
Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).
file_id : str
File ID
width : Optional[float]
Resize preview image width, Pass an integer between 0 to 4000.
height : Optional[float]
Resize preview image height, Pass an integer between 0 to 4000.
gravity : Optional[ImageGravity]
Image crop gravity. Can be one of auto,center,top-left,top,top-right,left,right,bottom-left,bottom,bottom-right
quality : Optional[float]
Preview image quality. Pass an integer between 0 to 100. Defaults to keep existing image quality.
border_width : Optional[float]
Preview image border in pixels. Pass an integer between 0 to 100. Defaults to 0.
border_color : Optional[str]
Preview image border color. Use a valid HEX color, no # is needed for prefix.
border_radius : Optional[float]
Preview image border radius in pixels. Pass an integer between 0 to 4000.
opacity : Optional[float]
Preview image opacity. Only works with images having an alpha channel (like png). Pass a number between 0 to 1.
rotation : Optional[float]
Preview image rotation in degrees. Pass an integer between -360 and 360.
background : Optional[str]
Preview image background color. Only works with transparent images (png). Use a valid HEX color, no # is needed for prefix.
output : Optional[ImageFormat]
Output format type (jpeg, jpg, png, gif and webp).
token : Optional[str]
File token for accessing this file.
Returns
-------
bytes
Response as bytes
Raises
------
AppwriteException
If API request fails
"""
api_path = '/storage/buckets/{bucketId}/files/{fileId}/preview'
api_params = {}
if bucket_id is None or bucket_id == '':
raise AppwriteException('Missing required parameter: "bucket_id"')
if file_id is None or file_id == '':
raise AppwriteException('Missing required parameter: "file_id"')
api_path = api_path.replace('{bucketId}', str(self._normalize_value(bucket_id)))
api_path = api_path.replace('{fileId}', str(self._normalize_value(file_id)))
if width is not None:
api_params['width'] = self._normalize_value(width)
if height is not None:
api_params['height'] = self._normalize_value(height)
if gravity is not None:
api_params['gravity'] = self._normalize_value(gravity)
if quality is not None:
api_params['quality'] = self._normalize_value(quality)
if border_width is not None:
api_params['borderWidth'] = self._normalize_value(border_width)
if border_color is not None:
api_params['borderColor'] = self._normalize_value(border_color)
if border_radius is not None:
api_params['borderRadius'] = self._normalize_value(border_radius)
if opacity is not None:
api_params['opacity'] = self._normalize_value(opacity)
if rotation is not None:
api_params['rotation'] = self._normalize_value(rotation)
if background is not None:
api_params['background'] = self._normalize_value(background)
if output is not None:
api_params['output'] = self._normalize_value(output)
if token is not None:
api_params['token'] = self._normalize_value(token)
response = self.client.call(
'get',
api_path,
{
'X-Appwrite-Project': self.client.get_config('project'),
'accept': 'image/*',
},
api_params,
)
return response
def get_file_view(
self,
bucket_id: str,
file_id: str,
token: Optional[str] = None,
) -> bytes:
"""
Get a file content by its unique ID. This endpoint is similar to the download method but returns with no 'Content-Disposition: attachment' header.
Parameters
----------
bucket_id : str
Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).
file_id : str
File ID.
token : Optional[str]
File token for accessing this file.
Returns
-------
bytes
Response as bytes
Raises
------
AppwriteException
If API request fails
"""
api_path = '/storage/buckets/{bucketId}/files/{fileId}/view'
api_params = {}
if bucket_id is None or bucket_id == '':
raise AppwriteException('Missing required parameter: "bucket_id"')
if file_id is None or file_id == '':
raise AppwriteException('Missing required parameter: "file_id"')
api_path = api_path.replace('{bucketId}', str(self._normalize_value(bucket_id)))
api_path = api_path.replace('{fileId}', str(self._normalize_value(file_id)))
if token is not None:
api_params['token'] = self._normalize_value(token)
response = self.client.call(
'get',
api_path,
{
'X-Appwrite-Project': self.client.get_config('project'),
'accept': '*/*',
},
api_params,
)
return response
You can’t perform that action at this time.
