Skip to content

Commit

Permalink
Merge branch 'master' into dev-2306-access-point
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzong18 authored Oct 19, 2023
2 parents e763020 + dec10ed commit b3a61b9
Show file tree
Hide file tree
Showing 19 changed files with 1,402 additions and 130 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# ChangeLog - Aliyun OSS SDK for Go

## 版本号:v2.2.9 日期:2023-08-25
### 变更内容
- 增加:support force path style option.
- 增加:support context.Context option.
- 修改:remove LifecycleFilterNot.Prefix omitempty attribute.


## 版本号:v2.2.8 日期:2023-07-31
### 变更内容
- 增加:support EnvironmentVariableCredentialsProvider
- 增加:support describe regions api.
- 增加:support create bucket with server encryption parameters.
- 增加:support referer black list.
- 增加:support aysnc process object api.
- 增加:support ObjectSizeGreaterThan and ObjectSizeLessThan in lifecycle rule.
- 增加:add DeepColdArchive storage class.
- 修复:fix bug.

## 版本号:v2.2.7 日期:2023-03-23
### 变更内容
- 增加:support get info form EC & x-oss-err.
Expand Down
2 changes: 1 addition & 1 deletion README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
> - 使用此SDK,用户可以方便地在任何应用、任何时间、任何地点上传,下载和管理数据。
## 版本
> - Current version: v2.2.7
> - Current version: v2.2.9
## 运行环境
> - Go 1.5及以上。
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
> - With this SDK, you can upload, download and manage data on any app anytime and anywhere conveniently.
## Version
> - Current version: v2.2.7
> - Current version: v2.2.9
## Running Environment
> - Go 1.5 or above.
## Installing
### Install the SDK through GitHub
> - Run the 'go get github.com/aliyun/aliyun-oss-go-sdk/oss' command to get the remote code package.
> - Run the 'go get github.com/aliyun/aliyun-oss-go-sdk' command to get the remote code package.
> - Use 'import "github.com/aliyun/aliyun-oss-go-sdk/oss"' in your code to introduce OSS Go SDK package.
## Getting Started
Expand Down
163 changes: 91 additions & 72 deletions oss/bucket.go

Large diffs are not rendered by default.

208 changes: 208 additions & 0 deletions oss/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package oss

import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha1"
"crypto/tls"
Expand Down Expand Up @@ -2279,6 +2280,182 @@ func (s *OssBucketSuite) TestGetConfig(c *C) {
c.Assert(bucket.GetConfig().IsEnableMD5, Equals, false)
}

func (s *OssBucketSuite) TestForcePathStyle(c *C) {
url, err := url.ParseRequestURI(endpoint)
client, err := New(endpoint, accessID, accessKey, ForcePathStyle(true))
c.Assert(err, IsNil)

_, err = client.GetBucketInfo(bucketName)
c.Assert(err, NotNil)
c.Assert(err.(ServiceError).Code, Equals, "SecondLevelDomainForbidden")
c.Assert(err.(ServiceError).HostID, Equals, url.Host)

bucket, err := client.Bucket(bucketName)
c.Assert(err, IsNil)

c.Assert(bucket.GetConfig().IsPathStyle, Equals, true)

objectName := "demo.txt"

err = bucket.PutObject(objectName, strings.NewReader("hi oss"))
c.Assert(err, NotNil)
c.Assert(err.(ServiceError).Code, Equals, "SecondLevelDomainForbidden")

str, err := bucket.SignURL(objectName, HTTPPut, 3600)
c.Assert(err, IsNil)
strUrl := endpoint + "/" + bucketName + "/" + objectName
c.Assert(strings.Contains(str, strUrl), Equals, true)
}

func (s *OssBucketSuite) TestUseCname(c *C) {
url, err := url.ParseRequestURI(endpoint)
c.Assert(err, IsNil)
cnameEndpoint := bucketName + "." + url.Host
client, err := New(cnameEndpoint, accessID, accessKey, UseCname(true))
c.Assert(err, IsNil)

info, err := client.GetBucketInfo(bucketName)

c.Assert(err, IsNil)
c.Assert(info.BucketInfo.Name, Equals, bucketName)

client, err = New(cnameEndpoint, accessID, accessKey)
_, err = client.GetBucketInfo(bucketName)
c.Assert(err, NotNil)
c.Assert(err.(ServiceError).HostID, Equals, bucketName+"."+cnameEndpoint)
}

func (s *OssBucketSuite) TestContextTimeout(c *C) {
client, err := New(endpoint, accessID, accessKey)
c.Assert(err, IsNil)

bucket, err := client.Bucket(bucketName)
c.Assert(err, IsNil)

objectName := objectNamePrefix + RandStr(8)
objectValue := "红藕香残玉簟秋。轻解罗裳,独上兰舟。云中谁寄锦书来?雁字回时,月满西楼。"

bucketNameDest := bucketNamePrefix + RandLowStr(8)
c.Assert(err, IsNil)

objectNameDest := objectName + RandLowStr(5)

// Put
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 1*time.Nanosecond)
defer cancel()
err = bucket.PutObject(objectName, strings.NewReader(objectValue), WithContext(ctx))
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), "context deadline exceeded"), Equals, true)

// Get not exist object
_, err = bucket.GetObject(objectName)
c.Assert(err, NotNil)
c.Assert(err.(ServiceError).StatusCode, Equals, 404)

