This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.go
150 lines (131 loc) · 4.36 KB
/
resource.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) 2021 Mert Bora Alper and EASE Lab
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package mare
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func init() {
err := os.Setenv("AWS_SDK_LOAD_CONFIG", "true")
if err != nil {
logrus.Fatal("Failed to set AWS_SDK_LOAD_CONFIG")
}
}
func (x *Resource) Get(ctx context.Context) (string, error) {
switch x.Backend {
case ResourceBackend_FILE:
return getFileResource(x.Locator)
case ResourceBackend_S3:
return getS3Resource(ctx, x.Locator)
case ResourceBackend_XDT:
panic("NOT IMPLEMENTED YET")
}
return "", fmt.Errorf("unknown backend: %d", x.Backend)
}
func getFileResource(path string) (string, error) {
data, err := ioutil.ReadFile(path)
return string(data), err
}
func getS3Resource(ctx context.Context, uri string) (string, error) {
parsed, err := parseS3URI(uri)
if err != nil {
return "", errors.Wrap(err, "failed to parse S3 uri")
}
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return "", errors.Wrap(err, "failed to load AWS config")
}
s3Client := s3.NewFromConfig(cfg)
params := &s3.GetObjectInput{
Bucket: aws.String(parsed.Hostname()),
Key: aws.String(parsed.Path),
}
resp, err := s3Client.GetObject(ctx, params)
if err != nil {
return "", errors.Wrapf(err, "failed to get object `%s`", uri)
}
data, err := ioutil.ReadAll(resp.Body)
return string(data), err
}
func (x *ResourceHint) Put(ctx context.Context, data string) (*Resource, error) {
switch x.Backend {
case ResourceBackend_FILE:
return putFileResource(x.Hint, data)
case ResourceBackend_S3:
return putS3Resource(ctx, x.Hint, data)
case ResourceBackend_XDT:
panic("NOT IMPLEMENTED YET")
}
return nil, fmt.Errorf("unknown backend: %d", x.Backend)
}
func putFileResource(dirname string, data string) (*Resource, error) {
f, err := ioutil.TempFile(dirname, "mare-*.tsv")
if err != nil {
return nil, errors.Wrap(err, "failed to create a temp file")
}
defer f.Close()
_, err = f.Write([]byte(data))
if err != nil {
return nil, errors.Wrap(err, "failed to write")
}
return &Resource{Backend: ResourceBackend_FILE, Locator: f.Name()}, nil
}
func putS3Resource(ctx context.Context, uri string, data string) (*Resource, error) {
parsed, err := parseS3URI(uri)
if err != nil {
return nil, errors.Wrap(err, "failed to parse S3 uri")
}
bucket := parsed.Hostname()
key := path.Join(parsed.Path, fmt.Sprintf("mare-%s.tsv", RandString(8)))
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to load AWS config")
}
s3Client := s3.NewFromConfig(cfg)
params := &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: strings.NewReader(data),
}
_, err = s3Client.PutObject(ctx, params)
if err != nil {
return nil, errors.Wrap(err, "failed to put object")
}
return &Resource{Backend: ResourceBackend_S3, Locator: fmt.Sprintf("s3://%s/%s", bucket, key)}, nil
}
// parseS3URI is copied from Corral.
func parseS3URI(uri string) (*url.URL, error) {
parsed, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("failed to parse S3URI: %s", err)
}
parsed.Path = strings.TrimPrefix(parsed.Path, "/")
return parsed, err
}