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 展示了如何创建存储空间
  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. // 场景1:使用默认参数创建bucket
  15. err = client.CreateBucket(bucketName)
  16. if err != nil {
  17. HandleError(err)
  18. }
  19. // 删除bucket
  20. err = client.DeleteBucket(bucketName)
  21. if err != nil {
  22. HandleError(err)
  23. }
  24. // 场景2:创建bucket时指定其权限
  25. err = client.CreateBucket(bucketName, oss.ACL(oss.ACLPublicRead))
  26. if err != nil {
  27. HandleError(err)
  28. }
  29. // 场景3:重复创建OSS不会报错,但是不做任何操作,指定的ACL无效
  30. err = client.CreateBucket(bucketName, oss.ACL(oss.ACLPublicReadWrite))
  31. if err != nil {
  32. HandleError(err)
  33. }
  34. // 删除bucket
  35. err = client.DeleteBucket(bucketName)
  36. if err != nil {
  37. HandleError(err)
  38. }
  39. fmt.Println("CreateBucketSample completed")
  40. }