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