WeaviateEx.Types.Blob (WeaviateEx v0.7.4)
View SourceBinary/Blob data handling for Weaviate.
Weaviate stores blobs as base64-encoded strings. This module provides a struct for blob data and utilities for encoding/decoding.
Struct Usage
The Blob struct wraps binary data for automatic serialization:
# Create from binary data
blob = Blob.new(<<binary_data>>)
# Use in object properties - automatically serialized to base64
WeaviateEx.Objects.create("MyCollection", %{
properties: %{image: blob}
})Utility Functions
# Encode binary data directly
encoded = Blob.encode(<<binary_data>>)
# Encode file
{:ok, encoded} = Blob.encode_file("/path/to/image.jpg")
# Decode
{:ok, binary} = Blob.decode(encoded)
# Decode to file
:ok = Blob.decode_to_file(encoded, "/path/to/output.jpg")
Summary
Functions
Decode base64 string to binary.
Decode base64 string, raising on error.
Write decoded blob to file.
Encode binary data to base64 string for Weaviate.
Encode file contents to base64 string.
Encode file contents, raising on error.
Create a Blob from base64-encoded string.
Create a Blob from a file.
Create a Blob from a file, raising on error.
Create a new Blob from binary data.
Convert Blob to base64-encoded string for Weaviate API.
Types
@type t() :: %WeaviateEx.Types.Blob{data: binary() | nil}
Functions
Decode base64 string to binary.
Examples
iex> Blob.decode("SGVsbG8sIFdvcmxkIQ==")
{:ok, "Hello, World!"}
iex> Blob.decode("invalid!!!")
:error
Decode base64 string, raising on error.
Examples
iex> Blob.decode!("SGVsbG8sIFdvcmxkIQ==")
"Hello, World!"
Write decoded blob to file.
Examples
:ok = Blob.decode_to_file(encoded, "/path/to/output.bin")
Encode binary data to base64 string for Weaviate.
Examples
iex> Blob.encode("Hello, World!")
"SGVsbG8sIFdvcmxkIQ=="
@spec encode_file(Path.t()) :: {:ok, String.t()} | {:error, File.posix()}
Encode file contents to base64 string.
Examples
{:ok, encoded} = Blob.encode_file("/path/to/file.bin")
Encode file contents, raising on error.
Examples
encoded = Blob.encode_file!("/path/to/file.bin")
Create a Blob from base64-encoded string.
Examples
iex> Blob.from_base64("SGVsbG8sIFdvcmxkIQ==")
{:ok, %Blob{data: "Hello, World!"}}
@spec from_file(Path.t()) :: {:ok, t()} | {:error, File.posix()}
Create a Blob from a file.
Examples
{:ok, blob} = Blob.from_file("/path/to/image.jpg")
Create a Blob from a file, raising on error.
Examples
blob = Blob.from_file!("/path/to/image.jpg")
Create a new Blob from binary data.
Examples
iex> blob = Blob.new("Hello, World!")
%Blob{data: "Hello, World!"}
iex> blob = Blob.new(<<1, 2, 3, 4, 5>>)
%Blob{data: <<1, 2, 3, 4, 5>>}
Convert Blob to base64-encoded string for Weaviate API.
Examples
iex> Blob.new("Hello, World!") |> Blob.to_base64()
"SGVsbG8sIFdvcmxkIQ=="