Class: Appwrite::Storage

Inherits:
Service show all
Defined in:
lib/appwrite/services/storage.rb

Instance Method Summary collapse

Methods inherited from Service

#initialize

Constructor Details

This class inherits a constructor from Appwrite::Service

Instance Method Details

#create_bucket(bucket_id:, name:, permission:, read: nil, write: nil, enabled: nil, maximum_file_size: nil, allowed_file_extensions: nil, encryption: nil, antivirus: nil) ⇒ Bucket

Create a new storage bucket.

Parameters:

  • bucket_id (string)

    Unique Id. Choose your own unique ID or pass the string ‘unique()` to auto generate it. 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 (string)

    Bucket name

  • permission (string)

    Permissions type model to use for reading files in this bucket. You can use bucket-level permission set once on the bucket using the ‘read` and `write` params, or you can set file-level permission where each file read and write params will decide who has access to read and write to each file individually. [learn more about permissions](/docs/permissions) and get a full list of available permissions.

  • read (array) (defaults to: nil)

    An array of strings with read permissions. By default no user is granted with any read permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.

  • write (array) (defaults to: nil)

    An array of strings with write permissions. By default no user is granted with any write permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.

  • enabled (boolean) (defaults to: nil)

    Is bucket enabled?

  • maximum_file_size (number) (defaults to: nil)

    Maximum file size allowed in bytes. Maximum allowed value is 30MB. For self-hosted setups you can change the max limit by changing the ‘_APP_STORAGE_LIMIT` environment variable. [Learn more about storage environment variables](docs/environment-variables#storage)

  • allowed_file_extensions (array) (defaults to: nil)

    Allowed file extensions. Maximum of 100 extensions are allowed, each 64 characters long.

  • encryption (boolean) (defaults to: nil)

    Is encryption enabled? For file size above 20MB encryption is skipped even if it's enabled

  • antivirus (boolean) (defaults to: nil)

    Is virus scanning enabled? For file size above 20MB AntiVirus scanning is skipped even if it's enabled

Returns:

  • (Bucket)


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
# File 'lib/appwrite/services/storage.rb', line 56

def create_bucket(bucket_id:, name:, permission:, read: nil, write: nil, enabled: nil, maximum_file_size: nil, allowed_file_extensions: nil, encryption: nil, antivirus: nil)
    if bucket_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "bucketId"')
    end

    if name.nil?
        raise Appwrite::Exception.new('Missing required parameter: "name"')
    end

    if permission.nil?
        raise Appwrite::Exception.new('Missing required parameter: "permission"')
    end

    path = '/storage/buckets'

    params = {
        bucketId: bucket_id,
        name: name,
        permission: permission,
        read: read,
        write: write,
        enabled: enabled,
        maximumFileSize: maximum_file_size,
        allowedFileExtensions: allowed_file_extensions,
        encryption: encryption,
        antivirus: antivirus,
    }

    headers = {
        "content-type": 'application/json',
    }

    @client.call(
        method: 'POST',
        path: path,
        headers: headers,
        params: params,
        response_type: Models::Bucket
    )
end

#create_file(bucket_id:, file_id:, file:, read: nil, write: nil, on_progress: nil) ⇒ File

Create a new file. Before using this route, you should create a new bucket resource using either a [server integration](/docs/server/database#storageCreateBucket) API or directly from your Appwrite console.

Larger files should be uploaded using multiple requests with the [content-range](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 (string)

    Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](/docs/server/storage#createBucket).

  • file_id (string)

    File ID. Choose your own unique ID or pass the string "unique()" to auto generate it. 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 (file)

    Binary file.

  • read (array) (defaults to: nil)

    An array of strings with read permissions. By default only the current user is granted with read permissions. [learn more about permissions](appwrite.io/docs/permissions) and get a full list of available permissions.

  • write (array) (defaults to: nil)

    An array of strings with write permissions. By default only the current user is granted with write permissions. [learn more about permissions](appwrite.io/docs/permissions) and get a full list of available permissions.

Returns:



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
# File 'lib/appwrite/services/storage.rb', line 279

def create_file(bucket_id:, file_id:, file:, read: nil, write: nil, on_progress: nil)
    if bucket_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "bucketId"')
    end

    if file_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "fileId"')
    end

    if file.nil?
        raise Appwrite::Exception.new('Missing required parameter: "file"')
    end

    path = '/storage/buckets/{bucketId}/files'
        .gsub('{bucketId}', bucket_id)

    params = {
        fileId: file_id,
        file: file,
        read: read,
        write: write,
    }

    headers = {
        "content-type": 'multipart/form-data',
    }

    id_param_name = "fileId"
    param_name = 'file'

    @client.chunked_upload(
        path: path,
        headers: headers,
        params: params,
        param_name: param_name,
        id_param_name: id_param_name,
        on_progress: on_progress,
        response_type: Models::File
    )
end

#delete_bucket(bucket_id:) ⇒ Object

Delete a storage bucket by its unique ID.

Parameters:

  • bucket_id (string)

    Bucket unique ID.

Returns:



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/appwrite/services/storage.rb', line 187

def delete_bucket(bucket_id:)
    if bucket_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "bucketId"')
    end

    path = '/storage/buckets/{bucketId}'
        .gsub('{bucketId}', bucket_id)

    params = {
    }

    headers = {
        "content-type": 'application/json',
    }

    @client.call(
        method: 'DELETE',
        path: path,
        headers: headers,
        params: params,
    )
end

#delete_file(bucket_id:, file_id:) ⇒ Object

Delete a file by its unique ID. Only users with write permissions have access to delete this resource.

Parameters:

  • bucket_id (string)

    Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](/docs/server/storage#createBucket).

  • file_id (string)

    File ID.

Returns:



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
# File 'lib/appwrite/services/storage.rb', line 403

def delete_file(bucket_id:, file_id:)
    if bucket_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "bucketId"')
    end

    if file_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "fileId"')
    end

    path = '/storage/buckets/{bucketId}/files/{fileId}'
        .gsub('{bucketId}', bucket_id)
        .gsub('{fileId}', file_id)

    params = {
    }

    headers = {
        "content-type": 'application/json',
    }

    @client.call(
        method: 'DELETE',
        path: path,
        headers: headers,
        params: params,
    )
end

#get_bucket(bucket_id:) ⇒ Bucket

Get a storage bucket by its unique ID. This endpoint response returns a JSON object with the storage bucket metadata.

Parameters:

  • bucket_id (string)

    Bucket unique ID.

Returns:

  • (Bucket)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/appwrite/services/storage.rb', line 103

def get_bucket(bucket_id:)
    if bucket_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "bucketId"')
    end

    path = '/storage/buckets/{bucketId}'
        .gsub('{bucketId}', bucket_id)

    params = {
    }

    headers = {
        "content-type": 'application/json',
    }

    @client.call(
        method: 'GET',
        path: path,
        headers: headers,
        params: params,
        response_type: Models::Bucket
    )
end

#get_file(bucket_id:, file_id:) ⇒ File

Get a file by its unique ID. This endpoint response returns a JSON object with the file metadata.

Parameters:

  • bucket_id (string)

    Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](/docs/server/storage#createBucket).

  • file_id (string)

    File ID.

Returns:



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
# File 'lib/appwrite/services/storage.rb', line 327

def get_file(bucket_id:, file_id:)
    if bucket_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "bucketId"')
    end

    if file_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "fileId"')
    end

    path = '/storage/buckets/{bucketId}/files/{fileId}'
        .gsub('{bucketId}', bucket_id)
        .gsub('{fileId}', file_id)

    params = {
    }

    headers = {
        "content-type": 'application/json',
    }

    @client.call(
        method: 'GET',
        path: path,
        headers: headers,
        params: params,
        response_type: Models::File
    )
end

#get_file_download(bucket_id:, file_id:) ⇒ Object

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 (string)

    Storage bucket ID. You can create a new storage bucket using the Storage service [server integration](/docs/server/storage#createBucket).

  • file_id (string)

    File ID.

Returns:



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
# File 'lib/appwrite/services/storage.rb', line 439

def get_file_download(bucket_id:, file_id:)
    if bucket_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "bucketId"')
    end

    if file_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "fileId"')
    end

    path = '/storage/buckets/{bucketId}/files/{fileId}/download'
        .gsub('{bucketId}', bucket_id)
        .gsub('{fileId}', file_id)

    params = {
    }

    headers = {
        "content-type": 'application/json',
    }

    @client.call(
        method: 'GET',
        path: path,
        headers: headers,
        params: params,
    )
end

#get_file_preview(bucket_id:, file_id:, width: nil, height: nil, gravity: nil, quality: nil, border_width: nil, border_color: nil, border_radius: nil, opacity: nil, rotation: nil, background: nil, output: nil) ⇒ Object

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 (string)

    Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](/docs/server/storage#createBucket).

  • file_id (string)

    File ID

  • width (number) (defaults to: nil)

    Resize preview image width, Pass an integer between 0 to 4000.

  • height (number) (defaults to: nil)

    Resize preview image height, Pass an integer between 0 to 4000.

  • gravity (string) (defaults to: nil)

    Image crop gravity. Can be one of center,top-left,top,top-right,left,right,bottom-left,bottom,bottom-right

  • quality (number) (defaults to: nil)

    Preview image quality. Pass an integer between 0 to 100. Defaults to 100.

  • border_width (number) (defaults to: nil)

    Preview image border in pixels. Pass an integer between 0 to 100. Defaults to 0.

  • border_color (string) (defaults to: nil)

    Preview image border color. Use a valid HEX color, no # is needed for prefix.

  • border_radius (number) (defaults to: nil)

    Preview image border radius in pixels. Pass an integer between 0 to 4000.

  • opacity (number) (defaults to: nil)

    Preview image opacity. Only works with images having an alpha channel (like png). Pass a number between 0 to 1.

  • rotation (number) (defaults to: nil)

    Preview image rotation in degrees. Pass an integer between -360 and 360.

  • background (string) (defaults to: nil)

    Preview image background color. Only works with transparent images (png). Use a valid HEX color, no # is needed for prefix.

  • output (string) (defaults to: nil)

    Output format type (jpeg, jpg, png, gif and webp).

Returns:



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
# File 'lib/appwrite/services/storage.rb', line 488

def get_file_preview(bucket_id:, file_id:, width: nil, height: nil, gravity: nil, quality: nil, border_width: nil, border_color: nil, border_radius: nil, opacity: nil, rotation: nil, background: nil, output: nil)
    if bucket_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "bucketId"')
    end

    if file_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "fileId"')
    end

    path = '/storage/buckets/{bucketId}/files/{fileId}/preview'
        .gsub('{bucketId}', bucket_id)
        .gsub('{fileId}', file_id)

    params = {
        width: width,
        height: height,
        gravity: gravity,
        quality: quality,
        borderWidth: border_width,
        borderColor: border_color,
        borderRadius: border_radius,
        opacity: opacity,
        rotation: rotation,
        background: background,
        output: output,
    }

    headers = {
        "content-type": 'application/json',
    }

    @client.call(
        method: 'GET',
        path: path,
        headers: headers,
        params: params,
    )
end

#get_file_view(bucket_id:, file_id:) ⇒ Object

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 (string)

    Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](/docs/server/storage#createBucket).

  • file_id (string)

    File ID.

Returns:



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
# File 'lib/appwrite/services/storage.rb', line 535

def get_file_view(bucket_id:, file_id:)
    if bucket_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "bucketId"')
    end

    if file_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "fileId"')
    end

    path = '/storage/buckets/{bucketId}/files/{fileId}/view'
        .gsub('{bucketId}', bucket_id)
        .gsub('{fileId}', file_id)

    params = {
    }

    headers = {
        "content-type": 'application/json',
    }

    @client.call(
        method: 'GET',
        path: path,
        headers: headers,
        params: params,
    )
end

#list_buckets(search: nil, limit: nil, offset: nil, cursor: nil, cursor_direction: nil, order_type: nil) ⇒ BucketList

Get a list of all the storage buckets. You can use the query params to filter your results.

Parameters:

  • search (string) (defaults to: nil)

    Search term to filter your list results. Max length: 256 chars.

  • limit (number) (defaults to: nil)

    Results limit value. By default will return maximum 25 results. Maximum of 100 results allowed per request.

  • offset (number) (defaults to: nil)

    Results offset. The default value is 0. Use this param to manage pagination.

  • cursor (string) (defaults to: nil)

    ID of the bucket used as the starting point for the query, excluding the bucket itself. Should be used for efficient pagination when working with large sets of data.

  • cursor_direction (string) (defaults to: nil)

    Direction of the cursor.

  • order_type (string) (defaults to: nil)

    Order result by ASC or DESC order.

Returns:

  • (BucketList)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/appwrite/services/storage.rb', line 17

def list_buckets(search: nil, limit: nil, offset: nil, cursor: nil, cursor_direction: nil, order_type: nil)
    path = '/storage/buckets'

    params = {
        search: search,
        limit: limit,
        offset: offset,
        cursor: cursor,
        cursorDirection: cursor_direction,
        orderType: order_type,
    }

    headers = {
        "content-type": 'application/json',
    }

    @client.call(
        method: 'GET',
        path: path,
        headers: headers,
        params: params,
        response_type: Models::BucketList
    )
end

#list_files(bucket_id:, search: nil, limit: nil, offset: nil, cursor: nil, cursor_direction: nil, order_type: nil) ⇒ FileList

Get a list of all the user files. You can use the query params to filter your results. On admin mode, this endpoint will return a list of all of the project’s files. [Learn more about different API modes](/docs/admin).

Parameters:

  • bucket_id (string)

    Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](/docs/server/storage#createBucket).

  • search (string) (defaults to: nil)

    Search term to filter your list results. Max length: 256 chars.

  • limit (number) (defaults to: nil)

    Maximum number of files to return in response. By default will return maximum 25 results. Maximum of 100 results allowed per request.

  • offset (number) (defaults to: nil)

    Offset value. The default value is 0. Use this param to manage pagination. [learn more about pagination](appwrite.io/docs/pagination)

  • cursor (string) (defaults to: nil)

    ID of the file used as the starting point for the query, excluding the file itself. Should be used for efficient pagination when working with large sets of data. [learn more about pagination](appwrite.io/docs/pagination)

  • cursor_direction (string) (defaults to: nil)

    Direction of the cursor.

  • order_type (string) (defaults to: nil)

    Order result by ASC or DESC order.

Returns:

  • (FileList)


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
# File 'lib/appwrite/services/storage.rb', line 223

def list_files(bucket_id:, search: nil, limit: nil, offset: nil, cursor: nil, cursor_direction: nil, order_type: nil)
    if bucket_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "bucketId"')
    end

    path = '/storage/buckets/{bucketId}/files'
        .gsub('{bucketId}', bucket_id)

    params = {
        search: search,
        limit: limit,
        offset: offset,
        cursor: cursor,
        cursorDirection: cursor_direction,
        orderType: order_type,
    }

    headers = {
        "content-type": 'application/json',
    }

    @client.call(
        method: 'GET',
        path: path,
        headers: headers,
        params: params,
        response_type: Models::FileList
    )
end

#update_bucket(bucket_id:, name:, permission:, read: nil, write: nil, enabled: nil, maximum_file_size: nil, allowed_file_extensions: nil, encryption: nil, antivirus: nil) ⇒ Bucket

Update a storage bucket by its unique ID.

Parameters:

  • bucket_id (string)

    Bucket unique ID.

  • name (string)

    Bucket name

  • permission (string)

    Permissions type model to use for reading files in this bucket. You can use bucket-level permission set once on the bucket using the ‘read` and `write` params, or you can set file-level permission where each file read and write params will decide who has access to read and write to each file individually. [learn more about permissions](/docs/permissions) and get a full list of available permissions.

  • read (array) (defaults to: nil)

    An array of strings with read permissions. By default inherits the existing read permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.

  • write (array) (defaults to: nil)

    An array of strings with write permissions. By default inherits the existing write permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.

  • enabled (boolean) (defaults to: nil)

    Is bucket enabled?

  • maximum_file_size (number) (defaults to: nil)

    Maximum file size allowed in bytes. Maximum allowed value is 30MB. For self hosted version you can change the limit by changing _APP_STORAGE_LIMIT environment variable. [Learn more about storage environment variables](docs/environment-variables#storage)

  • allowed_file_extensions (array) (defaults to: nil)

    Allowed file extensions. Maximum of 100 extensions are allowed, each 64 characters long.

  • encryption (boolean) (defaults to: nil)

    Is encryption enabled? For file size above 20MB encryption is skipped even if it's enabled

  • antivirus (boolean) (defaults to: nil)

    Is virus scanning enabled? For file size above 20MB AntiVirus scanning is skipped even if it's enabled

Returns:

  • (Bucket)


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
# File 'lib/appwrite/services/storage.rb', line 141

def update_bucket(bucket_id:, name:, permission:, read: nil, write: nil, enabled: nil, maximum_file_size: nil, allowed_file_extensions: nil, encryption: nil, antivirus: nil)
    if bucket_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "bucketId"')
    end

    if name.nil?
        raise Appwrite::Exception.new('Missing required parameter: "name"')
    end

    if permission.nil?
        raise Appwrite::Exception.new('Missing required parameter: "permission"')
    end

    path = '/storage/buckets/{bucketId}'
        .gsub('{bucketId}', bucket_id)

    params = {
        name: name,
        permission: permission,
        read: read,
        write: write,
        enabled: enabled,
        maximumFileSize: maximum_file_size,
        allowedFileExtensions: allowed_file_extensions,
        encryption: encryption,
        antivirus: antivirus,
    }

    headers = {
        "content-type": 'application/json',
    }

    @client.call(
        method: 'PUT',
        path: path,
        headers: headers,
        params: params,
        response_type: Models::Bucket
    )
end

#update_file(bucket_id:, file_id:, read: nil, write: nil) ⇒ File

Update a file by its unique ID. Only users with write permissions have access to update this resource.

Parameters:

  • bucket_id (string)

    Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](/docs/server/storage#createBucket).

  • file_id (string)

    File unique ID.

  • read (array) (defaults to: nil)

    An array of strings with read permissions. By default no user is granted with any read permissions. [learn more about permissions](appwrite.io/docs/permissions) and get a full list of available permissions.

  • write (array) (defaults to: nil)

    An array of strings with write permissions. By default no user is granted with any write permissions. [learn more about permissions](appwrite.io/docs/permissions) and get a full list of available permissions.

Returns:



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
# File 'lib/appwrite/services/storage.rb', line 365

def update_file(bucket_id:, file_id:, read: nil, write: nil)
    if bucket_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "bucketId"')
    end

    if file_id.nil?
        raise Appwrite::Exception.new('Missing required parameter: "fileId"')
    end

    path = '/storage/buckets/{bucketId}/files/{fileId}'
        .gsub('{bucketId}', bucket_id)
        .gsub('{fileId}', file_id)

    params = {
        read: read,
        write: write,
    }

    headers = {
        "content-type": 'application/json',
    }

    @client.call(
        method: 'PUT',
        path: path,
        headers: headers,
        params: params,
        response_type: Models::File
    )
end