Skip to content

Commit efe124f

Browse files
authored
Add support for ali oss (#327)
1 parent 368f3a4 commit efe124f

File tree

7 files changed

+48
-37
lines changed

7 files changed

+48
-37
lines changed

api/object.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
const (
99
CurrentAccount = "current"
1010
OtherAccount = "other"
11+
PathStyle = "pathStyle"
1112
)
1213

1314
// ListObjectSourcesV2 ListObjectSourcesV2
@@ -62,7 +63,7 @@ func (api *API) ListBucketObjectsV2(c *common.Context) (interface{}, error) {
6263

6364
func (api *API) parseObject(c *common.Context) (*models.ObjectRequestParams, error) {
6465
params := &models.ObjectRequestParams{}
65-
params.ExternalObjectInfo.PathStyle = true
66+
params.ExternalObjectInfo.AddressFormat = PathStyle
6667
if err := c.Bind(params); err != nil {
6768
return nil, err
6869
}

api/object_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ func TestListBucketsV2(t *testing.T) {
8383
assert.Equal(t, http.StatusOK, w.Code)
8484

8585
info := models.ExternalObjectInfo{
86-
Endpoint: "x",
87-
Ak: "xx",
88-
Sk: "xxx",
89-
PathStyle: true,
86+
Endpoint: "x",
87+
Ak: "xx",
88+
Sk: "xxx",
89+
AddressFormat: PathStyle,
9090
}
9191
mkObjectService.EXPECT().ListExternalBuckets(info, "baidubos").Return(buckets, nil).Times(1)
9292
req, _ = http.NewRequest(http.MethodGet, "/v2/objects/baidubos/buckets?account=other&endpoint=x&ak=xx&sk=xxx", nil)
@@ -186,10 +186,10 @@ func TestListBucketObjectsV2(t *testing.T) {
186186
assert.Equal(t, http.StatusNotFound, w4.Code)
187187

188188
info := models.ExternalObjectInfo{
189-
Endpoint: "x",
190-
Ak: "xx",
191-
Sk: "xxx",
192-
PathStyle: true,
189+
Endpoint: "x",
190+
Ak: "xx",
191+
Sk: "xxx",
192+
AddressFormat: PathStyle,
193193
}
194194
mkObjectService.EXPECT().ListExternalBucketObjects(info, "baetyl-test", "baidubos").Return(objectsResult, nil).Times(1)
195195
req, _ = http.NewRequest(http.MethodGet, "/v2/objects/baidubos/buckets/baetyl-test/objects?account=other&endpoint=x&ak=xx&sk=xxx", nil)

models/configuration.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ type ConfigFunctionItem struct {
4040
}
4141

4242
type ConfigObjectItem struct {
43-
Source string `json:"source,omitempty"`
44-
Account string `json:"account,omitempty"`
45-
Endpoint string `json:"endpoint,omitempty"`
46-
Bucket string `json:"bucket,omitempty"`
47-
Object string `json:"object,omitempty"`
48-
Unpack string `json:"unpack,omitempty"`
49-
MD5 string `json:"md5,omitempty"`
50-
Ak string `json:"ak,omitempty"`
51-
Sk string `json:"sk,omitempty"`
43+
Source string `json:"source,omitempty"`
44+
Account string `json:"account,omitempty"`
45+
Endpoint string `json:"endpoint,omitempty"`
46+
Bucket string `json:"bucket,omitempty"`
47+
Object string `json:"object,omitempty"`
48+
Unpack string `json:"unpack,omitempty"`
49+
MD5 string `json:"md5,omitempty"`
50+
Ak string `json:"ak,omitempty"`
51+
Sk string `json:"sk,omitempty"`
52+
AddressFormat string `json:"addressFormat,omitempty" default:"pathStyle"`
5253
}
5354

5455
func EqualConfig(config1, config2 *specV1.Configuration) bool {

models/object.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ type ObjectRequestParams struct {
9393
}
9494

9595
type ExternalObjectInfo struct {
96-
Endpoint string `form:"endpoint,omitempty"`
97-
Ak string `form:"ak,omitempty"`
98-
Sk string `form:"sk,omitempty"`
99-
PathStyle bool `form:"pathStyle,omitempty"`
96+
Endpoint string `form:"endpoint,omitempty"`
97+
Ak string `form:"ak,omitempty"`
98+
Sk string `form:"sk,omitempty"`
99+
AddressFormat string `form:"addressFormat,omitempty"`
100100
}

plugin/awss3/awss3.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import (
2121
"github.com/baetyl/baetyl-cloud/v2/plugin"
2222
)
2323

24+
const (
25+
VirtualHost = "virtualHost"
26+
)
27+
2428
type awss3Storage struct {
2529
s3Client *s3.S3
2630
cfg *S3Config
@@ -41,7 +45,7 @@ func New() (plugin.Plugin, error) {
4145
return new(awss3Storage), nil
4246
}
4347

44-
sessionProvider, err := newS3Session(cfg.AWSS3.Endpoint, cfg.AWSS3.Ak, cfg.AWSS3.Sk, cfg.AWSS3.Region, cfg.AWSS3.PathStyle)
48+
sessionProvider, err := newS3Session(cfg.AWSS3.Endpoint, cfg.AWSS3.Ak, cfg.AWSS3.Sk, cfg.AWSS3.Region, cfg.AWSS3.AddressFormat)
4549
if err != nil {
4650
return nil, err
4751
}
@@ -261,7 +265,7 @@ func (c *awss3Storage) GenInternalObjectURL(_, bucket, object string) (*models.O
261265

262266
// ListExternalBuckets ListExternalBuckets
263267
func (c *awss3Storage) ListExternalBuckets(info models.ExternalObjectInfo) ([]models.Bucket, error) {
264-
sessionProvider, err := newS3Session(info.Endpoint, info.Ak, info.Sk, "", info.PathStyle)
268+
sessionProvider, err := newS3Session(info.Endpoint, info.Ak, info.Sk, "", info.AddressFormat)
265269
if err != nil {
266270
return nil, err
267271
}
@@ -271,7 +275,7 @@ func (c *awss3Storage) ListExternalBuckets(info models.ExternalObjectInfo) ([]mo
271275

272276
// HeadExternalBucket HeadExternalBucket
273277
func (c *awss3Storage) HeadExternalBucket(info models.ExternalObjectInfo, bucket string) error {
274-
sessionProvider, err := newS3Session(info.Endpoint, info.Ak, info.Sk, "", info.PathStyle)
278+
sessionProvider, err := newS3Session(info.Endpoint, info.Ak, info.Sk, "", info.AddressFormat)
275279
if err != nil {
276280
return err
277281
}
@@ -281,7 +285,7 @@ func (c *awss3Storage) HeadExternalBucket(info models.ExternalObjectInfo, bucket
281285

282286
// ListExternalBucketObjects ListExternalBucketObjects
283287
func (c *awss3Storage) ListExternalBucketObjects(info models.ExternalObjectInfo, bucket string, params *models.ObjectParams) (*models.ListObjectsResult, error) {
284-
sessionProvider, err := newS3Session(info.Endpoint, info.Ak, info.Sk, "", info.PathStyle)
288+
sessionProvider, err := newS3Session(info.Endpoint, info.Ak, info.Sk, "", info.AddressFormat)
285289
if err != nil {
286290
return nil, err
287291
}
@@ -298,7 +302,7 @@ func (c *awss3Storage) ListExternalBucketObjects(info models.ExternalObjectInfo,
298302

299303
// GenExternalObjectURL GenExternalObjectURL
300304
func (c *awss3Storage) GenExternalObjectURL(info models.ExternalObjectInfo, bucket, object string) (*models.ObjectURL, error) {
301-
sessionProvider, err := newS3Session(info.Endpoint, info.Ak, info.Sk, "", info.PathStyle)
305+
sessionProvider, err := newS3Session(info.Endpoint, info.Ak, info.Sk, "", info.AddressFormat)
302306
if err != nil {
303307
return nil, err
304308
}
@@ -325,11 +329,15 @@ func (c *awss3Storage) checkInternalSupported() error {
325329
return nil
326330
}
327331

328-
func newS3Session(endpoint, ak, sk, region string, pathStyle bool) (*session.Session, error) {
332+
func newS3Session(endpoint, ak, sk, region, addressFormat string) (*session.Session, error) {
329333
if region == "" {
330334
region = "us-east-1"
331335
}
332336

337+
pathStyle := true
338+
if addressFormat == VirtualHost {
339+
pathStyle = false
340+
}
333341
s3Config := &aws.Config{
334342
Credentials: credentials.NewStaticCredentials(ak, sk, ""),
335343
Endpoint: aws.String(endpoint),

plugin/awss3/awss3_config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ type CloudConfig struct {
88
}
99

1010
type S3Config struct {
11-
Endpoint string `yaml:"endpoint" json:"endpoint"`
12-
Ak string `yaml:"ak" json:"ak" validate:"nonzero"`
13-
Sk string `yaml:"sk" json:"sk" validate:"nonzero"`
14-
Region string `yaml:"region" json:"region" default:"us-east-1"`
15-
PathStyle bool `yaml:"pathStyle" json:"pathStyle" default:"true"`
16-
Expiration time.Duration `yaml:"expiration" json:"expiration" default:"1h"`
11+
Endpoint string `yaml:"endpoint" json:"endpoint"`
12+
Ak string `yaml:"ak" json:"ak" validate:"nonzero"`
13+
Sk string `yaml:"sk" json:"sk" validate:"nonzero"`
14+
Region string `yaml:"region" json:"region" default:"us-east-1"`
15+
AddressFormat string `yaml:"addressFormat" json:"addressFormat" default:"pathStyle"`
16+
Expiration time.Duration `yaml:"expiration" json:"expiration" default:"1h"`
1717
}

service/sync.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,10 @@ func (t *SyncServiceImpl) PopulateConfigObject(k, v string, cfg *specV1.Configur
212212
res, err = t.ObjectService.GenInternalObjectURL(obj.Metadata["userID"], item.Bucket, item.Object, item.Source)
213213
} else {
214214
res, err = t.ObjectService.GenExternalObjectURL(models.ExternalObjectInfo{
215-
Endpoint: item.Endpoint,
216-
Ak: item.Ak,
217-
Sk: item.Sk,
215+
Endpoint: item.Endpoint,
216+
Ak: item.Ak,
217+
Sk: item.Sk,
218+
AddressFormat: item.AddressFormat,
218219
}, item.Bucket, item.Object, item.Source)
219220
}
220221
if err != nil {

0 commit comments

Comments
 (0)