Skip to content

resumable upload request header add content-length #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Changelog
## 7.25.3

* 修复
* 分片上传请求头增加 Content-Length


## 7.25.2

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ github.com/qiniu/go-sdk
在您的项目中的 `go.mod` 文件内添加这行代码

```
require github.com/qiniu/go-sdk/v7 v7.25.2
require github.com/qiniu/go-sdk/v7 v7.25.3
```

并且在项目中使用 `"github.com/qiniu/go-sdk/v7"` 引用 Qiniu Go SDK。
Expand Down
2 changes: 1 addition & 1 deletion conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/qiniu/go-sdk/v7/internal/env"
)

const Version = "7.25.1"
const Version = "7.25.3"

const (
CONTENT_TYPE_JSON = "application/json"
Expand Down
42 changes: 27 additions & 15 deletions internal/api-generator/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,6 @@ func (description *ApiDetailedDescription) generatePackage(group *jen.Group, opt
))
}))
}
if description.Request.HeaderNames != nil {
group.Add(
jen.List(jen.Id("headers"), jen.Err()).Op(":=").Id("innerRequest").Dot("buildHeaders").Call(),
)
group.Add(
jen.If(
jen.Err().Op("!=").Nil(),
).BlockFunc(func(group *jen.Group) {
group.Add(jen.Return(jen.Nil(), jen.Err()))
}),
)
}
guessPathSegmentsCount := 0
if description.BasePath != "" {
guessPathSegmentsCount += len(description.getBasePathSegments())
Expand Down Expand Up @@ -243,6 +231,22 @@ func (description *ApiDetailedDescription) generatePackage(group *jen.Group, opt
}),
)
}
if description.Request.HeaderNames != nil {
group.Add(
jen.List(jen.Id("headers"), jen.Err()).Op(":=").Id("innerRequest").Dot("buildHeaders").Call(),
)
group.Add(
jen.If(
jen.Err().Op("!=").Nil(),
).BlockFunc(func(group *jen.Group) {
group.Add(jen.Return(jen.Nil(), jen.Err()))
}),
)
} else {
group.Add(
jen.Id("headers").Op(":=").Qual("net/http", "Header").Values(jen.Dict{}),
)
}
if requestBody := description.Request.Body; requestBody != nil {
if json := requestBody.Json; json != nil {
group.Add(
Expand Down Expand Up @@ -301,6 +305,16 @@ func (description *ApiDetailedDescription) generatePackage(group *jen.Group, opt
))
}),
)
group.Add(
jen.Id("hErr").Op(":=").Qual(PackageNameUtils, "HttpHeadAddContentLength").Call(jen.Id("headers"), jen.Id("body")),
)
group.Add(
jen.If(
jen.Id("hErr").Op("!=").Nil(),
).Block(
jen.Return(jen.Nil(), jen.Id("hErr")),
),
)
}
}
if isStorageAPIs() {
Expand Down Expand Up @@ -405,9 +419,7 @@ func (description *ApiDetailedDescription) generatePackage(group *jen.Group, opt
group.Add(jen.Id("Endpoints").Op(":").Id("options").Dot("OverwrittenEndpoints"))
group.Add(jen.Id("Region").Op(":").Id("options").Dot("OverwrittenRegion"))
group.Add(jen.Id("Interceptors").Op(":").Index().Qual(PackageNameHTTPClient, "Interceptor").Values(jen.Id("uplogInterceptor")))
if description.Request.HeaderNames != nil {
group.Add(jen.Id("Header").Op(":").Id("headers"))
}
group.Add(jen.Id("Header").Op(":").Id("headers"))
switch description.Request.Authorization.ToAuthorization() {
case AuthorizationQbox:
group.Add(jen.Id("AuthType").Op(":").Qual(PackageNameAuth, "TokenQBox"))
Expand Down
1 change: 1 addition & 0 deletions internal/api-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
PackageNameRegion = "github.com/qiniu/go-sdk/v7/storagev2/region"
PackageNameUpToken = "github.com/qiniu/go-sdk/v7/storagev2/uptoken"
PackageNameErrors = "github.com/qiniu/go-sdk/v7/storagev2/errors"
PackageNameUtils = "github.com/qiniu/go-sdk/v7/storagev2/internal/utils"
PackageNameUplog = "github.com/qiniu/go-sdk/v7/internal/uplog"
PackageNameInternalIo = "github.com/qiniu/go-sdk/v7/internal/io"
)
Expand Down
20 changes: 15 additions & 5 deletions internal/clientv2/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -85,7 +86,7 @@ func (o *RequestParams) init() {
func NewRequest(options RequestParams) (req *http.Request, err error) {
var (
bodyWrapper *requestBodyWrapperWithProgress = nil
contentLength uint64
contentLength int64 = 0
)

options.init()
Expand All @@ -94,11 +95,17 @@ func NewRequest(options RequestParams) (req *http.Request, err error) {
if err != nil {
return nil, err
}
if options.OnRequestProgress != nil && body != nil {

if body != nil {
if contentLengthHeaderValue := options.Header.Get("Content-Length"); contentLengthHeaderValue != "" {
contentLength, _ = strconv.ParseUint(contentLengthHeaderValue, 10, 64)
contentLength, err = strconv.ParseInt(contentLengthHeaderValue, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid Content-Length header value: %s, %s", contentLengthHeaderValue, err)
}
}
if options.OnRequestProgress != nil {
bodyWrapper = &requestBodyWrapperWithProgress{body: body, expectedSize: uint64(contentLength), callback: options.OnRequestProgress}
}
bodyWrapper = &requestBodyWrapperWithProgress{body: body, expectedSize: contentLength, callback: options.OnRequestProgress}
}
if bodyWrapper != nil {
req, err = http.NewRequest(options.Method, options.Url, bodyWrapper)
Expand All @@ -124,14 +131,17 @@ func NewRequest(options RequestParams) (req *http.Request, err error) {
if bodyWrapper != nil {
return &requestBodyWrapperWithProgress{
body: reqBody,
expectedSize: contentLength,
expectedSize: uint64(contentLength),
callback: options.OnRequestProgress,
}, nil
} else {
return reqBody, nil
}
}
}
if contentLength > 0 {
req.ContentLength = contentLength
}
return
}

Expand Down
4 changes: 3 additions & 1 deletion storagev2/apis/api_add_bucket_event_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
httpclient "github.com/qiniu/go-sdk/v7/storagev2/http_client"
region "github.com/qiniu/go-sdk/v7/storagev2/region"
uptoken "github.com/qiniu/go-sdk/v7/storagev2/uptoken"
"net/http"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -84,6 +85,7 @@ func (storage *Storage) AddBucketEventRule(ctx context.Context, request *AddBuck
} else {
rawQuery += query.Encode()
}
headers := http.Header{}
bucketName := options.OverwrittenBucketName
if bucketName == "" {
var err error
Expand All @@ -105,7 +107,7 @@ func (storage *Storage) AddBucketEventRule(ctx context.Context, request *AddBuck
if err != nil {
return nil, err
}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, OnRequestProgress: options.OnRequestProgress}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, Header: headers, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, OnRequestProgress: options.OnRequestProgress}
if options.OverwrittenEndpoints == nil && options.OverwrittenRegion == nil && storage.client.GetRegions() == nil {
bucketHosts := httpclient.DefaultBucketHosts()
if options.OverwrittenBucketHosts != nil {
Expand Down
4 changes: 3 additions & 1 deletion storagev2/apis/api_add_bucket_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
httpclient "github.com/qiniu/go-sdk/v7/storagev2/http_client"
region "github.com/qiniu/go-sdk/v7/storagev2/region"
uptoken "github.com/qiniu/go-sdk/v7/storagev2/uptoken"
"net/http"
"net/url"
"strconv"
"strings"
Expand Down Expand Up @@ -60,6 +61,7 @@ func (storage *Storage) AddBucketRules(ctx context.Context, request *AddBucketRu
pathSegments = append(pathSegments, "rules", "add")
path := "/" + strings.Join(pathSegments, "/")
var rawQuery string
headers := http.Header{}
body, err := innerRequest.build()
if err != nil {
return nil, err
Expand All @@ -85,7 +87,7 @@ func (storage *Storage) AddBucketRules(ctx context.Context, request *AddBucketRu
if err != nil {
return nil, err
}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, RequestBody: httpclient.GetFormRequestBody(body), OnRequestProgress: options.OnRequestProgress}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, Header: headers, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, RequestBody: httpclient.GetFormRequestBody(body), OnRequestProgress: options.OnRequestProgress}
if options.OverwrittenEndpoints == nil && options.OverwrittenRegion == nil && storage.client.GetRegions() == nil {
bucketHosts := httpclient.DefaultBucketHosts()
if options.OverwrittenBucketHosts != nil {
Expand Down
4 changes: 3 additions & 1 deletion storagev2/apis/api_async_fetch_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
httpclient "github.com/qiniu/go-sdk/v7/storagev2/http_client"
region "github.com/qiniu/go-sdk/v7/storagev2/region"
uptoken "github.com/qiniu/go-sdk/v7/storagev2/uptoken"
"net/http"
"strings"
"time"
)
Expand Down Expand Up @@ -58,6 +59,7 @@ func (storage *Storage) AsyncFetchObject(ctx context.Context, request *AsyncFetc
pathSegments = append(pathSegments, "sisyphus", "fetch")
path := "/" + strings.Join(pathSegments, "/")
var rawQuery string
headers := http.Header{}
body, err := httpclient.GetJsonRequestBody(&innerRequest)
if err != nil {
return nil, err
Expand All @@ -84,7 +86,7 @@ func (storage *Storage) AsyncFetchObject(ctx context.Context, request *AsyncFetc
if err != nil {
return nil, err
}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, BufferResponse: true, RequestBody: body, OnRequestProgress: options.OnRequestProgress}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, Header: headers, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, BufferResponse: true, RequestBody: body, OnRequestProgress: options.OnRequestProgress}
if options.OverwrittenEndpoints == nil && options.OverwrittenRegion == nil && storage.client.GetRegions() == nil {
bucketHosts := httpclient.DefaultBucketHosts()
if bucketName != "" {
Expand Down
4 changes: 3 additions & 1 deletion storagev2/apis/api_batch_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
httpclient "github.com/qiniu/go-sdk/v7/storagev2/http_client"
region "github.com/qiniu/go-sdk/v7/storagev2/region"
uptoken "github.com/qiniu/go-sdk/v7/storagev2/uptoken"
"net/http"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -57,6 +58,7 @@ func (storage *Storage) BatchOps(ctx context.Context, request *BatchOpsRequest,
pathSegments = append(pathSegments, "batch")
path := "/" + strings.Join(pathSegments, "/")
var rawQuery string
headers := http.Header{}
body, err := innerRequest.build()
if err != nil {
return nil, err
Expand All @@ -76,7 +78,7 @@ func (storage *Storage) BatchOps(ctx context.Context, request *BatchOpsRequest,
if err != nil {
return nil, err
}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, BufferResponse: true, RequestBody: httpclient.GetFormRequestBody(body), OnRequestProgress: options.OnRequestProgress}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, Header: headers, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, BufferResponse: true, RequestBody: httpclient.GetFormRequestBody(body), OnRequestProgress: options.OnRequestProgress}
if options.OverwrittenEndpoints == nil && options.OverwrittenRegion == nil && storage.client.GetRegions() == nil {
bucketHosts := httpclient.DefaultBucketHosts()
if bucketName != "" {
Expand Down
4 changes: 3 additions & 1 deletion storagev2/apis/api_check_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
httpclient "github.com/qiniu/go-sdk/v7/storagev2/http_client"
region "github.com/qiniu/go-sdk/v7/storagev2/region"
uptoken "github.com/qiniu/go-sdk/v7/storagev2/uptoken"
"net/http"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -75,6 +76,7 @@ func (storage *Storage) CheckShare(ctx context.Context, request *CheckShareReque
} else {
rawQuery += query.Encode()
}
headers := http.Header{}
bucketName := options.OverwrittenBucketName
uplogInterceptor, err := uplog.NewRequestUplog("checkShare", bucketName, "", func() (string, error) {
credentials := innerRequest.Credentials
Expand All @@ -90,7 +92,7 @@ func (storage *Storage) CheckShare(ctx context.Context, request *CheckShareReque
if err != nil {
return nil, err
}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, OnRequestProgress: options.OnRequestProgress}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, Header: headers, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, OnRequestProgress: options.OnRequestProgress}
if options.OverwrittenEndpoints == nil && options.OverwrittenRegion == nil && storage.client.GetRegions() == nil {
bucketHosts := httpclient.DefaultBucketHosts()
if bucketName != "" {
Expand Down
4 changes: 3 additions & 1 deletion storagev2/apis/api_copy_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
httpclient "github.com/qiniu/go-sdk/v7/storagev2/http_client"
region "github.com/qiniu/go-sdk/v7/storagev2/region"
uptoken "github.com/qiniu/go-sdk/v7/storagev2/uptoken"
"net/http"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -79,6 +80,7 @@ func (storage *Storage) CopyObject(ctx context.Context, request *CopyObjectReque
}
path := "/" + strings.Join(pathSegments, "/")
var rawQuery string
headers := http.Header{}
bucketName := options.OverwrittenBucketName
if bucketName == "" {
var err error
Expand All @@ -101,7 +103,7 @@ func (storage *Storage) CopyObject(ctx context.Context, request *CopyObjectReque
if err != nil {
return nil, err
}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, OnRequestProgress: options.OnRequestProgress}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, Header: headers, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, OnRequestProgress: options.OnRequestProgress}
if options.OverwrittenEndpoints == nil && options.OverwrittenRegion == nil && storage.client.GetRegions() == nil {
bucketHosts := httpclient.DefaultBucketHosts()
if bucketName != "" {
Expand Down
4 changes: 3 additions & 1 deletion storagev2/apis/api_create_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
httpclient "github.com/qiniu/go-sdk/v7/storagev2/http_client"
region "github.com/qiniu/go-sdk/v7/storagev2/region"
uptoken "github.com/qiniu/go-sdk/v7/storagev2/uptoken"
"net/http"
"strings"
"time"
)
Expand Down Expand Up @@ -52,6 +53,7 @@ func (storage *Storage) CreateBucket(ctx context.Context, request *CreateBucketR
}
path := "/" + strings.Join(pathSegments, "/")
var rawQuery string
headers := http.Header{}
bucketName := options.OverwrittenBucketName
uplogInterceptor, err := uplog.NewRequestUplog("createBucket", bucketName, "", func() (string, error) {
credentials := innerRequest.Credentials
Expand All @@ -67,7 +69,7 @@ func (storage *Storage) CreateBucket(ctx context.Context, request *CreateBucketR
if err != nil {
return nil, err
}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, OnRequestProgress: options.OnRequestProgress}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, Header: headers, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, OnRequestProgress: options.OnRequestProgress}
if options.OverwrittenEndpoints == nil && options.OverwrittenRegion == nil && storage.client.GetRegions() == nil {
bucketHosts := httpclient.DefaultBucketHosts()
if options.OverwrittenBucketHosts != nil {
Expand Down
4 changes: 3 additions & 1 deletion storagev2/apis/api_create_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
httpclient "github.com/qiniu/go-sdk/v7/storagev2/http_client"
region "github.com/qiniu/go-sdk/v7/storagev2/region"
uptoken "github.com/qiniu/go-sdk/v7/storagev2/uptoken"
"net/http"
"strings"
"time"
)
Expand Down Expand Up @@ -53,6 +54,7 @@ func (storage *Storage) CreateShare(ctx context.Context, request *CreateShareReq
path := "/" + strings.Join(pathSegments, "/")
path += "/"
var rawQuery string
headers := http.Header{}
body, err := httpclient.GetJsonRequestBody(&innerRequest)
if err != nil {
return nil, err
Expand All @@ -72,7 +74,7 @@ func (storage *Storage) CreateShare(ctx context.Context, request *CreateShareReq
if err != nil {
return nil, err
}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, BufferResponse: true, RequestBody: body, OnRequestProgress: options.OnRequestProgress}
req := httpclient.Request{Method: "POST", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, Header: headers, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, BufferResponse: true, RequestBody: body, OnRequestProgress: options.OnRequestProgress}
if options.OverwrittenEndpoints == nil && options.OverwrittenRegion == nil && storage.client.GetRegions() == nil {
bucketHosts := httpclient.DefaultBucketHosts()
if bucketName != "" {
Expand Down
Loading
Loading