bucket_encryption.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package sample
  2. import (
  3. "fmt"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. )
  6. // BucketEncryptionSample shows how to get and set the bucket encryption Algorithm
  7. func BucketEncryptionSample() {
  8. // New client
  9. client, err := oss.New(endpoint, accessID, accessKey)
  10. if err != nil {
  11. HandleError(err)
  12. }
  13. // Create a bucket with default parameters
  14. err = client.CreateBucket(bucketName)
  15. if err != nil {
  16. HandleError(err)
  17. }
  18. // SetBucketEncryption:AES256 ,"123"
  19. encryptionRule := oss.ServerEncryptionRule{}
  20. encryptionRule.SSEDefault.SSEAlgorithm = string(oss.AESAlgorithm)
  21. err = client.SetBucketEncryption(bucketName, encryptionRule)
  22. if err != nil {
  23. HandleError(err)
  24. }
  25. // Get bucket encryption
  26. encryptionResult, err := client.GetBucketEncryption(bucketName)
  27. if err != nil {
  28. HandleError(err)
  29. }
  30. fmt.Println("Bucket Encryption:", encryptionResult)
  31. // Delete the bucket
  32. err = client.DeleteBucketEncryption(bucketName)
  33. if err != nil {
  34. HandleError(err)
  35. }
  36. // Delete the object and bucket
  37. err = DeleteTestBucketAndObject(bucketName)
  38. if err != nil {
  39. HandleError(err)
  40. }
  41. fmt.Println("BucketEncryptionSample completed")
  42. }