/
gocloud.go
95 lines (66 loc) · 1.9 KB
/
gocloud.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
package openaccess
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"gocloud.dev/blob"
"gocloud.dev/blob/s3blob"
_ "log"
"net/url"
)
const IS_SMITHSONIAN_S3 string = "github.com/aaronland/go-smithsonian-openaccess#is_smithsonian_s3"
// Special-case bucket opener to deal with setting the necessary flags to know how
// to fetch data fromt the `smithsonian-open-access` S3 bucket. Note how we are returning
// a new context.Context with signals for the rest of the code to use (20201119/straup)
func OpenBucket(ctx context.Context, uri string) (context.Context, *blob.Bucket, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, nil, err
}
is_smithsonian_s3 := false
switch u.Scheme {
case "s3":
if u.Host == AWS_S3_BUCKET {
is_smithsonian_s3 = true
}
case "si":
uri = fmt.Sprintf("s3://%s?region=%s", AWS_S3_BUCKET, AWS_S3_REGION)
is_smithsonian_s3 = true
default:
// pass
}
var bucket *blob.Bucket
if is_smithsonian_s3 {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(AWS_S3_REGION),
Credentials: credentials.AnonymousCredentials,
})
if err != nil {
return nil, nil, err
}
// SKIPMETADATA GOES HERE
b, err := s3blob.OpenBucket(ctx, sess, AWS_S3_BUCKET, nil)
if err != nil {
return nil, nil, err
}
bucket = b
} else {
b, err := blob.OpenBucket(ctx, uri)
if err != nil {
return nil, nil, err
}
bucket = b
}
ctx = context.WithValue(ctx, IS_SMITHSONIAN_S3, is_smithsonian_s3)
return ctx, bucket, nil
}
func OpenMetadataBucket(ctx context.Context, uri string) (context.Context, *blob.Bucket, error) {
ctx, bucket, err := OpenBucket(ctx, uri)
if err != nil {
return nil, nil, fmt.Errorf("Failed to open metadata bucket, %w", err)
}
bucket = blob.PrefixedBucket(bucket, "metadata/edan")
return ctx, bucket, nil
}