-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathpresign.go
134 lines (112 loc) · 2.88 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package command
import (
"context"
"fmt"
"time"
"github.com/urfave/cli/v2"
"github.com/peak/s5cmd/v2/log/stat"
"github.com/peak/s5cmd/v2/storage"
"github.com/peak/s5cmd/v2/storage/url"
)
var presignHelpTemplate = `Name:
{{.HelpName}} - {{.Usage}}
Usage:
{{.HelpName}} [options] source
Options:
{{range .VisibleFlags}}{{.}}
{{end}}
Examples:
1. Print a remote object url to stdout
> s5cmd {{.HelpName}} s3://bucket/prefix/object
2. Print a remote object url with a specific expiration time to stdout
> s5cmd {{.HelpName}} --expire 24h s3://bucket/prefix/object
`
func NewPresignCommand() *cli.Command {
cmd := &cli.Command{
Name: "presign",
HelpName: "presign",
Usage: "print remote object presign url",
Flags: []cli.Flag{
&cli.DurationFlag{
Name: "expire",
Usage: "url valid duration",
Value: time.Hour * 3,
},
&cli.StringFlag{
Name: "version-id",
Usage: "use the specified version of an object",
},
},
CustomHelpTemplate: presignHelpTemplate,
Before: func(c *cli.Context) error {
err := validatePresignCommand(c)
if err != nil {
printError(commandFromContext(c), c.Command.Name, err)
}
return err
},
Action: func(c *cli.Context) (err error) {
defer stat.Collect(c.Command.FullName(), &err)()
op := c.Command.Name
fullCommand := commandFromContext(c)
src, err := url.New(c.Args().Get(0), url.WithVersion(c.String("version-id")))
if err != nil {
printError(fullCommand, op, err)
return err
}
return Presign{
src: src,
op: op,
fullCommand: fullCommand,
expire: c.Duration("expire"),
storageOpts: NewStorageOpts(c),
}.Run(c.Context)
},
}
return cmd
}
// Presign holds presign operation flags and states.
type Presign struct {
src *url.URL
op string
fullCommand string
expire time.Duration
storageOpts storage.Options
}
// Run prints content of given source to standard output.
func (c Presign) Run(ctx context.Context) error {
client, err := storage.NewRemoteClient(ctx, c.src, c.storageOpts)
if err != nil {
printError(c.fullCommand, c.op, err)
return err
}
url, err := client.Presign(ctx, c.src, c.expire)
if err != nil {
printError(c.fullCommand, c.op, err)
return err
}
fmt.Println(url)
return nil
}
func validatePresignCommand(c *cli.Context) error {
if c.Args().Len() != 1 {
return fmt.Errorf("expected remote object url")
}
src, err := url.New(c.Args().Get(0), url.WithVersion(c.String("version-id")))
if err != nil {
return err
}
if !src.IsRemote() {
return fmt.Errorf("source must be a remote object")
}
if src.IsBucket() || src.IsPrefix() {
return fmt.Errorf("remote source must be an object")
}
if src.IsWildcard() {
return fmt.Errorf("remote source %q can not contain glob characters", src)
}
if err := checkVersioningWithGoogleEndpoint(c); err != nil {
return err
}
return nil
}