2018-05-26 16:22:41 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-04 17:12:51 +02:00
|
|
|
http "net/http"
|
|
|
|
|
2018-05-26 16:22:41 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2018-10-04 17:12:51 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2018-05-26 16:22:41 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// s3Transport implements RoundTripper for the 's3' protocol.
|
|
|
|
type s3Transport struct {
|
|
|
|
svc *s3.S3
|
|
|
|
}
|
|
|
|
|
2018-10-04 17:12:51 +02:00
|
|
|
func newS3Transport() http.RoundTripper {
|
2018-11-13 15:23:59 +02:00
|
|
|
s3Conf := aws.NewConfig()
|
|
|
|
|
|
|
|
if len(conf.S3Region) != 0 {
|
2018-12-09 13:06:52 +02:00
|
|
|
s3Conf.Region = aws.String(conf.S3Region)
|
2018-11-13 15:23:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(conf.S3Endpoint) != 0 {
|
2018-12-09 13:06:52 +02:00
|
|
|
s3Conf.Endpoint = aws.String(conf.S3Endpoint)
|
|
|
|
s3Conf.S3ForcePathStyle = aws.Bool(true)
|
2018-11-13 15:23:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return s3Transport{s3.New(session.New(), s3Conf)}
|
2018-05-26 16:22:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t s3Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
|
|
|
input := &s3.GetObjectInput{
|
|
|
|
Bucket: aws.String(req.URL.Host),
|
|
|
|
Key: aws.String(req.URL.Path),
|
|
|
|
}
|
2018-11-13 16:04:04 +02:00
|
|
|
|
|
|
|
if len(req.URL.RawQuery) > 0 {
|
|
|
|
input.VersionId = aws.String(req.URL.RawQuery)
|
|
|
|
}
|
|
|
|
|
2018-05-26 16:22:41 +02:00
|
|
|
s3req, _ := t.svc.GetObjectRequest(input)
|
|
|
|
|
|
|
|
s3err := s3req.Send()
|
|
|
|
if s3err == nil { // resp is now filled
|
|
|
|
return s3req.HTTPResponse, nil
|
|
|
|
}
|
|
|
|
fmt.Println("s3 error", s3err)
|
|
|
|
return nil, s3err
|
|
|
|
}
|