-
Notifications
You must be signed in to change notification settings - Fork 20
/
storage_presign.go
69 lines (54 loc) · 1.54 KB
/
storage_presign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cmd
import (
"fmt"
"strings"
"time"
"github.com/exoscale/cli/pkg/storage/sos"
"github.com/spf13/cobra"
)
var storagePresignCmd = &cobra.Command{
Use: "presign sos://BUCKET/OBJECT",
Short: "Generate a pre-signed URL to an object",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
args[0] = strings.TrimPrefix(args[0], sos.BucketPrefix)
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
bucket string
key string
)
expires, err := cmd.Flags().GetDuration("expires")
if err != nil {
return err
}
method, err := cmd.Flags().GetString("method")
if err != nil {
return err
}
parts := strings.SplitN(args[0], "/", 2)
bucket, key = parts[0], parts[1]
storage, err := sos.NewStorageClient(
gContext,
sos.ClientOptZoneFromBucket(gContext, bucket),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %w", err)
}
url, err := storage.GenPresignedURL(gContext, method, bucket, key, expires)
if err != nil {
return fmt.Errorf("unable to pre-sign %s%s/%s: %w", sos.BucketPrefix, bucket, key, err)
}
fmt.Println(url)
return nil
},
}
func init() {
storagePresignCmd.Flags().StringP("method", "m", "get", "pre-signed URL method (get|put)")
storagePresignCmd.Flags().DurationP("expires", "e", 900*time.Second,
`expiration duration for the generated pre-signed URL (e.g. "1h45m", "30s"); supported units: "s", "m", "h"`)
storageCmd.AddCommand(storagePresignCmd)
}