create_bucket.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package sample
  2. import (
  3. "fmt"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. )
  6. // CreateBucketSample shows how to create bucket
  7. func CreateBucketSample() {
  8. // New client
  9. client, err := oss.New(endpoint, accessID, accessKey)
  10. if err != nil {
  11. HandleError(err)
  12. }
  13. DeleteTestBucketAndObject(bucketName)
  14. // Case 1: Create a bucket with default parameters
  15. err = client.CreateBucket(bucketName)
  16. if err != nil {
  17. HandleError(err)
  18. }
  19. // Delete bucket
  20. err = client.DeleteBucket(bucketName)
  21. if err != nil {
  22. HandleError(err)
  23. }
  24. // Case 2: Create the bucket with ACL
  25. err = client.CreateBucket(bucketName, oss.ACL(oss.ACLPublicRead))
  26. if err != nil {
  27. HandleError(err)
  28. }
  29. // Case 3: Repeat the same bucket. OSS will not return error, but just no op. The ACL is not updated.
  30. err = client.CreateBucket(bucketName, oss.ACL(oss.ACLPublicReadWrite))
  31. if err != nil {
  32. HandleError(err)
  33. }
  34. // Delete bucket
  35. err = client.DeleteBucket(bucketName)
  36. if err != nil {
  37. HandleError(err)
  38. }
  39. fmt.Println("CreateBucketSample completed")
  40. }