Client
Configure the unified storage client and call provider-independent file operations.
Options
files.Options configures a Client.
type Options struct {
Adapter Adapter
Prefix string
ReadOnly bool
Timeout time.Duration
Retries *RetryOptions
Hooks Hooks
Middleware []Middleware
}Construct a client
adapter := memory.New(memory.Options{})
client, err := files.New(files.Options{
Adapter: adapter,
Prefix: "tenants/acme",
Timeout: 5 * time.Second,
Retries: &files.RetryOptions{Max: 2},
})
if err != nil {
return err
}
fmt.Println(client.Prefix())
// Output: tenants/acmeRead adapter metadata
fmt.Println(client.Adapter().Name())
fmt.Println(client.Capabilities().RangeRead)
fmt.Printf("%T\n", client.Raw())Raw returns the provider-native client or adapter data. For S3, it returns the AWS S3 client.
File handles
File binds a key to a handle with the same operations.
profile := client.File("profiles/ada.txt")
_, err := profile.Upload(ctx, files.StringBody("Ada"), files.UploadOptions{})
if err != nil {
return err
}
stored, err := profile.Download(ctx, files.DownloadOptions{})
if err != nil {
return err
}
text, err := stored.Text(ctx)
if err != nil {
return err
}
fmt.Println(text)
// Output: AdaThe handle supports Upload, Download, Head, Exists, Delete, URL, SignedUploadURL, CopyTo, CopyFrom, MoveTo, and MoveFrom.