Download link generator for snapshots
All checks were successful
continuous-integration/drone/push Build is passing

Three days expiration
This commit is contained in:
Adam Štrauch 2021-11-03 22:32:38 +01:00
parent 66476df2bc
commit 872826c0b1
Signed by: cx
GPG Key ID: 018304FFA8988F8D
5 changed files with 85 additions and 26 deletions

View File

@ -1,6 +1,7 @@
package drivers package drivers
import ( import (
"errors"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
@ -83,3 +84,8 @@ func (f FSDriver) List(prefix string) ([]string, error) {
func (f FSDriver) Delete(key string) error { func (f FSDriver) Delete(key string) error {
return os.Remove(path.Join(f.Path, key)) return os.Remove(path.Join(f.Path, key))
} }
// GetDownloadLink is not implemented in this driver
func (f FSDriver) GetDownloadLink(key string) (string, error) {
return "", errors.New("not implemented")
}

View File

@ -5,11 +5,14 @@ import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"time"
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials" "github.com/minio/minio-go/v7/pkg/credentials"
) )
const s3LinkExpiration = 72 // in hours
// S3Driver provides basic interface for S3 storage compatible with Driver interface // S3Driver provides basic interface for S3 storage compatible with Driver interface
type S3Driver struct { type S3Driver struct {
// S3 storage related things // S3 storage related things
@ -155,3 +158,19 @@ func (s S3Driver) Delete(key string) error {
return nil return nil
} }
// GetDownloadLink returns URL of object with given key. That URL can be used to download the object.
func (s S3Driver) GetDownloadLink(key string) (string, error) {
client, err := s.getMinioClient()
if err != nil {
return "", fmt.Errorf("getting minio client error: %v", err)
}
expiry := time.Second * s3LinkExpiration * 60 * 60 // 1 day.
presignedURL, err := client.PresignedPutObject(context.Background(), s.Bucket, key, expiry)
if err != nil {
return "", fmt.Errorf("generating presign URL error: %v", err)
}
return presignedURL.String(), nil
}

View File

@ -19,4 +19,7 @@ type DriverInterface interface {
// Delete deletes one object // Delete deletes one object
Delete(key string) error Delete(key string) error
// GetDownloadLink returns URL of object with given key. That URL can be used to download the object.
GetDownloadLink(key string) (string, error)
} }

View File

@ -331,6 +331,16 @@ func (s *SnapshotProcessor) GetSnapshot(key string) (Snapshot, error) {
return snapshot, nil return snapshot, nil
} }
// GetDownloadLink returns an URL for given snapshot
func (s *SnapshotProcessor) GetDownloadLink(key string) (string, error) {
link, err := s.GetDownloadLink(key)
if err != nil {
return link, err
}
return link, nil
}
// DeleteSnapshot delete's one snapshot // DeleteSnapshot delete's one snapshot
func (s *SnapshotProcessor) DeleteSnapshot(key string) error { func (s *SnapshotProcessor) DeleteSnapshot(key string) error {
err := s.Driver.Delete(key) err := s.Driver.Delete(key)

View File

@ -40,32 +40,33 @@ func _messageHandler(m *nats.Msg) error {
fmt.Printf("Received a message: %v\n", message) fmt.Printf("Received a message: %v\n", message)
eventHandlerMap := map[string](func(m *nats.Msg, message *RequestMessage) error){ eventHandlerMap := map[string](func(m *nats.Msg, message *RequestMessage) error){
"list": listEventHandler, "list": listEventHandler,
"get": getEventHandler, "get": getEventHandler,
"create": createEventHandler, "create": createEventHandler,
"register": registerEventHandler, "register": registerEventHandler,
"update": updateEventHandler, "update": updateEventHandler,
"delete": deleteEventHandler, "delete": deleteEventHandler,
"stop": stopEventHandler, "stop": stopEventHandler,
"start": startEventHandler, "start": startEventHandler,
"restart": restartEventHandler, "restart": restartEventHandler,
"update_keys": updateKeysEventHandler, "update_keys": updateKeysEventHandler,
"set_password": setPasswordEventHandler, "set_password": setPasswordEventHandler,
"processes": processesEventHandler, "processes": processesEventHandler,
"enable_tech": enableTechEventHandler, "enable_tech": enableTechEventHandler,
"rebuild": rebuildEventHandler, "rebuild": rebuildEventHandler,
"add_label": addLabelEventHandler, "add_label": addLabelEventHandler,
"remove_label": removeLabelEventHandler, "remove_label": removeLabelEventHandler,
"list_orphans": listOrphansEventHandler, "list_orphans": listOrphansEventHandler,
"node": getNoteEventHandler, "node": getNoteEventHandler,
"create_snapshot": createSnapshotEventHandler, "create_snapshot": createSnapshotEventHandler,
"restore_from_snapshot": restoreFromSnapshotEventHandler, "restore_from_snapshot": restoreFromSnapshotEventHandler,
"list_snapshots": listSnapshotsEventHandler, "list_snapshots": listSnapshotsEventHandler,
"list_apps_snapshots": listAppsSnapshotsEventHandler, "list_apps_snapshots": listAppsSnapshotsEventHandler,
"list_snapshots_by_label": listSnapshotsByLabelEventHandler, "list_snapshots_by_label": listSnapshotsByLabelEventHandler,
"get_snapshot": getSnapshotEventHandler, "get_snapshot": getSnapshotEventHandler,
"delete_snapshot": deleteSnapshotEventHandler, "get_snapshot_download_link": getSnapshotDownloadLinkEventHandler,
"delete_app_snapshots": deleteAppSnapshotsEventHandler, "delete_snapshot": deleteSnapshotEventHandler,
"delete_app_snapshots": deleteAppSnapshotsEventHandler,
} }
if eventHandler, ok := eventHandlerMap[message.Type]; ok { if eventHandler, ok := eventHandlerMap[message.Type]; ok {
@ -1000,6 +1001,26 @@ func getSnapshotEventHandler(m *nats.Msg, message *RequestMessage) error {
return nil return nil
} }
/*
getSnapshotDownloadLinkEventHandler return URL that can be used to download snapshot
Payload: string with a snapshot name (key)
Response: string with the URL
*/
func getSnapshotDownloadLinkEventHandler(m *nats.Msg, message *RequestMessage) error {
link, err := snapshotProcessor.GetDownloadLink(message.Payload)
if err != nil {
return errorReplyFormater(m, "backend error", err)
}
err = m.Respond([]byte(link))
if err != nil {
log.Println("ERROR: get snapshot:", err.Error())
}
return nil
}
/* /*
deleteSnapshotEventHandler delete a single snapshot. This is not bound to application name. deleteSnapshotEventHandler delete a single snapshot. This is not bound to application name.