@@ -5,13 +5,14 @@ import (
5
5
"context"
6
6
"crypto/tls"
7
7
"errors"
8
- "github.com/aws/aws-sdk-go/aws/request"
9
8
"io"
10
9
"net/http"
11
10
"net/url"
12
11
"strings"
13
12
"time"
14
13
14
+ "github.com/aws/aws-sdk-go/aws/request"
15
+
15
16
"github.com/aws/aws-sdk-go/aws"
16
17
"github.com/aws/aws-sdk-go/aws/credentials"
17
18
"github.com/aws/aws-sdk-go/aws/defaults"
@@ -34,12 +35,13 @@ type S3Storage struct {
34
35
ctx context.Context
35
36
listMarker * string
36
37
rlBucket ratelimit.Bucket
38
+ serverGzip bool
37
39
}
38
40
39
41
// NewS3Storage return new configured S3 storage.
40
42
//
41
43
// 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 {
43
45
sess := session .Must (session .NewSessionWithOptions (session.Options {
44
46
SharedConfigState : session .SharedConfigEnable ,
45
47
}))
@@ -83,6 +85,7 @@ func NewS3Storage(awsNoSign bool, awsAccessKey, awsSecretKey, awsToken, awsRegio
83
85
retryInterval : retryDelay ,
84
86
ctx : context .TODO (),
85
87
rlBucket : ratelimit .NewFakeBucket (),
88
+ serverGzip : serverGzip ,
86
89
}
87
90
88
91
return & st
@@ -105,7 +108,7 @@ func (st *S3Storage) WithRateLimit(limit int) error {
105
108
106
109
// List S3 bucket and send founded objects to chan.
107
110
func (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 {
109
112
for _ , o := range p .Contents {
110
113
key , _ := url .QueryUnescape (aws .StringValue (o .Key ))
111
114
key = strings .Replace (key , st .prefix , "" , 1 )
@@ -117,19 +120,19 @@ func (st *S3Storage) List(output chan<- *storage.Object) error {
117
120
IsLatest : aws .Bool (true ),
118
121
}
119
122
}
120
- st .listMarker = p .Marker
123
+ st .listMarker = p .NextContinuationToken
121
124
return ! lastPage // continue paging
122
125
}
123
126
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 ,
130
133
}
131
134
132
- if err := st .awsSvc .ListObjectsPagesWithContext (st .ctx , input , listObjectsFn ); err != nil {
135
+ if err := st .awsSvc .ListObjectsV2PagesWithContext (st .ctx , input , listObjectsFn ); err != nil {
133
136
return err
134
137
}
135
138
storage .Log .Debugf ("Listing bucket finished" )
@@ -191,12 +194,6 @@ func (st *S3Storage) PutObject(obj *storage.Object) error {
191
194
return nil
192
195
}
193
196
194
- func withAcceptEncoding (e string ) request.Option {
195
- return func (r * request.Request ) {
196
- r .HTTPRequest .Header .Add ("Accept-Encoding" , e )
197
- }
198
- }
199
-
200
197
// GetObjectContent read object content and metadata from S3.
201
198
func (st * S3Storage ) GetObjectContent (obj * storage.Object ) error {
202
199
input := & s3.GetObjectInput {
@@ -205,7 +202,12 @@ func (st *S3Storage) GetObjectContent(obj *storage.Object) error {
205
202
VersionId : obj .VersionId ,
206
203
}
207
204
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 ... )
209
211
if err != nil {
210
212
return err
211
213
}
0 commit comments