github.com/aliyun/aliyun-oss-go-sdk@v3.0.2+incompatible/sample/get_object.go (about)

     1  package sample
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"os"
     9  
    10  	"github.com/aliyun/aliyun-oss-go-sdk/oss"
    11  )
    12  
    13  // GetObjectSample shows the streaming download, range download and resumable download.
    14  func GetObjectSample() {
    15  	// Create bucket
    16  	bucket, err := GetTestBucket(bucketName)
    17  	if err != nil {
    18  		HandleError(err)
    19  	}
    20  
    21  	// Upload the object
    22  	err = bucket.PutObjectFromFile(objectKey, localFile)
    23  	if err != nil {
    24  		HandleError(err)
    25  	}
    26  
    27  	// Case 1: Download the object into ReadCloser(). The body needs to be closed
    28  	body, err := bucket.GetObject(objectKey)
    29  	if err != nil {
    30  		HandleError(err)
    31  	}
    32  
    33  	data, err := ioutil.ReadAll(body)
    34  	body.Close()
    35  	if err != nil {
    36  		HandleError(err)
    37  	}
    38  	fmt.Println("size of data is: ", len(data))
    39  
    40  	// Case 2: Download in the range of object.
    41  	body, err = bucket.GetObject(objectKey, oss.Range(15, 19))
    42  	if err != nil {
    43  		HandleError(err)
    44  	}
    45  	data, err = ioutil.ReadAll(body)
    46  	body.Close()
    47  	fmt.Println("the range of data is: ", string(data))
    48  
    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)
    52  	if err != nil {
    53  		HandleError(err)
    54  	}
    55  	io.Copy(buf, body)
    56  	body.Close()
    57  
    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)
    60  	if err != nil {
    61  		HandleError(err)
    62  	}
    63  	defer fd.Close()
    64  
    65  	body, err = bucket.GetObject(objectKey)
    66  	if err != nil {
    67  		HandleError(err)
    68  	}
    69  	io.Copy(fd, body)
    70  	body.Close()
    71  
    72  	// Case 5: Download the object to local file with file name specified
    73  	err = bucket.GetObjectToFile(objectKey, "mynewfile-2.jpg")
    74  	if err != nil {
    75  		HandleError(err)
    76  	}
    77  
    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))
    81  	if err != nil {
    82  		HandleError(err)
    83  	}
    84  	body.Close()
    85  
    86  	// Last modified time contraint is not met, do not download the file
    87  	_, err = bucket.GetObject(objectKey, oss.IfUnmodifiedSince(pastDate))
    88  	if err == nil {
    89  		HandleError(fmt.Errorf("This result is not the expected result"))
    90  	}
    91  	// body.Close()
    92  
    93  	meta, err := bucket.GetObjectDetailedMeta(objectKey)
    94  	if err != nil {
    95  		HandleError(err)
    96  	}
    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))
   100  	if err != nil {
   101  		HandleError(err)
   102  	}
   103  	body.Close()
   104  
   105  	// Check the content, etag contraint is not met, do not download the file
   106  	_, err = bucket.GetObject(objectKey, oss.IfNoneMatch(etag))
   107  	if err == nil {
   108  		HandleError(fmt.Errorf("This result is not the expected result"))
   109  	}
   110  	// body.Close()
   111  
   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)
   115  	if err != nil {
   116  		HandleError(err)
   117  	}
   118  
   119  	// Part size is 100K and 3 coroutines are used
   120  	err = bucket.DownloadFile(objectKey, "mynewfile-3.jpg", 100*1024, oss.Routines(3))
   121  	if err != nil {
   122  		HandleError(err)
   123  	}
   124  
   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, ""))
   127  	if err != nil {
   128  		HandleError(err)
   129  	}
   130  
   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"))
   134  	if err != nil {
   135  		HandleError(err)
   136  	}
   137  
   138  	// Case 8: Use GZIP encoding for downloading the file, GetObject/GetObjectToFile are the same.
   139  	err = bucket.PutObjectFromFile(objectKey, htmlLocalFile)
   140  	if err != nil {
   141  		HandleError(err)
   142  	}
   143  
   144  	err = bucket.GetObjectToFile(objectKey, "myhtml.gzip", oss.AcceptEncoding("gzip"))
   145  	if err != nil {
   146  		HandleError(err)
   147  	}
   148  
   149  	// Delete the object and bucket
   150  	err = DeleteTestBucketAndObject(bucketName)
   151  	if err != nil {
   152  		HandleError(err)
   153  	}
   154  
   155  	fmt.Println("GetObjectSample completed")
   156  }