From 46081e2068aeea62783863515ab116bb6af20661 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Mon, 19 Feb 2018 19:19:53 +0100 Subject: test coverage for object upload, download, metadata update --- object.go | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'object.go') diff --git a/object.go b/object.go index 83ada1d..9da08c5 100644 --- a/object.go +++ b/object.go @@ -115,7 +115,7 @@ func (o *Object) Headers() (ObjectHeaders, error) { //Update updates the object's headers using a POST request. To add URL //parameters, pass a non-nil *RequestOptions. // -//If you are not sure whether the container exists, use Create() instead. +//This operation returns http.StatusNotFound if the object does not exist. // //A successful POST request implies Invalidate() since it may change metadata. func (o *Object) Update(headers ObjectHeaders, opts *RequestOptions) error { @@ -125,7 +125,7 @@ func (o *Object) Update(headers ObjectHeaders, opts *RequestOptions) error { ObjectName: o.name, Headers: headersToHTTP(headers), Options: opts, - ExpectStatusCodes: []int{204}, + ExpectStatusCodes: []int{202}, }.Do(o.c.a.client) if err == nil { o.Invalidate() @@ -146,6 +146,10 @@ func (o *Object) Update(headers ObjectHeaders, opts *RequestOptions) error { // var buffer string // o.Upload(bytes.NewReader([]byte(buffer)), headers, opts) // +//If you have neither an io.Reader nor a []byte or string, but you have a +//function that generates the object's content into an io.Writer, use +//UploadWithWriter instead. +// //If content is a *bytes.Reader or a *bytes.Buffer instance, the Content-Length //and Etag request headers will be computed automatically. Otherwise, it is //highly recommended that the caller set these headers (if possible) to allow @@ -235,6 +239,37 @@ func tryComputeEtag(content io.Reader, headers ObjectHeaders) { } } +//UploadWithWriter is a variant of Upload that can be used when the object's +//content is generated by some function or package that takes an io.Writer +//instead of supplying an io.Reader. For example: +// +// func greeting(target io.Writer, name string) error { +// _, err := fmt.Fprintf(target, "Hello %s!\n", name) +// return err +// } +// +// obj := container.Object("greeting-for-susan-and-jeffrey") +// err := obj.UploadWithWriter(nil, nil, func(w io.Writer) error { +// err := greeting(w, "Susan") +// if err == nil { +// err = greeting(w, "Jeffrey") +// } +// return err +// }) +// +//If you do not need an io.Writer, always use Upload instead. +func (o *Object) UploadWithWriter(headers ObjectHeaders, opts *RequestOptions, callback func(io.Writer) error) error { + reader, writer := io.Pipe() + errChan := make(chan error) + go func() { + err := o.Upload(reader, headers, opts) + reader.CloseWithError(err) //stop the writer if it is still writing + errChan <- err + }() + writer.CloseWithError(callback(writer)) //stop the reader if it is still reading + return <-errChan +} + //Delete deletes the object using a DELETE request. To add URL parameters, //pass a non-nil *RequestOptions. // -- cgit v1.2.3