-
Notifications
You must be signed in to change notification settings - Fork 232
/
get_object.go
156 lines (133 loc) · 4.22 KB
/
get_object.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package sample
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
// GetObjectSample shows the streaming download, range download and resumable download.
func GetObjectSample() {
// Create bucket
bucket, err := GetTestBucket(bucketName)
if err != nil {
HandleError(err)
}
// Upload the object
err = bucket.PutObjectFromFile(objectKey, localFile)
if err != nil {
HandleError(err)
}
// Case 1: Download the object into ReadCloser(). The body needs to be closed
body, err := bucket.GetObject(objectKey)
if err != nil {
HandleError(err)
}
data, err := ioutil.ReadAll(body)
body.Close()
if err != nil {
HandleError(err)
}
fmt.Println("size of data is: ", len(data))
// Case 2: Download in the range of object.
body, err = bucket.GetObject(objectKey, oss.Range(15, 19))
if err != nil {
HandleError(err)
}
data, err = ioutil.ReadAll(body)
body.Close()
fmt.Println("the range of data is: ", string(data))
// Case 3: Download the object to byte array. This is for small object.
buf := new(bytes.Buffer)
body, err = bucket.GetObject(objectKey)
if err != nil {
HandleError(err)
}
io.Copy(buf, body)
body.Close()
// Case 4: Download the object to local file. The file handle needs to be specified
fd, err := os.OpenFile("mynewfile-1.jpg", os.O_WRONLY|os.O_CREATE, 0660)
if err != nil {
HandleError(err)
}
defer fd.Close()
body, err = bucket.GetObject(objectKey)
if err != nil {
HandleError(err)
}
io.Copy(fd, body)
body.Close()
// Case 5: Download the object to local file with file name specified
err = bucket.GetObjectToFile(objectKey, "mynewfile-2.jpg")
if err != nil {
HandleError(err)
}
// Case 6: Get the object with contraints. When contraints are met, download the file. Otherwise return precondition error
// last modified time constraint is met, download the file
body, err = bucket.GetObject(objectKey, oss.IfModifiedSince(pastDate))
if err != nil {
HandleError(err)
}
body.Close()
// Last modified time contraint is not met, do not download the file
_, err = bucket.GetObject(objectKey, oss.IfUnmodifiedSince(pastDate))
if err == nil {
HandleError(fmt.Errorf("This result is not the expected result"))
}
// body.Close()
meta, err := bucket.GetObjectDetailedMeta(objectKey)
if err != nil {
HandleError(err)
}
etag := meta.Get(oss.HTTPHeaderEtag)
// Check the content, etag contraint is met, download the file
body, err = bucket.GetObject(objectKey, oss.IfMatch(etag))
if err != nil {
HandleError(err)
}
body.Close()
// Check the content, etag contraint is not met, do not download the file
_, err = bucket.GetObject(objectKey, oss.IfNoneMatch(etag))
if err == nil {
HandleError(fmt.Errorf("This result is not the expected result"))
}
// body.Close()
// Case 7: Big file's multipart download, concurrent and resumable download is supported.
// multipart download with part size 100KB. By default single coroutine is used and no checkpoint
err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024)
if err != nil {
HandleError(err)
}
// Part size is 100K and 3 coroutines are used
err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Routines(3))
if err != nil {
HandleError(err)
}
// Part size is 100K and 3 coroutines with checkpoint
err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
if err != nil {
HandleError(err)
}
// Specify the checkpoint file path to record which parts have been downloaded.
// This file path can be specified by the 2nd parameter of Checkpoint, it will be the download directory if the file path is empty.
err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Checkpoint(true, "mynewfile.cp"))
if err != nil {
HandleError(err)
}
// Case 8: Use GZIP encoding for downloading the file, GetObject/GetObjectToFile are the same.
err = bucket.PutObjectFromFile(objectKey, htmlLocalFile)
if err != nil {
HandleError(err)
}
err = bucket.GetObjectToFile(objectKey, "myhtml.gzip", oss.AcceptEncoding("gzip"))
if err != nil {
HandleError(err)
}
// Delete the object and bucket
err = DeleteTestBucketAndObject(bucketName)
if err != nil {
HandleError(err)
}
fmt.Println("GetObjectSample completed")
}