-
Notifications
You must be signed in to change notification settings - Fork 13
/
presign.go
87 lines (73 loc) · 2.48 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
package main
import (
"github.com/aos-dev/go-storage/v2"
"github.com/qingstor/noah/task"
"github.com/spf13/cobra"
"github.com/qingstor/qsctl/v2/cmd/qsctl/taskutils"
"github.com/qingstor/qsctl/v2/constants"
"github.com/qingstor/qsctl/v2/pkg/i18n"
"github.com/qingstor/qsctl/v2/utils"
)
type presignFlags struct {
expire int
}
var presignFlag = presignFlags{}
// PresignCommand will handle list command.
var PresignCommand = &cobra.Command{
Use: "presign qs://<bucket_name>/<object_key>",
Short: i18n.Sprintf("get the pre-signed URL by the object key"),
Long: i18n.Sprintf(`qsctl presign can generate a pre-signed URL for the object.
Within the given expire time, anyone who receives this URL can retrieve
the object with an HTTP GET request. If an object belongs to a public bucket,
generate a URL spliced by bucket name, zone and its name, anyone who receives
this URL can always retrieve the object with an HTTP GET request.`),
Example: utils.AlignPrintWithColon(
i18n.Sprintf("Presign object: qsctl qs://bucket-name/object-name"),
),
Args: cobra.ExactArgs(1),
PreRun: validatePresignFlag,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true // handle runtime errors with i18n, do not show error
if err := presignRun(cmd, args); err != nil {
i18n.Fprintf(cmd.OutOrStderr(), "Execute %s command error: %s\n", "presign", err.Error())
return err
}
return nil
},
}
func presignRun(c *cobra.Command, args []string) (err error) {
silenceUsage(c) // silence usage when handled error returns
rootTask := taskutils.NewAtStorageTask(10)
_, err = utils.ParseAtStorageInput(rootTask, args[0])
if err != nil {
return
}
t := task.NewReachFile(rootTask)
t.SetReacher(rootTask.GetStorage().(storage.Reacher))
t.SetExpire(presignFlag.expire)
t.Run(c.Context())
if t.GetFault().HasError() {
return t.GetFault()
}
i18n.Fprintf(c.OutOrStdout(), "%s\n", t.GetURL())
return nil
}
func initPresignFlag() {
PresignCommand.Flags().IntVarP(&presignFlag.expire, constants.ExpireFlag, "e", 0,
i18n.Sprintf("the number of seconds until the pre-signed URL expires. Default is 300 seconds"))
}
func validatePresignFlag(_ *cobra.Command, _ []string) {
// set expire default to DefaultPresignExpire
if presignFlag.expire <= 0 {
presignFlag.expire = constants.DefaultPresignExpire
}
}
func resetPresignFlag() {
presignFlag = presignFlags{}
}
type presignShellHandler struct {
blankShellHandler
}
func (h presignShellHandler) postRun(_ error) {
resetPresignFlag()
}