forked from gomods/athens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
100 lines (88 loc) · 2.94 KB
/
storage.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
package s3
import (
"bytes"
"context"
"fmt"
"io"
"net/url"
"github.com/opentracing/opentracing-go"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/service/s3/s3manager/s3manageriface"
"github.com/gomods/athens/pkg/config/env"
moduploader "github.com/gomods/athens/pkg/storage/module"
)
// Storage implements (github.com/gomods/athens/pkg/storage).Saver and
// also provides a function to fetch the location of a module
// Storage uses amazon aws go SDK which expects these env variables
// - AWS_REGION - region for this storage, e.g 'us-west-2'
// - AWS_ACCESS_KEY_ID -
// - AWS_SECRET_ACCESS_KEY -
// - AWS_SESSION_TOKEN - [optional]
// For information how to get your keyId and access key turn to official aws docs: https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/setting-up.html
type Storage struct {
bucket string
baseURI *url.URL
uploader s3manageriface.UploaderAPI
}
// New creates a new AWS S3 CDN saver
func New(bucketName string) (*Storage, error) {
u, err := url.Parse(fmt.Sprintf("http://%s.s3.amazonaws.com", bucketName))
if err != nil {
return nil, err
}
// create a session
sess, err := session.NewSession()
if err != nil {
return nil, err
}
uploader := s3manager.NewUploader(sess)
return &Storage{
bucket: bucketName,
uploader: uploader,
baseURI: u,
}, nil
}
// NewWithUploader creates a new AWS S3 CDN saver with provided uploader
func NewWithUploader(bucketName string, uploader s3manageriface.UploaderAPI) (*Storage, error) {
u, err := url.Parse(fmt.Sprintf("http://%s.s3.amazonaws.com", bucketName))
if err != nil {
return nil, err
}
return &Storage{
bucket: bucketName,
uploader: uploader,
baseURI: u,
}, nil
}
// BaseURL returns the base URL that stores all modules. It can be used
// in the "meta" tag redirect response to vgo.
//
// For example:
//
// <meta name="go-import" content="gomods.com/athens mod BaseURL()">
func (s Storage) BaseURL() *url.URL {
return env.CDNEndpointWithDefault(s.baseURI)
}
// Save implements the (github.com/gomods/athens/pkg/storage).Saver interface.
func (s *Storage) Save(ctx context.Context, module, version string, mod []byte, zip io.Reader, info []byte) error {
sp, ctx := opentracing.StartSpanFromContext(ctx, "storage.s3.Save")
defer sp.Finish()
err := moduploader.Upload(ctx, module, version, bytes.NewReader(info), bytes.NewReader(mod), zip, s.upload)
// TODO: take out lease on the /list file and add the version to it
//
// Do that only after module source+metadata is uploaded
return err
}
func (s *Storage) upload(ctx context.Context, path, contentType string, stream io.Reader) error {
sp, ctx := opentracing.StartSpanFromContext(ctx, "storage.s3.upload")
defer sp.Finish()
upParams := &s3manager.UploadInput{
Bucket: &s.bucket,
Key: &path,
Body: stream,
ContentType: &contentType,
}
_, err := s.uploader.UploadWithContext(ctx, upParams)
return err
}