Skip to content

Commit 64dfef8

Browse files
author
YangSen-qn
committed
Merge branch 'master' of YangSen-qn:qiniu/go-sdk
2 parents e751985 + df3d811 commit 64dfef8

File tree

6 files changed

+108
-9
lines changed

6 files changed

+108
-9
lines changed

internal/clientv2/request.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"fmt"
78
"io"
89
"net/http"
910
"net/url"
@@ -85,7 +86,7 @@ func (o *RequestParams) init() {
8586
func NewRequest(options RequestParams) (req *http.Request, err error) {
8687
var (
8788
bodyWrapper *requestBodyWrapperWithProgress = nil
88-
contentLength uint64
89+
contentLength int64 = 0
8990
)
9091

9192
options.init()
@@ -94,11 +95,17 @@ func NewRequest(options RequestParams) (req *http.Request, err error) {
9495
if err != nil {
9596
return nil, err
9697
}
97-
if options.OnRequestProgress != nil && body != nil {
98+
99+
if body != nil {
98100
if contentLengthHeaderValue := options.Header.Get("Content-Length"); contentLengthHeaderValue != "" {
99-
contentLength, _ = strconv.ParseUint(contentLengthHeaderValue, 10, 64)
101+
contentLength, err = strconv.ParseInt(contentLengthHeaderValue, 10, 64)
102+
if err != nil {
103+
return nil, fmt.Errorf("invalid Content-Length header value: %s, %w", contentLengthHeaderValue, err)
104+
}
105+
}
106+
if options.OnRequestProgress != nil {
107+
bodyWrapper = &requestBodyWrapperWithProgress{body: body, expectedSize: uint64(contentLength), callback: options.OnRequestProgress}
100108
}
101-
bodyWrapper = &requestBodyWrapperWithProgress{body: body, expectedSize: contentLength, callback: options.OnRequestProgress}
102109
}
103110
if bodyWrapper != nil {
104111
req, err = http.NewRequest(options.Method, options.Url, bodyWrapper)
@@ -124,14 +131,17 @@ func NewRequest(options RequestParams) (req *http.Request, err error) {
124131
if bodyWrapper != nil {
125132
return &requestBodyWrapperWithProgress{
126133
body: reqBody,
127-
expectedSize: contentLength,
134+
expectedSize: uint64(contentLength),
128135
callback: options.OnRequestProgress,
129136
}, nil
130137
} else {
131138
return reqBody, nil
132139
}
133140
}
134141
}
142+
if contentLength > 0 {
143+
req.ContentLength = contentLength
144+
}
135145
return
136146
}
137147

storagev2/apis/query_bucket_v4/api.go

+45-1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,44 @@ func (j *ApiDomains) validate() error {
229229
return nil
230230
}
231231

232+
// 主 S3 域名列表
233+
type PreferedS3Domains = []string
234+
235+
// S3 域名
236+
type S3Domains struct {
237+
RegionAlias string // S3 Region
238+
PreferedApiDomains PreferedS3Domains // 主 S3 域名列表
239+
}
240+
type jsonS3Domains struct {
241+
RegionAlias string `json:"region_alias"` // S3 Region
242+
PreferedApiDomains PreferedS3Domains `json:"domains"` // 主 S3 域名列表
243+
}
244+
245+
func (j *S3Domains) MarshalJSON() ([]byte, error) {
246+
if err := j.validate(); err != nil {
247+
return nil, err
248+
}
249+
return json.Marshal(&jsonS3Domains{RegionAlias: j.RegionAlias, PreferedApiDomains: j.PreferedApiDomains})
250+
}
251+
func (j *S3Domains) UnmarshalJSON(data []byte) error {
252+
var nj jsonS3Domains
253+
if err := json.Unmarshal(data, &nj); err != nil {
254+
return err
255+
}
256+
j.RegionAlias = nj.RegionAlias
257+
j.PreferedApiDomains = nj.PreferedApiDomains
258+
return nil
259+
}
260+
func (j *S3Domains) validate() error {
261+
if j.RegionAlias == "" {
262+
return errors.MissingRequiredFieldError{Name: "RegionAlias"}
263+
}
264+
if len(j.PreferedApiDomains) == 0 {
265+
return errors.MissingRequiredFieldError{Name: "PreferedApiDomains"}
266+
}
267+
return nil
268+
}
269+
232270
// 存储空间服务域名
233271
type BucketQueryHost struct {
234272
RegionId string // 区域 ID
@@ -239,6 +277,7 @@ type BucketQueryHost struct {
239277
RsDomains RsDomains // 对象管理域名
240278
RsfDomains RsfDomains // 对象列举域名
241279
ApiDomains ApiDomains // API 域名
280+
S3Domains S3Domains // S3 域名
242281
}
243282
type jsonBucketQueryHost struct {
244283
RegionId string `json:"region"` // 区域 ID
@@ -249,13 +288,14 @@ type jsonBucketQueryHost struct {
249288
RsDomains RsDomains `json:"rs"` // 对象管理域名
250289
RsfDomains RsfDomains `json:"rsf"` // 对象列举域名
251290
ApiDomains ApiDomains `json:"api"` // API 域名
291+
S3Domains S3Domains `json:"s3"` // S3 域名
252292
}
253293

254294
func (j *BucketQueryHost) MarshalJSON() ([]byte, error) {
255295
if err := j.validate(); err != nil {
256296
return nil, err
257297
}
258-
return json.Marshal(&jsonBucketQueryHost{RegionId: j.RegionId, TimeToLive: j.TimeToLive, UpDomains: j.UpDomains, IoDomains: j.IoDomains, IoSrcDomains: j.IoSrcDomains, RsDomains: j.RsDomains, RsfDomains: j.RsfDomains, ApiDomains: j.ApiDomains})
298+
return json.Marshal(&jsonBucketQueryHost{RegionId: j.RegionId, TimeToLive: j.TimeToLive, UpDomains: j.UpDomains, IoDomains: j.IoDomains, IoSrcDomains: j.IoSrcDomains, RsDomains: j.RsDomains, RsfDomains: j.RsfDomains, ApiDomains: j.ApiDomains, S3Domains: j.S3Domains})
259299
}
260300
func (j *BucketQueryHost) UnmarshalJSON(data []byte) error {
261301
var nj jsonBucketQueryHost
@@ -270,6 +310,7 @@ func (j *BucketQueryHost) UnmarshalJSON(data []byte) error {
270310
j.RsDomains = nj.RsDomains
271311
j.RsfDomains = nj.RsfDomains
272312
j.ApiDomains = nj.ApiDomains
313+
j.S3Domains = nj.S3Domains
273314
return nil
274315
}
275316
func (j *BucketQueryHost) validate() error {
@@ -294,6 +335,9 @@ func (j *BucketQueryHost) validate() error {
294335
if err := j.ApiDomains.validate(); err != nil {
295336
return err
296337
}
338+
if err := j.S3Domains.validate(); err != nil {
339+
return err
340+
}
297341
return nil
298342
}
299343

storagev2/apistest/apis_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/qiniu/go-sdk/v7/storagev2/errors"
1919
"github.com/qiniu/go-sdk/v7/storagev2/http_client"
2020
"github.com/qiniu/go-sdk/v7/storagev2/uptoken"
21+
"github.com/stretchr/testify/require"
2122
)
2223

2324
var (
@@ -105,3 +106,22 @@ func TestCreateBucket(t *testing.T) {
105106
t.FailNow()
106107
}
107108
}
109+
110+
func TestQuery(t *testing.T) {
111+
credentials := credentials.NewCredentials(testAK, testSK)
112+
113+
storage := apis.NewStorage(&http_client.Options{Credentials: credentials})
114+
115+
response, err := storage.QueryBucketV4(context.Background(), &apis.QueryBucketV4Request{
116+
Bucket: testBucket,
117+
AccessKey: testAK,
118+
}, nil)
119+
120+
require.NoError(t, err)
121+
122+
for _, host := range response.Hosts {
123+
require.NotNil(t, host.S3Domains)
124+
require.NotEmpty(t, host.S3Domains.RegionAlias)
125+
require.NotEmpty(t, host.S3Domains.PreferedApiDomains)
126+
}
127+
}

storagev2/internal/api-specs/query_bucket_v4.yml

+20
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,23 @@ response:
154154
name: PreferedApiDomains
155155
documentation: 主 API 域名列表
156156
type: string
157+
- field_name: s3_domains
158+
key: s3
159+
documentation: S3 域名
160+
type:
161+
struct:
162+
name: S3Domains
163+
documentation: S3 域名
164+
fields:
165+
- field_name: region_alias
166+
key: region_alias
167+
documentation: S3 Region
168+
type: string
169+
- field_name: prefered_api_domains
170+
key: domains
171+
documentation: 主 S3 域名列表
172+
type:
173+
array:
174+
name: PreferedS3Domains
175+
documentation: 主 S3 域名列表
176+
type: string

storagev2/internal/utils/http.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"io"
55
"net/http"
66
"strconv"
7+
8+
innerio "github.com/qiniu/go-sdk/v7/internal/io"
79
)
810

9-
func HttpHeadAddContentLength(header http.Header, data io.ReadSeekCloser) error {
11+
func HttpHeadAddContentLength(header http.Header, data innerio.ReadSeekCloser) error {
1012
_, err := data.Seek(0, io.SeekStart)
1113
if err != nil {
1214
return err

storagev2/internal/utils/http_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build unit
2+
// +build unit
3+
14
package utils
25

36
import (
@@ -7,7 +10,7 @@ import (
710
"net/http"
811
"testing"
912

10-
internal_io "github.com/qiniu/go-sdk/v7/internal/io"
13+
innerio "github.com/qiniu/go-sdk/v7/internal/io"
1114
)
1215

1316
// 模拟 io.ReadSeekCloser 以模拟错误
@@ -29,7 +32,7 @@ func (e *errorSeeker) Seek(offset int64, whence int) (int64, error) {
2932

3033
func TestHttpHeadAddContentLength_Success(t *testing.T) {
3134
data := bytes.NewReader([]byte("test"))
32-
dataS := internal_io.MakeReadSeekCloserFromLimitedReader(data, data.Size())
35+
dataS := innerio.MakeReadSeekCloserFromLimitedReader(data, data.Size())
3336

3437
header := http.Header{}
3538
err := HttpHeadAddContentLength(header, dataS)

0 commit comments

Comments
 (0)