Skip to content

Commit

Permalink
新增文件上传下载限速、支持生成4种外链且支持限速
Browse files Browse the repository at this point in the history
  • Loading branch information
wilac-pv committed Jun 8, 2023
1 parent 2f5b9f6 commit a68f819
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 190 deletions.
13 changes: 1 addition & 12 deletions aws/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Request struct {
*Service
Handlers Handlers
Time time.Time
ExpireTime time.Duration
ExpireTime int64
Operation *Operation
HTTPRequest *http.Request
HTTPResponse *http.Response
Expand Down Expand Up @@ -135,17 +135,6 @@ func (r *Request) SetReaderBody(reader io.ReadSeeker) {
r.Body = reader
}

// Presign returns the request's signed URL. Error will be returned
// if the signing fails.
func (r *Request) Presign(expireTime time.Duration) (string, error) {
r.ExpireTime = expireTime
r.Sign()
if r.Error != nil {
return "", r.Error
}
return r.HTTPRequest.URL.String(), nil
}

// Build will build the request's object so it can be signed and sent
// to the service. Build will also validate all the request's parameters.
// Anny additional build Handlers set on this request will be run
Expand Down
3 changes: 1 addition & 2 deletions internal/signer/v2/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type signer struct {
Service *aws.Service
Request *http.Request
Time time.Time
ExpireTime time.Duration
ExpireTime int64
ServiceName string
Region string
CredValues credentials.Value
Expand Down Expand Up @@ -305,7 +305,6 @@ func (v2 *signer) buildStringToSign() {
if len(typelist) > 0 {
contenttype = v2.Request.Header["Content-Type"][0]
}

signItems := []string{v2.Request.Method, md5, contenttype}
if v2.isPresign {
signItems = append(signItems, v2.Query["Expires"][0])
Expand Down
2 changes: 1 addition & 1 deletion internal/signer/v4/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var ignoredHeaders = map[string]bool{
type signer struct {
Request *http.Request
Time time.Time
ExpireTime time.Duration
ExpireTime int64
ServiceName string
Region string
CredValues credentials.Value
Expand Down
27 changes: 1 addition & 26 deletions internal/signer/v4/v4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func TestIgnorePreResignRequestWithValidCreds(t *testing.T) {
nil,
nil,
)
r.ExpireTime = time.Minute * 10
r.ExpireTime = 10

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

func TestPreResignRequestExpiredCreds(t *testing.T) {
provider := &credentials.StaticProvider{credentials.Value{"AKID", "SECRET", "SESSION"}}
creds := credentials.NewCredentials(provider)
r := aws.NewRequest(
aws.NewService(&aws.Config{Credentials: creds}),
&aws.Operation{
Name: "BatchGetItem",
HTTPMethod: "POST",
HTTPPath: "/",
},
nil,
nil,
)
r.ExpireTime = time.Minute * 10

Sign(r)
querySig := r.HTTPRequest.URL.Query().Get("X-Amz-Signature")

creds.Expire()
r.Time = time.Now().Add(time.Hour * 48)

Sign(r)
assert.NotEqual(t, querySig, r.HTTPRequest.URL.Query().Get("X-Amz-Signature"))
}

func BenchmarkPresignRequest(b *testing.B) {
signer := buildSigner("dynamodb", "us-east-1", time.Now(), 300*time.Second, "{}")
for i := 0; i < b.N; i++ {
Expand Down
77 changes: 29 additions & 48 deletions service/s3/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import (
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -928,15 +926,6 @@ func (c *S3) GetObjectToFile(bucket, objectKey, filePath, Range string) error {

return os.Rename(tempFilePath, filePath)
}
func (c *S3) GetObjectPresignedUrl(input *GetObjectInput, expires time.Duration) (*url.URL, error) {
req, _ := c.GetObjectRequest(input)
req.ExpireTime = expires
err := req.Sign()
if *input.TrafficLimit > 0 {
req.HTTPRequest.URL.RawQuery += "&x-kss-traffic-limit=" + strconv.FormatInt(*input.TrafficLimit, 10)
}
return req.HTTPRequest.URL, err
}

var opGetObject *aws.Operation

Expand Down Expand Up @@ -1605,42 +1594,41 @@ func (c *S3) PutBucketWebsiteRequest(input *PutBucketWebsiteInput) (req *aws.Req
return
}

type HTTPMethod string

const (
PUT HTTPMethod = "PUT"
GET HTTPMethod = "GET"
DELETE HTTPMethod = "DELETE"
HEAD HTTPMethod = "HEAD"
)

type metadataGeneratePresignedUrlInput struct {
SDKShapeTraits bool `type:"structure" payload:"GeneratePresignedUrlInput"`
}
type GeneratePresignedUrlInput struct {
HTTPMethod string

// The canned ACL to apply to the object.
ACL *string `location:"header" locationName:"x-amz-acl" type:"string"`

Bucket *string `location:"uri" locationName:"Bucket" type:"string" required:"true"`

// Specifies what content encodings have been applied to the object and thus
// what decoding mechanisms must be applied to obtain the media-type referenced
// by the Content-Type header field.
ContentEncoding *string `location:"header" locationName:"Content-Encoding" type:"string"`

// The language the content is in.
ContentLanguage *string `location:"header" locationName:"Content-Language" type:"string"`

// Size of the body in bytes. This parameter is useful when the size of the
// body cannot be determined automatically.
ContentLength *int64 `location:"header" locationName:"Content-Length" type:"integer"`

// A standard MIME type describing the format of the object data.
ContentType *string `location:"header" locationName:"Content-Type" type:"string"`

// The date and time at which the object is no longer cacheable.
Expires time.Duration

Key *string `location:"uri" locationName:"Key" type:"string" required:"true"`

// A map of metadata to store with the object in S3.
Metadata map[string]*string `location:"headers" locationName:"x-amz-meta-" type:"map"`

CallbackUrl *string `location:"header" locationName:"x-kss-callbackurl" type:"string"`
CallbackBody *string `location:"header" locationName:"x-kss-callbackbody" type:"string"`
TrafficLimit *int64 `location:"querystring" locationName:"x-kss-traffic-limit" type:"integer"`

TrafficLimit *int64 `location:"header" locationName:"x-kss-traffic-limit" type:"integer"`
HTTPMethod HTTPMethod

metadataPutObjectInput `json:"-" xml:"-"`
// The date and time at which the object is no longer cacheable.
Expires int64

metadataGeneratePresignedUrlInput `json:"-" xml:"-"`
}
type GeneratePresignedUrlOutput struct {
url *string
Expand Down Expand Up @@ -1717,31 +1705,24 @@ func (c *S3) PutObject(input *PutObjectInput) (*PutObjectOutput, error) {
func (c *S3) GeneratePresignedUrlInput(input *GeneratePresignedUrlInput) (url string) {

opPutObject = &aws.Operation{
Name: "PutObject",
HTTPMethod: input.HTTPMethod,
HTTPMethod: string(input.HTTPMethod),
HTTPPath: "/{Bucket}/{Key+}",
}

output := &GeneratePresignedUrlOutput{}
req := c.newRequest(opPutObject, input, output)
req.ExpireTime = input.Expires
req.Sign()
if *input.TrafficLimit > 0 {
req.HTTPRequest.URL.RawQuery += "&x-kss-traffic-limit=" + strconv.FormatInt(*input.TrafficLimit, 10)
now := time.Now().Unix()
if c.Config.SignerVersion == "V2" {
req.ExpireTime = input.Expires + now
} else {
req.ExpireTime = input.Expires
}
req.Sign()
//if input.TrafficLimit != nil && *input.TrafficLimit > 0 {
// req.HTTPRequest.URL.RawQuery += "&x-amz-traffic-limit=" + strconv.FormatInt(*input.TrafficLimit, 10)
//}
return req.HTTPRequest.URL.String()
}

func (c *S3) PutObjectPresignedUrl(input *PutObjectInput, expires time.Duration) (*url.URL, error) {
req, _ := c.PutObjectRequest(input)
req.ExpireTime = expires
err := req.Sign()
if *input.TrafficLimit > 0 {
req.HTTPRequest.URL.RawQuery += "&x-kss-traffic-limit=" + strconv.FormatInt(*input.TrafficLimit, 10)
}
return req.HTTPRequest.URL, err
}

func (c *S3) PutObjectPreassignedInput(input *PutObjectInput) (*http.Request, error) {
req, _ := c.PutObjectRequest(input)
err := req.Sign()
Expand Down
20 changes: 0 additions & 20 deletions service/s3/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package s3

import (
"github.com/ks3sdklib/aws-sdk-go/aws"
"net/url"
"time"
)

// GetBucketCORSRequest generates a request for the GetBucketCORS operation.
Expand Down Expand Up @@ -35,12 +33,6 @@ func (c *S3) GetBucketCORS(input *GetBucketCORSInput) (*GetBucketCORSOutput, err
err := req.Send()
return out, err
}
func (c *S3) GetBucketCORSPresignedUrl(input *GetBucketCORSInput, expires time.Duration) (*url.URL, error) {
req, _ := c.GetBucketCORSRequest(input)
req.ExpireTime = expires
err := req.Sign()
return req.HTTPRequest.URL, err
}

var opGetBucketCORS *aws.Operation

Expand Down Expand Up @@ -73,12 +65,6 @@ func (c *S3) DeleteBucketCORS(input *DeleteBucketCORSInput) (*DeleteBucketCORSOu
err := req.Send()
return out, err
}
func (c *S3) DeleteBucketCORSPresignedUrl(input *DeleteBucketCORSInput, expires time.Duration) (*url.URL, error) {
req, _ := c.DeleteBucketCORSRequest(input)
req.ExpireTime = expires
err := req.Sign()
return req.HTTPRequest.URL, err
}

var opDeleteBucketCORS *aws.Operation

Expand Down Expand Up @@ -110,12 +96,6 @@ func (c *S3) PutBucketCORS(input *PutBucketCORSInput) (*PutBucketCORSOutput, err
err := req.Send()
return out, err
}
func (c *S3) PutBucketCORSPresignedUrl(input *PutBucketCORSInput, expires time.Duration) (*url.URL, error) {
req, _ := c.PutBucketCORSRequest(input)
req.ExpireTime = expires
err := req.Sign()
return req.HTTPRequest.URL, err
}

var opPutBucketCORS *aws.Operation

Expand Down
13 changes: 0 additions & 13 deletions service/s3/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package s3

import (
"github.com/ks3sdklib/aws-sdk-go/aws"
"net/url"
"time"
)

Expand Down Expand Up @@ -181,12 +180,6 @@ func (c *S3) DeleteBucketLifecycle(input *DeleteBucketLifecycleInput) (*DeleteBu
err := req.Send()
return out, err
}
func (c *S3) DeleteBucketLifecyclePresignedUrl(input *DeleteBucketLifecycleInput, expires time.Duration) (*url.URL, error) {
req, _ := c.DeleteBucketLifecycleRequest(input)
req.ExpireTime = expires
err := req.Sign()
return req.HTTPRequest.URL, err
}

var opDeleteBucketLifecycle *aws.Operation

Expand Down Expand Up @@ -219,12 +212,6 @@ func (c *S3) GetBucketLifecycle(input *GetBucketLifecycleInput) (*GetBucketLifec
err := req.Send()
return out, err
}
func (c *S3) GetBucketLifecyclePresignedUrl(input *GetBucketLifecycleInput, expires time.Duration) (*url.URL, error) {
req, _ := c.GetBucketLifecycleRequest(input)
req.ExpireTime = expires
err := req.Sign()
return req.HTTPRequest.URL, err
}

var opGetBucketLifecycle *aws.Operation

Expand Down
Loading

0 comments on commit a68f819

Please sign in to comment.