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
Functions
Create a new file in a bucket.
Delete a file by its unique ID.
Get a file by its unique ID.
Get a file for download.
Get a file preview.
Get a file for viewing.
List files in a bucket.
Types
@type bucket_id() :: String.t()
@type file_id() :: String.t()
@type payload() :: Appwrite.Types.Client.Payload.t()
@type permissions() :: [String.t()]
@type queries() :: [String.t()]
Functions
@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
@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.
@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 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 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 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.
@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.