Storage
GoFrame provides a storage abstraction used to manage attachments. When running goframe init
, the CLI creates a provider and migrations so attachments can be stored on disk or in Amazon S3.
Choose the implementation through the storage.type
setting in config.yml
:
storage:
type: s3
bucket: "${STORAGE_BUCKET}"
region: "${STORAGE_REGION}"
access_key: "${STORAGE_ACCESS_KEY}"
secret_key: "${STORAGE_SECRET_KEY}"
endpoint: "${STORAGE_ENDPOINT}"
secure: ${STORAGE_SECURE:true}
You can also use disk
storage type in combination with the directory
property to store attachments on the local filesystem.
Interact with the storage via the contracts.Storage
interface:
attachment, err := storage.UploadAttachment(ctx, coretypes.UploadAttachmentOptions{
Filename: "example.png",
Content: file,
})
// or using a multipart.FileHeader
attachment, err := storage.UploadAttachment(ctx, coretypes.UploadAttachmentOptions{
Filename: "example.png",
ContentMultipartFileHeader: header,
})
To download an attachment you can use:
reader, err := storage.DownloadAttachment(ctx, attachment.ID)
if err != nil {
// handle error
}
defer reader.Close()
To serve attachments, register the handler in your router:
router.HandleFunc("/attachments/{id}", storage.AttachmentHandler("id"))
You can imagine having a middleware that checks permissions before serving the attachment.
Last updated on