Skip to content

Commit d9586a4

Browse files
authored
Merge pull request #22 from ks3sdklib/dev
Dev
2 parents 9ac0283 + a68f819 commit d9586a4

File tree

10 files changed

+309
-470
lines changed

10 files changed

+309
-470
lines changed

aws/request.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ package aws
22

33
import (
44
"bytes"
5+
"github.com/ks3sdklib/aws-sdk-go/aws/awsutil"
56
"io"
67
"io/ioutil"
78
"net/http"
89
"net/url"
910
"reflect"
1011
"strings"
1112
"time"
12-
"github.com/ks3sdklib/aws-sdk-go/aws/awsutil"
1313
)
1414

1515
// A Request is the service request to be made.
1616
type Request struct {
1717
*Service
1818
Handlers Handlers
1919
Time time.Time
20-
ExpireTime time.Duration
20+
ExpireTime int64
2121
Operation *Operation
2222
HTTPRequest *http.Request
2323
HTTPResponse *http.Response
@@ -65,7 +65,7 @@ func NewRequest(service *Service, operation *Operation, params interface{}, data
6565
if p == "" {
6666
p = "/"
6767
}
68-
68+
6969
httpReq, _ := http.NewRequest(method, "", nil)
7070
httpReq.URL, _ = url.Parse(service.Endpoint + p)
7171

@@ -85,6 +85,20 @@ func NewRequest(service *Service, operation *Operation, params interface{}, data
8585
return r
8686
}
8787

88+
func NewRequestV2(service *Service, method string, params interface{}) *Request {
89+
90+
httpReq, _ := http.NewRequest(method, "", nil)
91+
httpReq.URL, _ = url.Parse(service.Endpoint)
92+
r := &Request{
93+
Service: service,
94+
ExpireTime: 0,
95+
HTTPRequest: httpReq,
96+
Params: params,
97+
Error: nil,
98+
}
99+
return r
100+
}
101+
88102
// WillRetry returns if the request's can be retried.
89103
func (r *Request) WillRetry() bool {
90104
return r.Error != nil && r.Retryable.Get() && r.RetryCount < r.Service.MaxRetries()
@@ -121,17 +135,6 @@ func (r *Request) SetReaderBody(reader io.ReadSeeker) {
121135
r.Body = reader
122136
}
123137

124-
// Presign returns the request's signed URL. Error will be returned
125-
// if the signing fails.
126-
func (r *Request) Presign(expireTime time.Duration) (string, error) {
127-
r.ExpireTime = expireTime
128-
r.Sign()
129-
if r.Error != nil {
130-
return "", r.Error
131-
}
132-
return r.HTTPRequest.URL.String(), nil
133-
}
134-
135138
// Build will build the request's object so it can be signed and sent
136139
// to the service. Build will also validate all the request's parameters.
137140
// Anny additional build Handlers set on this request will be run
@@ -177,7 +180,7 @@ func (r *Request) Sign() error {
177180
func (r *Request) Send() error {
178181
for {
179182
r.Sign()
180-
183+
181184
if r.Error != nil {
182185
return r.Error
183186
}

internal/signer/v2/v2.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type signer struct {
6262
Service *aws.Service
6363
Request *http.Request
6464
Time time.Time
65-
ExpireTime time.Duration
65+
ExpireTime int64
6666
ServiceName string
6767
Region string
6868
CredValues credentials.Value
@@ -187,7 +187,7 @@ func (v2 *signer) buildTime() {
187187
v2.formattedTime = v2.Time.UTC().Format(timeFormat)
188188

189189
if v2.isPresign {
190-
duration := int64(v2.ExpireTime / time.Second)
190+
duration := int64(v2.ExpireTime)
191191
v2.Query.Set("Expires", strconv.FormatInt(duration, 10))
192192
} else {
193193
v2.Request.Header.Set("Date", v2.formattedTime)
@@ -305,7 +305,6 @@ func (v2 *signer) buildStringToSign() {
305305
if len(typelist) > 0 {
306306
contenttype = v2.Request.Header["Content-Type"][0]
307307
}
308-
309308
signItems := []string{v2.Request.Method, md5, contenttype}
310309
if v2.isPresign {
311310
signItems = append(signItems, v2.Query["Expires"][0])

internal/signer/v4/v4.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var ignoredHeaders = map[string]bool{
3636
type signer struct {
3737
Request *http.Request
3838
Time time.Time
39-
ExpireTime time.Duration
39+
ExpireTime int64
4040
ServiceName string
4141
Region string
4242
CredValues credentials.Value
@@ -192,7 +192,7 @@ func (v4 *signer) buildTime() {
192192
v4.formattedShortTime = v4.Time.UTC().Format(shortTimeFormat)
193193

194194
if v4.isPresign {
195-
duration := int64(v4.ExpireTime / time.Second)
195+
duration := int64(v4.ExpireTime)
196196
v4.Query.Set("X-Amz-Date", v4.formattedTime)
197197
v4.Query.Set("X-Amz-Expires", strconv.FormatInt(duration, 10))
198198
} else {

internal/signer/v4/v4_test.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func TestIgnorePreResignRequestWithValidCreds(t *testing.T) {
175175
nil,
176176
nil,
177177
)
178-
r.ExpireTime = time.Minute * 10
178+
r.ExpireTime = 10
179179

180180
Sign(r)
181181
sig := r.HTTPRequest.Header.Get("X-Amz-Signature")
@@ -205,31 +205,6 @@ func TestResignRequestExpiredCreds(t *testing.T) {
205205
assert.NotEqual(t, querySig, r.HTTPRequest.Header.Get("Authorization"))
206206
}
207207

208-
func TestPreResignRequestExpiredCreds(t *testing.T) {
209-
provider := &credentials.StaticProvider{credentials.Value{"AKID", "SECRET", "SESSION"}}
210-
creds := credentials.NewCredentials(provider)
211-
r := aws.NewRequest(
212-
aws.NewService(&aws.Config{Credentials: creds}),
213-
&aws.Operation{
214-
Name: "BatchGetItem",
215-
HTTPMethod: "POST",
216-
HTTPPath: "/",
217-
},
218-
nil,
219-
nil,
220-
)
221-
r.ExpireTime = time.Minute * 10
222-
223-
Sign(r)
224-
querySig := r.HTTPRequest.URL.Query().Get("X-Amz-Signature")
225-
226-
creds.Expire()
227-
r.Time = time.Now().Add(time.Hour * 48)
228-
229-
Sign(r)
230-
assert.NotEqual(t, querySig, r.HTTPRequest.URL.Query().Get("X-Amz-Signature"))
231-
}
232-
233208
func BenchmarkPresignRequest(b *testing.B) {
234209
signer := buildSigner("dynamodb", "us-east-1", time.Now(), 300*time.Second, "{}")
235210
for i := 0; i < b.N; i++ {

internal/util/utilfile/utilfile.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package utilfile
2+
3+
import (
4+
"crypto/md5"
5+
"encoding/hex"
6+
"io"
7+
"os"
8+
)
9+
10+
func GetFileMD5(filePath string) (string, error) {
11+
file, err := os.Open(filePath)
12+
if err != nil {
13+
return "", err
14+
}
15+
defer file.Close()
16+
17+
hash := md5.New()
18+
if _, err := io.Copy(hash, file); err != nil {
19+
return "", err
20+
}
21+
22+
md5Hash := hash.Sum(nil)
23+
return hex.EncodeToString(md5Hash), nil
24+
}

0 commit comments

Comments
 (0)