10 "github.com/aliyun/aliyun-oss-go-sdk/oss"
13 // GetObjectSample shows the streaming download, range download and resumable download.
14 func GetObjectSample() {
16 bucket, err := GetTestBucket(bucketName)
22 err = bucket.PutObjectFromFile(objectKey, localFile)
27 // Case 1: Download the object into ReadCloser(). The body needs to be closed
28 body, err := bucket.GetObject(objectKey)
33 data, err := ioutil.ReadAll(body)
38 fmt.Println("size of data is: ", len(data))
40 // Case 2: Download in the range of object.
41 body, err = bucket.GetObject(objectKey, oss.Range(15, 19))
45 data, err = ioutil.ReadAll(body)
47 fmt.Println("the range of data is: ", string(data))
49 // Case 3: Download the object to byte array. This is for small object.
50 buf := new(bytes.Buffer)
51 body, err = bucket.GetObject(objectKey)
58 // Case 4: Download the object to local file. The file handle needs to be specified
59 fd, err := os.OpenFile("mynewfile-1.jpg", os.O_WRONLY|os.O_CREATE, 0660)
65 body, err = bucket.GetObject(objectKey)
72 // Case 5: Download the object to local file with file name specified
73 err = bucket.GetObjectToFile(objectKey, "mynewfile-2.jpg")
78 // Case 6: Get the object with contraints. When contraints are met, download the file. Otherwise return precondition error
79 // last modified time constraint is met, download the file
80 body, err = bucket.GetObject(objectKey, oss.IfModifiedSince(pastDate))
86 // Last modified time contraint is not met, do not download the file
87 _, err = bucket.GetObject(objectKey, oss.IfUnmodifiedSince(pastDate))
89 HandleError(fmt.Errorf("This result is not the expected result"))
93 meta, err := bucket.GetObjectDetailedMeta(objectKey)
97 etag := meta.Get(oss.HTTPHeaderEtag)
98 // Check the content, etag contraint is met, download the file
99 body, err = bucket.GetObject(objectKey, oss.IfMatch(etag))
105 // Check the content, etag contraint is not met, do not download the file
106 _, err = bucket.GetObject(objectKey, oss.IfNoneMatch(etag))
108 HandleError(fmt.Errorf("This result is not the expected result"))
112 // Case 7: Big file's multipart download, concurrent and resumable download is supported.
113 // multipart download with part size 100KB. By default single coroutine is used and no checkpoint
114 err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024)
119 // Part size is 100K and 3 coroutines are used
120 err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Routines(3))
125 // Part size is 100K and 3 coroutines with checkpoint
126 err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
131 // Specify the checkpoint file path to record which parts have been downloaded.
132 // This file path can be specified by the 2nd parameter of Checkpoint, it will be the download directory if the file path is empty.
133 err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Checkpoint(true, "mynewfile.cp"))
138 // Case 8: Use GZIP encoding for downloading the file, GetObject/GetObjectToFile are the same.
139 err = bucket.PutObjectFromFile(objectKey, htmlLocalFile)
144 err = bucket.GetObjectToFile(objectKey, "myhtml.gzip", oss.AcceptEncoding("gzip"))
149 // Delete the object and bucket
150 err = DeleteTestBucketAndObject(bucketName)
155 fmt.Println("GetObjectSample completed")