-
Notifications
You must be signed in to change notification settings - Fork 5
/
s3.go
68 lines (61 loc) · 1.07 KB
/
s3.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
package proxy
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"io"
"io/ioutil"
"net/url"
"os"
)
var (
sess *session.Session
)
func init() {
sess = session.New()
}
type s3cli struct {
*s3.S3
}
func news3cli(cfgs ...*aws.Config) s3cli {
return s3cli{s3.New(sess, cfgs...)}
}
func (s s3cli) get(uri *url.URL) (filepath string, err error) {
input := &s3.GetObjectInput{
Bucket: aws.String(uri.Host),
Key: aws.String(uri.Path),
}
resp, e := s.GetObject(input)
if e != nil {
filepath, err = "", e
return
}
e = os.Mkdir(".cert", 0700)
if e != nil {
if prr, ok := e.(*os.PathError); ok {
switch {
case os.IsExist(prr.Err):
break // ok, continue
default:
filepath, err = "", prr.Err
return
}
} else {
filepath, err = "", e
return
}
}
f, e := ioutil.TempFile(".cert", "")
if e != nil {
filepath, err = "", e
return
}
defer f.Close()
_, e = io.Copy(f, resp.Body)
if e != nil {
filepath, err = "", e
return
}
filepath, err = f.Name(), nil
return
}