Skip to content

Commit a69112b

Browse files
authored
1.0.0-devpreview
1 parent 590b72e commit a69112b

File tree

101 files changed

+53232
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+53232
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# ChangeLog - Alibaba Cloud OSS SDK for Go v2
2+

README-CN.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# Alibaba Cloud OSS SDK for Go v2
2+
3+
[![GitHub version](https://badge.fury.io/gh/aliyun%2Falibabacloud-oss-go-sdk-v2.svg)](https://badge.fury.io/gh/aliyun%2Falibabacloud-oss-go-sdk-v2)
4+
5+
alibabacloud-oss-go-sdk-v2 是OSS在Go编译语言下的第二版SDK, 处于开发预览版状态
6+
7+
## [README in English](README.md)
8+
9+
## 关于
10+
> - 此Go SDK基于[阿里云对象存储服务](http://www.aliyun.com/product/oss/)官方API构建。
11+
> - 阿里云对象存储(Object Storage Service,简称OSS),是阿里云对外提供的海量,安全,低成本,高可靠的云存储服务。
12+
> - OSS适合存放任意文件类型,适合各种网站、开发企业及开发者使用。
13+
> - 使用此SDK,用户可以方便地在任何应用、任何时间、任何地点上传,下载和管理数据。
14+
15+
## 运行环境
16+
> - Go 1.18及以上。
17+
18+
## 安装方法
19+
### GitHub安装
20+
> - 执行命令`go get github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss`获取远程代码包。
21+
> - 在您的代码中使用`import "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"`引入OSS Go SDK的包。
22+
23+
## 快速使用
24+
#### 获取存储空间列表(List Bucket)
25+
```go
26+
package main
27+
28+
import (
29+
"context"
30+
"fmt"
31+
"log"
32+
33+
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
34+
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
35+
)
36+
37+
func main() {
38+
var (
39+
region = "cn-hangzhou"
40+
)
41+
42+
// Using the SDK's default configuration
43+
// loading credentials values from the environment variables
44+
cfg := oss.LoadDefaultConfig().
45+
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
46+
WithRegion(region)
47+
48+
client := oss.NewClient(cfg)
49+
50+
// Create the Paginator for the ListBuckets operation.
51+
p := client.NewListBucketsPaginator(&oss.ListBucketsRequest{})
52+
53+
// Iterate through the bucket pages
54+
var i int
55+
fmt.Println("Buckets:")
56+
for p.HasNext() {
57+
i++
58+
page, err := p.NextPage(context.TODO())
59+
if err != nil {
60+
log.Fatalf("failed to get page %v, %v", i, err)
61+
}
62+
// Print the bucket found
63+
for _, b := range page.Buckets {
64+
fmt.Printf("Bucket:%v, %v, %v\n", oss.ToString(b.Name), oss.ToString(b.StorageClass), oss.ToString(b.Location))
65+
}
66+
}
67+
}
68+
```
69+
70+
#### 获取文件列表(List Objects)
71+
```go
72+
package main
73+
74+
import (
75+
"context"
76+
"fmt"
77+
"log"
78+
79+
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
80+
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
81+
)
82+
83+
func main() {
84+
var (
85+
region = "cn-hangzhou"
86+
bucketName = "your bucket name"
87+
)
88+
89+
// Using the SDK's default configuration
90+
// loading credentials values from the environment variables
91+
cfg := oss.LoadDefaultConfig().
92+
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
93+
WithRegion(region)
94+
95+
client := oss.NewClient(cfg)
96+
97+
// Create the Paginator for the ListObjects operation.
98+
p := client.NewListObjectsPaginator(&oss.ListObjectsRequest{
99+
Bucket: oss.Ptr(bucketName),
100+
})
101+
102+
// Iterate through the object pages
103+
var i int
104+
fmt.Println("Objects:")
105+
for p.HasNext() {
106+
i++
107+
108+
page, err := p.NextPage(context.TODO())
109+
if err != nil {
110+
log.Fatalf("failed to get page %v, %v", i, err)
111+
}
112+
113+
// Print the objects found
114+
for _, obj := range page.Contents {
115+
fmt.Printf("Object:%v, %v, %v\n", oss.ToString(obj.Key), obj.Size, oss.ToTime(obj.LastModified))
116+
}
117+
}
118+
}
119+
```
120+
121+
#### 上传文件(Put Object)
122+
```go
123+
package main
124+
125+
import (
126+
"context"
127+
"fmt"
128+
"log"
129+
"os"
130+
131+
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
132+
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
133+
)
134+
135+
func main() {
136+
var (
137+
region = "cn-hangzhou"
138+
bucketName = "your bucket name"
139+
objectName = "your object name"
140+
localFile = "your local file path"
141+
)
142+
143+
// Using the SDK's default configuration
144+
// loading credentials values from the environment variables
145+
cfg := oss.LoadDefaultConfig().
146+
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
147+
WithRegion(region)
148+
149+
client := oss.NewClient(cfg)
150+
151+
file, err := os.Open(localFile)
152+
if err != nil {
153+
log.Fatalf("failed to open file %v", err)
154+
}
155+
defer file.Close()
156+
157+
result, err := client.PutObject(context.TODO(), &oss.PutObjectRequest{
158+
Bucket: oss.Ptr(bucketName),
159+
Key: oss.Ptr(objectName),
160+
Body: file,
161+
})
162+
163+
if err != nil {
164+
log.Fatalf("failed to put object %v", err)
165+
}
166+
167+
fmt.Printf("put object sucessfully, ETag :%v\n", result.ETag)
168+
}
169+
```
170+
171+
## 更多示例
172+
请参看`sample`目录
173+
174+
### 运行示例
175+
> - 进入示例程序目录 `sample`
176+
> - 通过环境变量,配置访问凭证, `export OSS_ACCESS_KEY_ID="your access key id"`, `export OSS_ACCESS_KEY_SECRET="your access key secrect"`
177+
> - 以 list_buckets.go 为例,执行 `go run list_buckets.go -region cn-hangzhou`
178+
179+
## 许可协议
180+
> - Apache-2.0, 请参阅 [许可文件](LICENSE)
181+

README.md

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# Alibaba Cloud OSS SDK for Go v2
2+
3+
[![GitHub version](https://badge.fury.io/gh/aliyun%2Falibabacloud-oss-go-sdk-v2.svg)](https://badge.fury.io/gh/aliyun%2Falibabacloud-oss-go-sdk-v2)
4+
5+
alibabacloud-oss-go-sdk-v2 is the Developer Preview for the v2 of the OSS SDK for the Go programming language
6+
7+
## [README in Chinese](README-CN.md)
8+
9+
## About
10+
> - This Go SDK is based on the official APIs of [Alibaba Cloud OSS](http://www.aliyun.com/product/oss/).
11+
> - Alibaba Cloud Object Storage Service (OSS) is a cloud storage service provided by Alibaba Cloud, featuring massive capacity, security, a low cost, and high reliability.
12+
> - The OSS can store any type of files and therefore applies to various websites, development enterprises and developers.
13+
> - With this SDK, you can upload, download and manage data on any app anytime and anywhere conveniently.
14+
15+
## Running Environment
16+
> - Go 1.18 or above.
17+
18+
## Installing
19+
### Install the SDK through GitHub
20+
> - Run the `go get github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss` command to get the remote code package.
21+
> - Use `import "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"` in your code to introduce OSS Go SDK package.
22+
23+
## Getting Started
24+
#### List Bucket
25+
```go
26+
package main
27+
28+
import (
29+
"context"
30+
"fmt"
31+
"log"
32+
33+
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
34+
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
35+
)
36+
37+
func main() {
38+
var (
39+
region = "cn-hangzhou"
40+
)
41+
42+
// Using the SDK's default configuration
43+
// loading credentials values from the environment variables
44+
cfg := oss.LoadDefaultConfig().
45+
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
46+
WithRegion(region)
47+
48+
client := oss.NewClient(cfg)
49+
50+
// Create the Paginator for the ListBuckets operation.
51+
p := client.NewListBucketsPaginator(&oss.ListBucketsRequest{})
52+
53+
// Iterate through the bucket pages
54+
var i int
55+
fmt.Println("Buckets:")
56+
for p.HasNext() {
57+
i++
58+
page, err := p.NextPage(context.TODO())
59+
if err != nil {
60+
log.Fatalf("failed to get page %v, %v", i, err)
61+
}
62+
// Print the bucket found
63+
for _, b := range page.Buckets {
64+
fmt.Printf("Bucket:%v, %v, %v\n", oss.ToString(b.Name), oss.ToString(b.StorageClass), oss.ToString(b.Location))
65+
}
66+
}
67+
}
68+
```
69+
70+
#### List Objects
71+
```go
72+
package main
73+
74+
import (
75+
"context"
76+
"fmt"
77+
"log"
78+
79+
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
80+
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
81+
)
82+
83+
func main() {
84+
var (
85+
region = "cn-hangzhou"
86+
bucketName = "your bucket name"
87+
)
88+
89+
// Using the SDK's default configuration
90+
// loading credentials values from the environment variables
91+
cfg := oss.LoadDefaultConfig().
92+
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
93+
WithRegion(region)
94+
95+
client := oss.NewClient(cfg)
96+
97+
// Create the Paginator for the ListObjects operation.
98+
p := client.NewListObjectsPaginator(&oss.ListObjectsRequest{
99+
Bucket: oss.Ptr(bucketName),
100+
})
101+
102+
// Iterate through the object pages
103+
var i int
104+
fmt.Println("Objects:")
105+
for p.HasNext() {
106+
i++
107+
108+
page, err := p.NextPage(context.TODO())
109+
if err != nil {
110+
log.Fatalf("failed to get page %v, %v", i, err)
111+
}
112+
113+
// Print the objects found
114+
for _, obj := range page.Contents {
115+
fmt.Printf("Object:%v, %v, %v\n", oss.ToString(obj.Key), obj.Size, oss.ToTime(obj.LastModified))
116+
}
117+
}
118+
}
119+
```
120+
121+
#### Put Object
122+
```go
123+
package main
124+
125+
import (
126+
"context"
127+
"fmt"
128+
"log"
129+
"os"
130+
131+
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
132+
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
133+
)
134+
135+
func main() {
136+
var (
137+
region = "cn-hangzhou"
138+
bucketName = "your bucket name"
139+
objectName = "your object name"
140+
localFile = "your local file path"
141+
)
142+
143+
// Using the SDK's default configuration
144+
// loading credentials values from the environment variables
145+
cfg := oss.LoadDefaultConfig().
146+
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
147+
WithRegion(region)
148+
149+
client := oss.NewClient(cfg)
150+
151+
file, err := os.Open(localFile)
152+
if err != nil {
153+
log.Fatalf("failed to open file %v", err)
154+
}
155+
defer file.Close()
156+
157+
result, err := client.PutObject(context.TODO(), &oss.PutObjectRequest{
158+
Bucket: oss.Ptr(bucketName),
159+
Key: oss.Ptr(objectName),
160+
Body: file,
161+
})
162+
163+
if err != nil {
164+
log.Fatalf("failed to put object %v", err)
165+
}
166+
167+
fmt.Printf("put object sucessfully, ETag :%v\n", result.ETag)
168+
}
169+
```
170+
171+
## Complete Example
172+
More example projects can be found in the `sample` folder
173+
174+
### Running Example
175+
> - Go to the sample code folder `sample`
176+
> - Configure credentials values from the environment variables, like `export OSS_ACCESS_KEY_ID="your access key id"`, `export OSS_ACCESS_KEY_SECRET="your access key secrect"`
177+
> - Take list_buckets.go as an example,run `go run list_buckets.go -region cn-hangzhou` command。
178+
179+
## License
180+
> - Apache-2.0, see [license file](LICENSE)

go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/aliyun/alibabacloud-oss-go-sdk-v2
2+
3+
go 1.18
4+
5+
require (
6+
github.com/stretchr/testify v1.8.4
7+
golang.org/x/time v0.4.0
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
gopkg.in/yaml.v3 v3.0.1 // indirect
14+
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
6+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7+
golang.org/x/time v0.4.0 h1:Z81tqI5ddIoXDPvVQ7/7CC9TnLM7ubaFG2qXYd5BbYY=
8+
golang.org/x/time v0.4.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
12+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)