-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (93 loc) · 3.34 KB
/
main.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
// File really does nothing. We're patching the aws sdk. This is purely a "test" entrypoint
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func main() {
var bucket, key string
var timeout time.Duration
flag.StringVar(&bucket, "b", "", "Bucket name.")
flag.StringVar(&key, "k", "", "Object key name.")
flag.DurationVar(&timeout, "d", 0, "Upload timeout.")
flag.Parse()
if err := run(bucket, key, timeout); err != nil{
os.Exit(1)
}
}
type Rando struct {
BucketKeyEnabled *bool
SSEKMSKeyId *string
SomethingWeird *string
}
func run(bucket string, key string, timeout time.Duration) error {
// All clients require a Session. The Session provides the client with
// shared configuration such as region, endpoint, and credentials. A
// Session should be shared where possible to take advantage of
// configuration and credential caching. See the session package for
// more information.
sess := session.Must(session.NewSession())
// Create a new instance of the service's client with a Session.
// Optional aws.Config values can also be provided as variadic arguments
// to the New function. This option allows you to provide service
// specific configuration.
svc := s3.New(sess)
mock := request.GetMock()
mock.On("s3", "PutObject").Return(&Rando{BucketKeyEnabled: aws.Bool(true), SSEKMSKeyId: aws.String("ABC")})
mock.On("s3", "PutObject").Return(&Rando{BucketKeyEnabled: aws.Bool(false), SomethingWeird: aws.String("Value")})
// mock.On("*", "*").Do(func (r *request.Request) {
// // call my jsfun with args
// })
// mock.On("*", "*").Return(&s3.PutObjectOutput{})
// svc.Handlers.Send.PushFront(func (r *request.Request) {
// fmt.Printf("%s\n", r.Operation.Name)
// // r.Operation.Name == "PutObject"
// // r.Params.Bucket == &"unknown"
// // r.Data (fill response data)
// })
// Create a context with a timeout that will abort the upload if it takes
// more than the passed in timeout.
ctx := context.Background()
var cancelFn func()
if timeout > 0 {
ctx, cancelFn = context.WithTimeout(ctx, timeout)
}
// Ensure the context is canceled to prevent leaking.
// See context package for more information, https://golang.org/pkg/context/
if cancelFn != nil {
defer cancelFn()
}
// Uploads the object to S3. The Context will interrupt the request if the
// timeout expires.
r, err := svc.PutObjectWithContext(ctx, &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: os.Stdin,
})
// r, err := svc.CreateBucketWithContext(ctx, &s3.CreateBucketInput{
// Bucket: aws.String(bucket),
// })
// r, err := svc.GetBucketAclWithContext(ctx, &s3.GetBucketAclInput{
// Bucket: aws.String(bucket),
// })
fmt.Printf("Returned %+v\n", *r)
if err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == request.CanceledErrorCode {
// If the SDK can determine the request or retry delay was canceled
// by a context the CanceledErrorCode error code will be returned.
return fmt.Errorf("upload canceled due to timeout, %v", err)
} else {
return fmt.Errorf("failed to upload object, %v", err)
}
}
fmt.Printf("successfully uploaded file to %s/%s\n", bucket, key)
return nil
}