edit-icon download-icon

Quick start

Last Updated: Nov 06, 2017

This document describes how to use OSS Go SDK to access OSS to view the bucket list and object list, and upload, download, and delete the objects. You must run the import github.com/aliyun/aliyun-oss-go-sdk/oss to introduce the OSS package to use the OSS Go SDK.

Initialize the client

You can initialize, or create a client as follows:

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. func handleError(err error) {
  8. fmt.Println("Error:", err)
  9. os.Exit(-1)
  10. }
  11. func main() {
  12. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  13. if err != nil {
  14. handleError(err)
  15. }
  16. lsRes, err := client.ListBuckets()
  17. if err != nil {
  18. handleError(err)
  19. }
  20. for _, bucket := range lsRes.Buckets {
  21. fmt.Println("bucket:", bucket.Name)
  22. }
  23. }

Note:

  • The endpoint is a domain for accessing OSS. For example, the endpoint for accessing OSS in the Hangzhou region is http://oss-cn-hangzhou.aliyuncs.com. For more information, see Endpoints.

  • The AccessKeyId and AccessKeySecret constitute an AccessKey to access the OSS. For more information, see Access Control.

  • We recommend you to replace the settings of Endpoint, AccessKeyID, and AccessKeySecret based on your actual values when you run the sample project.

View the bucket list

You can use Client.ListBuckets to view the bucket list:

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. func handleError(err error) {
  8. fmt.Println("Error:", err)
  9. os.Exit(-1)
  10. }
  11. func main() {
  12. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  13. if err != nil {
  14. handleError(err)
  15. }
  16. lsRes, err := client.ListBuckets()
  17. if err != nil {
  18. handleError(err)
  19. }
  20. for _, bucket := range lsRes.Buckets {
  21. fmt.Println("bucket:", bucket.Name)
  22. }
  23. }

Note:

  • For bucket naming conventions, see Basic OSS Concepts.
  • TThe bucket name has to be globally unique. Make sure, your bucket name is different from the other users’.

Get a bucket

Operations on a bucket are implemented through client methods, for example, creating/deleting a bucket, setting/clearing the bucket ACL, and setting the bucket lifecycle/anti-leech. Operations on an object are implemented through bucket methods, for example, uploading/downloading/deleting an object and setting the object ACL policies. You can use Client.Bucket to get the operation handle of a specified bucket.

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. func handleError(err error) {
  8. fmt.Println("Error:", err)
  9. os.Exit(-1)
  10. }
  11. func main() {
  12. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  13. if err != nil {
  14. handleError(err)
  15. }
  16. bucket, err := client.Bucket("my-bucket")
  17. if err != nil {
  18. handleError(err)
  19. }
  20. _, err = bucket.ListObjects()
  21. if err != nil {
  22. handleError(err)
  23. }
  24. }

View the object list

You can use Bucket.ListObjects to view the object list in a bucket:

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. func handleError(err error) {
  8. fmt.Println("Error:", err)
  9. os.Exit(-1)
  10. }
  11. func main() {
  12. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  13. if err != nil {
  14. handleError(err)
  15. }
  16. bucket, err := client.Bucket("my-bucket")
  17. if err != nil {
  18. handleError(err)
  19. }
  20. lsRes, err := bucket.ListObjects()
  21. if err != nil {
  22. handleError(err)
  23. }
  24. for _, object := range lsRes.Objects {
  25. fmt.Println("Object:", object.Key)
  26. }
  27. }

Upload an object

You can use Bucket.PutObjectFromFile to upload a file:

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. func handleError(err error) {
  8. fmt.Println("Error:", err)
  9. os.Exit(-1)
  10. }
  11. func main() {
  12. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  13. if err != nil {
  14. handleError(err)
  15. }
  16. bucket, err := client.Bucket("my-bucket")
  17. if err != nil {
  18. handleError(err)
  19. }
  20. err = bucket.PutObjectFromFile("my-object", "LocalFile")
  21. if err != nil {
  22. handleError(err)
  23. }
  24. }

The LocalFile is the path of the local file to be uploaded. When the file is uploaded, you can view it using Bucket.ListObjects.

Download an object

You can use Bucket.GetObject to download an object:

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. func handleError(err error) {
  8. fmt.Println("Error:", err)
  9. os.Exit(-1)
  10. }
  11. func main() {
  12. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  13. if err != nil {
  14. handleError(err)
  15. }
  16. bucket, err := client.Bucket("my-bucket")
  17. if err != nil {
  18. handleError(err)
  19. }
  20. err = bucket.GetObjectToFile("my-object", "LocalFile")
  21. if err != nil {
  22. handleError(err)
  23. }
  24. }

The LocalFile is the path to save the object. After the object is downloaded, you can open the object to view its content.

Delete an object

You can use Bucket.DeleteObject to delete an object from a bucket:

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. func handleError(err error) {
  8. fmt.Println("Error:", err)
  9. os.Exit(-1)
  10. }
  11. func main() {
  12. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  13. if err != nil {
  14. handleError(err)
  15. }
  16. bucket, err := client.Bucket("my-bucket")
  17. if err != nil {
  18. handleError(err)
  19. }
  20. err = bucket.DeleteObject("my-object")
  21. if err != nil {
  22. handleError(err)
  23. }
  24. }

After the object is deleted, you can use Bucket.ListObjects to verify the object deletion.

Learn more

Thank you! We've received your feedback.