|
| 1 | +# Alibaba Cloud OSS SDK for Go v2 |
| 2 | + |
| 3 | +[](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) |
0 commit comments