archive.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package sample
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  7. )
  8. // ArchiveSample archives sample
  9. func ArchiveSample() {
  10. // Create archive bucket
  11. client, err := oss.New(endpoint, accessID, accessKey)
  12. if err != nil {
  13. HandleError(err)
  14. }
  15. err = client.CreateBucket(bucketName, oss.StorageClass(oss.StorageArchive))
  16. if err != nil {
  17. HandleError(err)
  18. }
  19. archiveBucket, err := client.Bucket(bucketName)
  20. if err != nil {
  21. HandleError(err)
  22. }
  23. // Put archive object
  24. var val = "花间一壶酒,独酌无相亲。 举杯邀明月,对影成三人。"
  25. err = archiveBucket.PutObject(objectKey, strings.NewReader(val))
  26. if err != nil {
  27. HandleError(err)
  28. }
  29. // Check whether the object is archive class
  30. meta, err := archiveBucket.GetObjectDetailedMeta(objectKey)
  31. if err != nil {
  32. HandleError(err)
  33. }
  34. if meta.Get("X-Oss-Storage-Class") == string(oss.StorageArchive) {
  35. // Restore object
  36. err = archiveBucket.RestoreObject(objectKey)
  37. if err != nil {
  38. HandleError(err)
  39. }
  40. // Wait for restore completed
  41. meta, err = archiveBucket.GetObjectDetailedMeta(objectKey)
  42. for meta.Get("X-Oss-Restore") == "ongoing-request=\"true\"" {
  43. fmt.Println("x-oss-restore:" + meta.Get("X-Oss-Restore"))
  44. time.Sleep(1000 * time.Second)
  45. meta, err = archiveBucket.GetObjectDetailedMeta(objectKey)
  46. }
  47. }
  48. // Get restored object
  49. err = archiveBucket.GetObjectToFile(objectKey, localFile)
  50. if err != nil {
  51. HandleError(err)
  52. }
  53. // Restore repeatedly
  54. err = archiveBucket.RestoreObject(objectKey)
  55. // Delete object and bucket
  56. err = DeleteTestBucketAndObject(bucketName)
  57. if err != nil {
  58. HandleError(err)
  59. }
  60. fmt.Println("ArchiveSample completed")
  61. }