View Source Appwrite.Services.Storage (appwrite v0.1.9)

The Storage service allows you to manage your project files. Using the Storage service, you can upload, view, download, and query all your project files.

Each file in the service is granted with read and write permissions to manage who has access to view or edit it. You can also learn more about how to manage your resources permissions.

The preview endpoint allows you to generate preview images for your files. Using the preview endpoint, you can also manipulate the resulting image so that it will fit perfectly inside your app in terms of dimensions, file size, and style. The preview endpoint also allows you to change the resulting image file format for better compression or image quality for better delivery over the network.

Summary

Types

bucket_id()

@type bucket_id() :: String.t()

file_id()

@type file_id() :: String.t()

payload()

@type payload() :: Appwrite.Types.Client.Payload.t()

permissions()

@type permissions() :: [String.t()]

queries()

@type queries() :: [String.t()]

Functions

create_file(bucket_id, file_id, file, permissions \\ nil)

@spec create_file(bucket_id(), file_id() | nil, any(), permissions() | nil) ::
  {:ok, Appwrite.Types.File.t()}
  | {:error, Appwrite.Exceptions.AppwriteException.t()}

Create a new file in a bucket.

Parameters

  • bucket_id (String): The ID of the bucket.
  • file_id (String): Unique ID for the file.
  • file (File): The file to upload.
  • permissions (list of String, optional): Permissions for the file.

Returns

  • {:ok, File.t()} on success.
  • {:error, AppwriteException.t()} on failure.

Usage

Step 1: Create the fileInput.js Hook Navigate to the assets/js directory in your Phoenix project. Create a new file called fileInput.js with the following content:

export default { mounted() { this.el.addEventListener("change", async (e) => {

let file = e.target.files[0];
if (file) {
  let reader = new FileReader();

  reader.onload = (event) => {
    let base64Content = event.target.result.split(",")[1]; // Extract Base64 content
    this.pushEvent("file_selected", {
      name: file.name,
      size: file.size,
      type: file.type,
      content: base64Content, // Send Base64 file content
    });
  };

  reader.readAsDataURL(file); // Convert file to Base64 Data URL
}

}); }, };

Step 2: Initialize the Hook in app.js Open assets/js/app.js. Import and initialize the fileInput hook by adding the following code:

let Hooks = {}; import FileInput from "./fileInput"; Hooks.FileInput = FileInput;

let csrfToken = document .querySelector("meta[name='csrf-token']") .getAttribute("content"); let liveSocket = new LiveSocket("/live", Socket, { longPollFallbackMs: 2500, params: { _csrf_token: csrfToken }, hooks: Hooks, });

Step 3: Use the Hook in Your LiveView Frontend (HTML) Use the FileInput hook in your form input field: <.form phx-submit="file_selected">

</.form>

Handle the file upload event in your LiveView module: Add an event handler for file_selected:

@impl true def handle_event(

"file_selected",
%{"name" => name, "size" => size, "type" => type, "content" => base64_content},
socket

) do {:noreply, assign(socket, :file_content, %{ "name" => name, "size" => size, "type" => type, "data" => base64_content, "lastModified" => DateTime.utc_now() })} end

Add a handler to save the file using your storage service (e.g., Appwrite): @impl true def handle_event("save", _params, socket) do uploaded_file = Appwrite.Services.Storage.create_file(

bucket_id,
nil,
socket.assigns.file_content,
nil

)

end

delete_file(bucket_id, file_id)

@spec delete_file(bucket_id(), file_id()) ::
  {:ok, :deleted} | {:error, Appwrite.Exceptions.AppwriteException.t()}

Delete a file by its unique ID.

Parameters

  • bucket_id (String): The ID of the bucket.
  • file_id (String): The ID of the file.

Returns

  • {:ok, :deleted} on success.
  • {:error, AppwriteException.t()} on failure.

get_file(bucket_id, file_id)

@spec get_file(bucket_id(), file_id()) ::
  {:ok, Appwrite.Types.File.t()}
  | {:error, Appwrite.Exceptions.AppwriteException.t()}

Get a file by its unique ID.

Parameters

  • bucket_id (String): The ID of the bucket.
  • file_id (String): The ID of the file.

Returns

  • {:ok, File.t()} on success.
  • {:error, AppwriteException.t()} on failure.

get_file_download(bucket_id, file_id)

@spec get_file_download(String.t(), String.t()) :: String.t()

Get a file for download.

Retrieves the file content by its unique ID. The response includes a Content-Disposition: attachment header, prompting the browser to download the file.

Parameters

  • bucket_id (String.t): The ID of the bucket.
  • file_id (String.t): The ID of the file.

Returns

  • String.t: The URI for downloading the file.

Raises

  • AppwriteException if parameters are missing or request fails.

get_file_preview(bucket_id, file_id, options \\ [])

@spec get_file_preview(
  String.t(),
  String.t(),
  keyword()
) :: String.t()

Get a file preview.

Generates a preview of an image file. Supports query string arguments for resizing and customization.

Parameters

  • bucket_id (String.t): The ID of the bucket.
  • file_id (String.t): The ID of the file.
  • Optional preview settings (width, height, gravity, quality, etc.).

Returns

  • String.t: The URI for the file preview.

Raises

  • AppwriteException if parameters are missing or invalid.

get_file_view(bucket_id, file_id)

@spec get_file_view(String.t(), String.t()) :: String.t()

Get a file for viewing.

Retrieves file content by its unique ID. Unlike download, it does not include the Content-Disposition: attachment header.

Parameters

  • bucket_id (String.t): The ID of the bucket.
  • file_id (String.t): The ID of the file.

Returns

  • String.t: The URI for viewing the file.

Raises

  • AppwriteException if parameters are missing.

list_files(bucket_id, queries \\ nil, search \\ nil)

@spec list_files(bucket_id(), queries() | nil, String.t() | nil) ::
  {:ok, Appwrite.Types.FileList.t()}
  | {:error, Appwrite.Exceptions.AppwriteException.t()}

List files in a bucket.

Parameters

  • bucket_id (String): The ID of the bucket.
  • queries (list of String, optional): Queries to filter the results.
  • search (String, optional): Search query to filter files.

Returns

  • {:ok, FileList.t()} on success.
  • {:error, AppwriteException.t()} on failure.