@@ -5,13 +5,14 @@ import (
55 "context"
66 "crypto/tls"
77 "errors"
8- "github.com/aws/aws-sdk-go/aws/request"
98 "io"
109 "net/http"
1110 "net/url"
1211 "strings"
1312 "time"
1413
14+ "github.com/aws/aws-sdk-go/aws/request"
15+
1516 "github.com/aws/aws-sdk-go/aws"
1617 "github.com/aws/aws-sdk-go/aws/credentials"
1718 "github.com/aws/aws-sdk-go/aws/defaults"
@@ -34,12 +35,13 @@ type S3Storage struct {
3435 ctx context.Context
3536 listMarker * string
3637 rlBucket ratelimit.Bucket
38+ serverGzip bool
3739}
3840
3941// NewS3Storage return new configured S3 storage.
4042//
4143// You should always create new storage with this constructor.
42- func NewS3Storage (awsNoSign bool , awsAccessKey , awsSecretKey , awsToken , awsRegion , endpoint , bucketName , prefix string , keysPerReq int64 , retryCnt uint , retryDelay time.Duration , skipSSLVerify bool ) * S3Storage {
44+ func NewS3Storage (awsNoSign bool , awsAccessKey , awsSecretKey , awsToken , awsRegion , endpoint , bucketName , prefix string , keysPerReq int64 , retryCnt uint , retryDelay time.Duration , skipSSLVerify bool , serverGzip bool ) * S3Storage {
4345 sess := session .Must (session .NewSessionWithOptions (session.Options {
4446 SharedConfigState : session .SharedConfigEnable ,
4547 }))
@@ -83,6 +85,7 @@ func NewS3Storage(awsNoSign bool, awsAccessKey, awsSecretKey, awsToken, awsRegio
8385 retryInterval : retryDelay ,
8486 ctx : context .TODO (),
8587 rlBucket : ratelimit .NewFakeBucket (),
88+ serverGzip : serverGzip ,
8689 }
8790
8891 return & st
@@ -105,7 +108,7 @@ func (st *S3Storage) WithRateLimit(limit int) error {
105108
106109// List S3 bucket and send founded objects to chan.
107110func (st * S3Storage ) List (output chan <- * storage.Object ) error {
108- listObjectsFn := func (p * s3.ListObjectsOutput , lastPage bool ) bool {
111+ listObjectsFn := func (p * s3.ListObjectsV2Output , lastPage bool ) bool {
109112 for _ , o := range p .Contents {
110113 key , _ := url .QueryUnescape (aws .StringValue (o .Key ))
111114 key = strings .Replace (key , st .prefix , "" , 1 )
@@ -117,19 +120,19 @@ func (st *S3Storage) List(output chan<- *storage.Object) error {
117120 IsLatest : aws .Bool (true ),
118121 }
119122 }
120- st .listMarker = p .Marker
123+ st .listMarker = p .NextContinuationToken
121124 return ! lastPage // continue paging
122125 }
123126
124- input := & s3.ListObjectsInput {
125- Bucket : st .awsBucket ,
126- Prefix : aws .String (st .prefix ),
127- MaxKeys : aws .Int64 (st .keysPerReq ),
128- EncodingType : aws .String (s3 .EncodingTypeUrl ),
129- Marker : st .listMarker ,
127+ input := & s3.ListObjectsV2Input {
128+ Bucket : st .awsBucket ,
129+ Prefix : aws .String (st .prefix ),
130+ MaxKeys : aws .Int64 (st .keysPerReq ),
131+ EncodingType : aws .String (s3 .EncodingTypeUrl ),
132+ ContinuationToken : st .listMarker ,
130133 }
131134
132- if err := st .awsSvc .ListObjectsPagesWithContext (st .ctx , input , listObjectsFn ); err != nil {
135+ if err := st .awsSvc .ListObjectsV2PagesWithContext (st .ctx , input , listObjectsFn ); err != nil {
133136 return err
134137 }
135138 storage .Log .Debugf ("Listing bucket finished" )
@@ -191,12 +194,6 @@ func (st *S3Storage) PutObject(obj *storage.Object) error {
191194 return nil
192195}
193196
194- func withAcceptEncoding (e string ) request.Option {
195- return func (r * request.Request ) {
196- r .HTTPRequest .Header .Add ("Accept-Encoding" , e )
197- }
198- }
199-
200197// GetObjectContent read object content and metadata from S3.
201198func (st * S3Storage ) GetObjectContent (obj * storage.Object ) error {
202199 input := & s3.GetObjectInput {
@@ -205,7 +202,12 @@ func (st *S3Storage) GetObjectContent(obj *storage.Object) error {
205202 VersionId : obj .VersionId ,
206203 }
207204
208- result , err := st .awsSvc .GetObjectWithContext (st .ctx , input , withAcceptEncoding ("gzip" ))
205+ opts := make ([]request.Option , 0 , 1 )
206+ if ! st .serverGzip {
207+ opts = append (opts , withAcceptEncoding ("gzip" ))
208+ }
209+
210+ result , err := st .awsSvc .GetObjectWithContext (st .ctx , input , opts ... )
209211 if err != nil {
210212 return err
211213 }
0 commit comments