Skip to content

API Reference

/

Values

File

A models.File points to a file in a SurrealDB bucket, as the SurrealQL value f"bucket:/key" does. It holds the name of the bucket and the key of the file, not the content of the file. This page documents the type and how to send one. Files are an experimental feature of SurrealDB. A server turns the feature on with --allow-experimental files, but the SDK always decodes File values.

Package: github.com/surrealdb/surrealdb.go/v2/pkg/models

Source: pkg/models/file.go

type File struct {
	Bucket string
	Key    string
}
FieldDescription
BucketThe name of the bucket the file is in
KeyThe path of the file in the bucket, starting with a slash

Returns a File for the file at key in bucket. It adds a leading slash to key when key has none.

Syntax
f := models.NewFile(bucket, key)

Returns: File

f := models.NewFile("docs", "report.pdf")
fmt.Println(f.Bucket, f.Key)
Output
docs /report.pdf

Send a File as a query variable. The SDK sends it as its own kind, so any bucket and key work:

results, err := surrealdb.Query[[]byte](ctx, db, "RETURN $file.get()", map[string]any{
	"file": models.NewFile("docs", "report.pdf"),
})

A file that the server returns, such as one stored in a field of a record, decodes into a File, including from any. See Files for the functions that read and write the content of a file.

.String() returns the file as a file string, such as f"docs:/report.pdf". It puts a backslash before each character that is not an ASCII letter or digit, _, -, ., or a / in the key. For example, models.NewFile("docs", "my report.pdf") gives f"docs:/my\ report.pdf". A file string has no escape syntax, so SurrealDB reads the result back as the same file only when no character needed a backslash. Send a File as a variable rather than writing its text into a query.

Was this page helpful?