new_bucket.go 907 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package sample
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. // NewBucketSample 展示了如何初始化Client、Bucket
  8. func NewBucketSample() {
  9. // New Client
  10. client, err := oss.New(endpoint, accessID, accessKey)
  11. if err != nil {
  12. HandleError(err)
  13. }
  14. // Create Bucket
  15. err = client.CreateBucket(bucketName)
  16. if err != nil {
  17. HandleError(err)
  18. }
  19. // New Bucket
  20. bucket, err := client.Bucket(bucketName)
  21. if err != nil {
  22. HandleError(err)
  23. }
  24. // Put Object,上传一个Object
  25. var objectName = "myobject"
  26. err = bucket.PutObject(objectName, strings.NewReader("MyObjectValue"))
  27. if err != nil {
  28. HandleError(err)
  29. }
  30. // Delete Object,删除Object
  31. err = bucket.DeleteObject(objectName)
  32. if err != nil {
  33. HandleError(err)
  34. }
  35. // 删除bucket
  36. err = client.DeleteBucket(bucketName)
  37. if err != nil {
  38. HandleError(err)
  39. }
  40. fmt.Println("NewBucketSample completed")
  41. }