GET Object
API Description
You can use this API to download an object in a specified bucket.
Method Definition
func (obsClient ObsClient) GetObject(input *GetObjectInput) (output *GetObjectOutput, err error)
Method Definition If a Signed URL Is Used
func (obsClient ObsClient) GetObjectWithSignedUrl(signedUrl string, actualSignedRequestHeaders http.Header) (output *GetObjectOutput, err error)
Request Parameter
|
Field |
Type |
Optional or Mandatory |
|---|---|---|
|
input |
Mandatory |
Returned Result
|
Field |
Type |
|---|---|
|
output |
|
|
err |
error |
Sample Code
- Downloading an object:
func main() { input := &obs.GetObjectInput{} input.Bucket = "bucketname" input.Key = "objectkey" output, err := obsClient.GetObject(input) if err == nil { defer output.Body.Close() fmt.Printf("RequestId:%s\n", output.RequestId) fmt.Printf("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n", output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified) p := make([]byte, 1024) var readErr error var readCount int for { readCount, readErr = output.Body.Read(p) if readCount > 0 { fmt.Printf("%s", p[:readCount]) } if readErr != nil { break } } } else { if obsError, ok := err.(obs.ObsError); ok { fmt.Println(obsError.Code) fmt.Println(obsError.Message) } else { fmt.Println(err) } } } - Download an object and save it to the local PC:
func main() { input := &obs.GetObjectInput{} input.Bucket = "bucketname" input.Key = "objectkey" output, err := obsClient.GetObject(input) if err == nil { defer output.Body.Close() fmt.Printf("RequestId:%s\n", output.RequestId) fmt.Printf("StorageClass:%s, ETag:%s, ContentType:%s, ContentLength:%d, LastModified:%s\n", output.StorageClass, output.ETag, output.ContentType, output.ContentLength, output.LastModified) p, err := ioutil.ReadAll(output.Body) if err == nil { ioutil.WriteFile("***your file path***", p, os.ModeAppend); } } else { if obsError, ok := err.(obs.ObsError); ok { fmt.Println(obsError.Code) fmt.Println(obsError.Message) } else { fmt.Println(err) } } }
Last Article: PUT File
Next Article: PUT Object - Copy
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.