1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-29 08:22:11 +02:00
imgproxy/s3transport.go

59 lines
1.2 KiB
Go
Raw Normal View History

2018-05-26 16:22:41 +02:00
package main
import (
2020-02-27 17:44:59 +02:00
"fmt"
http "net/http"
2018-05-26 16:22:41 +02:00
"github.com/aws/aws-sdk-go/aws"
"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
}
2020-02-27 17:44:59 +02:00
func newS3Transport() (http.RoundTripper, error) {
2018-11-13 15:23:59 +02:00
s3Conf := aws.NewConfig()
if len(conf.S3Region) != 0 {
s3Conf.Region = aws.String(conf.S3Region)
2018-11-13 15:23:59 +02:00
}
if len(conf.S3Endpoint) != 0 {
s3Conf.Endpoint = aws.String(conf.S3Endpoint)
s3Conf.S3ForcePathStyle = aws.Bool(true)
2018-11-13 15:23:59 +02:00
}
sess, err := session.NewSession()
if err != nil {
2020-02-27 17:44:59 +02:00
return nil, fmt.Errorf("Can't create S3 session: %s", err)
}
2018-12-09 14:11:15 +02:00
if sess.Config.Region == nil || len(*sess.Config.Region) == 0 {
sess.Config.Region = aws.String("us-west-1")
}
2020-02-27 17:44:59 +02:00
return s3Transport{s3.New(sess, s3Conf)}, nil
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),
}
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)
2019-03-22 18:57:18 +02:00
if err := s3req.Send(); err != nil {
return nil, err
2018-05-26 16:22:41 +02:00
}
2019-03-22 18:57:18 +02:00
return s3req.HTTPResponse, nil
2018-05-26 16:22:41 +02:00
}