WeaviateEx.Types.Blob (WeaviateEx v0.7.4)

View Source

Binary/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

t()

@type t() :: %WeaviateEx.Types.Blob{data: binary() | nil}

Functions

decode(encoded)

@spec decode(String.t()) :: {:ok, binary()} | :error

Decode base64 string to binary.

Examples

iex> Blob.decode("SGVsbG8sIFdvcmxkIQ==")
{:ok, "Hello, World!"}

iex> Blob.decode("invalid!!!")
:error

decode!(encoded)

@spec decode!(String.t()) :: binary()

Decode base64 string, raising on error.

Examples

iex> Blob.decode!("SGVsbG8sIFdvcmxkIQ==")
"Hello, World!"

decode_to_file(encoded, path)

@spec decode_to_file(String.t(), Path.t()) :: :ok | {:error, term()}

Write decoded blob to file.

Examples

:ok = Blob.decode_to_file(encoded, "/path/to/output.bin")

encode(data)

@spec encode(binary()) :: String.t()

Encode binary data to base64 string for Weaviate.

Examples

iex> Blob.encode("Hello, World!")
"SGVsbG8sIFdvcmxkIQ=="

encode_file(path)

@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!(path)

@spec encode_file!(Path.t()) :: String.t()

Encode file contents, raising on error.

Examples

encoded = Blob.encode_file!("/path/to/file.bin")

from_base64(encoded)

@spec from_base64(String.t()) :: {:ok, t()} | :error

Create a Blob from base64-encoded string.

Examples

iex> Blob.from_base64("SGVsbG8sIFdvcmxkIQ==")
{:ok, %Blob{data: "Hello, World!"}}

from_file(path)

@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")

from_file!(path)

@spec from_file!(Path.t()) :: t()

Create a Blob from a file, raising on error.

Examples

blob = Blob.from_file!("/path/to/image.jpg")

new(data)

@spec new(binary()) :: t()

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>>}

to_base64(blob)

@spec to_base64(t()) :: String.t() | nil

Convert Blob to base64-encoded string for Weaviate API.

Examples

iex> Blob.new("Hello, World!") |> Blob.to_base64()
"SGVsbG8sIFdvcmxkIQ=="