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
Definition
type File struct {
Bucket string
Key string
}| Field | Description |
|---|---|
Bucket | The name of the bucket the file is in |
Key | The path of the file in the bucket, starting with a slash |
Constructor
NewFile
Returns a File for the file at key in bucket. It adds a leading slash to key when key has none.
f := models.NewFile(bucket, key)Returns: File
f := models.NewFile("docs", "report.pdf")
fmt.Println(f.Bucket, f.Key)docs /report.pdfFiles in queries
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.
Text form
.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.
See also
Value types for how values convert
Files for the SurrealQL kind