Storage
The storage package selects the provider from CLOUD: aws uses Amazon S3
through the AWS SDK for Go v2, with multipart upload and download of large
objects, while gcp and firebase use Google Cloud Storage.
Initialize
Configure credentials through the provider's standard mechanism, then:
colibri.InitializeApp()
storage.Initialize()
CLOUD=none does not create a storage provider.
Upload a form file
In v0.2.6, UploadFile receives *multipart.File:
file, header, err := ctx.FormFile("file")
if err != nil {
ctx.ErrorResponse(http.StatusBadRequest, err)
return
}
defer file.Close()
location, err := storage.UploadFile(
ctx.Context(),
"documents",
header.Filename,
&file,
)
Download and remove
file, err := storage.DownloadFile(ctx, "documents", objectKey)
if err != nil {
return err
}
defer file.Close()
content, err := io.ReadAll(file)
The returned value is a temporary *os.File; always close it.
err := storage.DeleteFile(ctx, "documents", objectKey)
The v0.2.6 upload API does not accept a generic reader. The bucket must already exist, and the application identity needs permission for each operation.
Metrics
With metrics enabled, each operation records storage.operation and
storage.operation.duration, with the operation (upload, download, or
delete) and result attributes, and storage.transferred, with the size of
uploaded and downloaded files in bytes. Object keys and buckets never become
metric attributes.