err = s.bucket.PutObject(objectName, strings.NewReader(objectValue))
c.Assert(err, IsNil)

// Get
_, err = bucket.GetObject(objectName, WithContext(ctx))
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), "context deadline exceeded"), Equals, true)

// CopyObjectTo
_, err = bucket.CopyObjectTo(bucketNameDest, objectNameDest, objectName, WithContext(ctx))
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), "context deadline exceeded"), Equals, true)
// Delete
err = s.bucket.DeleteObject(objectName)
c.Assert(err, IsNil)

var nextPos int64
// String append
nextPos, err = s.bucket.AppendObject(objectName, strings.NewReader("红藕香残玉簟秋。轻解罗裳,独上兰舟。"), nextPos, WithContext(ctx))
c.Assert(err, NotNil)

// Put with URL
signedURL, err := bucket.SignURL(objectName, HTTPPut, 3600)
c.Assert(err, IsNil)

err = bucket.PutObjectWithURL(signedURL, strings.NewReader(objectValue), WithContext(ctx))
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), "context deadline exceeded"), Equals, true)

// Get not exist object
_, err = bucket.GetObject(objectName)
c.Assert(err, NotNil)
c.Assert(err.(ServiceError).StatusCode, Equals, 404)

err = s.bucket.PutObject(objectName, strings.NewReader(objectValue))
c.Assert(err, IsNil)

// Get with URL
signedURL, err = bucket.SignURL(objectName, HTTPGet, 3600)
c.Assert(err, IsNil)

_, err = bucket.GetObjectWithURL(signedURL, WithContext(ctx))
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), "context deadline exceeded"), Equals, true)

err = s.bucket.DeleteObject(objectName)
c.Assert(err, IsNil)

content := RandStr(1024 * 1024)
var fileName = "test-file-" + RandStr(8)
CreateFile(fileName, content, c)

fd, err := os.Open(fileName)
c.Assert(err, IsNil)
defer fd.Close()

err = bucket.PutObjectFromFile(objectName, fileName, WithContext(ctx))
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), "context deadline exceeded"), Equals, true)

os.Remove(fileName)
}

func (s *OssBucketSuite) TestContextTimeoutBigFile(c *C) {
client, err := New(endpoint, accessID, accessKey)
c.Assert(err, IsNil)

bucket, err := client.Bucket(bucketName)
c.Assert(err, IsNil)

objectName := objectNamePrefix + RandStr(8)

//big file
content := RandStr(5 * 1024 * 1024)
var fileName = "test-file-" + RandStr(8)
CreateFile(fileName, content, c)

fd, err := os.Open(fileName)
c.Assert(err, IsNil)
defer fd.Close()

ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
objectNameBigfile := objectName + "-bigfile"
err = bucket.PutObjectFromFile(objectNameBigfile, fileName, WithContext(ctx))
if ctx != nil {
select {
case <-ctx.Done():
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), "context deadline exceeded"), Equals, true)
default:
c.Fail()
}
}

// Get not exist object
_, err = bucket.GetObject(objectNameBigfile)
c.Assert(err, NotNil)
c.Assert(err.(ServiceError).StatusCode, Equals, 404)

os.Remove(fileName)
}

func (s *OssBucketSuite) TestSTSToken(c *C) {
objectName := objectNamePrefix + RandStr(8)
objectValue := "红藕香残玉簟秋。轻解罗裳,独上兰舟。云中谁寄锦书来?雁字回时,月满西楼。"
Expand Down Expand Up @@ -2648,6 +2825,37 @@ func (s *OssBucketSuite) TestProcessObject(c *C) {
c.Assert(err, NotNil)
}

// TestAsyncProcessObject
func (s *OssBucketSuite) TestAsyncProcessObject(c *C) {
videoUrl := "https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/video.mp4?spm=a2c4g.64555.0.0.515675979u4B8w&file=video.mp4"
fileName := "video.mp4"
// 发起get请求
resp, err := http.Get(videoUrl)
c.Assert(err, IsNil)
defer resp.Body.Close()

// 创建文件
file, err := os.Create(fileName)
c.Assert(err, IsNil)
defer file.Close()
_, err = io.Copy(file, resp.Body)
c.Assert(err, IsNil)

err = s.bucket.PutObjectFromFile("demo.avi", fileName)
c.Assert(err, IsNil)

sourceImageName := "demo.avi"
style := "video/convert,f_avi,vcodec_h265,s_1920x1080,vb_2000000,fps_30,acodec_aac,ab_100000,sn_1"
targetObject := "demo"
process := fmt.Sprintf("%s|sys/saveas,b_%v,o_%v", style, strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(bucketName)), "="), strings.TrimRight(base64.URLEncoding.EncodeToString([]byte(targetObject)), "="))
_, err = s.bucket.AsyncProcessObject(sourceImageName, process)
c.Assert(err, NotNil)
c.Assert(err.(ServiceError).Code, Equals, "Imm Client")
c.Assert(strings.Contains(err.(ServiceError).Message, "ResourceNotFound, The specified resource Attachment is not found"), Equals, true)

os.Remove(fileName)
}

// Private
func CreateFileAndWrite(fileName string, data []byte) error {
os.Remove(fileName)
Expand Down
Loading

0 comments on commit b3a61b9

Please sign in to comment.