Method: Appwrite::Storage#create_file

Defined in:
lib/appwrite/services/storage.rb

#create_file(bucket_id:, file_id:, file:, permissions: 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](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](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](appwrite.io/docs/server/storage#createBucket).

  • file_id (String)

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

    Binary file. Appwrite SDKs provide helpers to handle file input. [Learn about file input](appwrite.io/docs/products/storage/upload-download#input-file).

  • permissions (Array) (defaults to: nil)

    An array of permission strings. By default, only the current user is granted all permissions. [Learn more about permissions](appwrite.io/docs/permissions).

Returns:

  • (File)


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

def create_file(bucket_id:, file_id:, file:, permissions: nil, on_progress: nil)
    api_path = '/storage/buckets/{bucketId}/files'
        .gsub('{bucketId}', bucket_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

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

    api_params = {
        fileId: file_id,
        file: file,
        permissions: permissions,
    }
    
    api_headers = {
        "content-type": 'multipart/form-data',
    }

    id_param_name = "fileId"
    param_name = 'file'

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