Skip to content

Commit

Permalink
GetObject now replies with size and md5sum
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana committed May 4, 2015
1 parent 056c97d commit da12c6e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
12 changes: 7 additions & 5 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strings"
)

type api struct {
Expand Down Expand Up @@ -240,7 +241,7 @@ func (a *api) putObjectRequest(bucket, object string, size int64, body io.ReadSe
}
r.Set("Content-MD5", md5Sum)
r.req.ContentLength = size
return req, nil
return r, nil
}

// PutObject - add an object to a bucket
Expand Down Expand Up @@ -288,17 +289,18 @@ func (a *api) getObjectRequest(bucket, object string, offset, length uint64) (*R
//
// Additionally it also takes range arguments to download the specified range bytes of an object.
// For more information about the HTTP Range header, go to http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.
func (a *api) GetObject(bucket, object string, offset, length uint64) (io.ReadCloser, error) {
func (a *api) GetObject(bucket, object string, offset, length uint64) (io.ReadCloser, int64, string, error) {
req, err := a.getObjectRequest(bucket, object, offset, length)
if err != nil {
return nil, err
return nil, 0, "", err
}
resp, err := req.Do()
if err != nil {
return nil, err
return nil, 0, "", err
}
md5sum := strings.Trim(resp.Header.Get("ETag"), "\"") // trim off the odd double quotes
// do not close body here, caller will close
return resp.Body, nil
return resp.Body, resp.ContentLength, md5sum, nil
}

// headObjectRequest wrapper creates a new HeadObject request
Expand Down
66 changes: 66 additions & 0 deletions examples/play/get_object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// +build ignore

/*
* Minimal object storage library (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package main

import (
"io"
"log"
"os"

"github.com/minio-io/objectstorage-go"
)

func main() {
config := new(objectstorage.Config)
config.Endpoint = "http://play.minio.io:9000"
config.AccessKeyID = ""
config.SecretAccessKey = ""
config.UserAgent = "Minio"
m := objectstorage.New(config)

err := m.PutBucket("testbucket")
if err != nil {
log.Println(err)
}

err = m.PutBucketACL("testbucket", "public-read")
if err != nil {
log.Println(err)
}

err = m.HeadBucket("testbucket")
if err != nil {
log.Println(err)
}

reader, size, _, err := m.GetObject("testbucket", "testfile", 0, 0)
if err != nil {
log.Println(err)
}
localfile, err := os.Create("newfile")
if err != nil {
log.Println(err)
}
defer localfile.Close()

_, err = io.CopyN(localfile, reader, size)
if err != nil {
log.Println(err)
}
}
2 changes: 1 addition & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type BucketInterface interface {
type ObjectInterface interface {
/// Object Read/Write/Stat/Unlink Operations
PutObject(bucket, object string, size int64, body io.ReadSeeker) error
GetObject(bucket, object string, offset, length uint64) (io.ReadCloser, error)
GetObject(bucket, object string, offset, length uint64) (io.ReadCloser, int64, string, error)
HeadObject(bucket, object string) error
DeleteObject(bucket, object string) error
}

0 comments on commit da12c6e

Please sign in to comment.