Documentation
¶
Overview ¶
package file provides functions that can be used to retrieve files from local and remote locations.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get retrieves a file from these locations (in order):
- Local disk
- HTTP or HTTPS URL
- AWS S3
If a file is found, then it is saved as a temporary local file and the name is returned. The caller is responsible for removing files when they are no longer needed; files should be removed even if an error occurs.
Example (Http) ¶
package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/brexhq/substation/internal/file"
)
func main() {
location := "https://example.com"
// a local copy of the HTTP body is created and must be removed when it's no longer needed, regardless of errors
path, err := file.Get(context.TODO(), location)
defer os.Remove(path)
if err != nil {
// handle err
panic(err)
}
f, err := os.Open(path)
if err != nil {
// handle err
panic(err)
}
defer f.Close()
buf := make([]byte, 16)
if _, err = f.Read(buf); err != nil {
// handle err
panic(err)
}
prefix := strings.HasPrefix(strings.ToUpper(string(buf)), "<!DOCTYPE")
fmt.Println(prefix)
}
Output: true
Example (Local) ¶
package main
import (
"context"
"fmt"
"io"
"os"
"github.com/brexhq/substation/internal/file"
)
func main() {
// temp file is used to simulate an open file and must be removed after the test completes
temp, _ := os.CreateTemp("", "substation")
defer os.Remove(temp.Name())
defer temp.Close()
_, _ = temp.Write([]byte("foo\nbar\nbaz"))
// a local copy of the file is created and must be removed when it's no longer needed, regardless of errors
path, err := file.Get(context.TODO(), temp.Name())
defer os.Remove(path)
if err != nil {
// handle err
panic(err)
}
f, err := os.Open(path)
if err != nil {
// handle err
panic(err)
}
defer f.Close()
buf, err := io.ReadAll(f)
if err != nil {
// handle err
panic(err)
}
fmt.Println(string(buf))
}
Output: foo bar baz
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.