diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2018-02-19 19:19:53 +0100 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2018-02-19 19:19:53 +0100 |
| commit | 46081e2068aeea62783863515ab116bb6af20661 (patch) | |
| tree | d4dcd5e849909b08597209781dba6fba526ad2ae /object.go | |
| parent | d1dec3782fb5f9aa5775dafb0ea1225af6279ed2 (diff) | |
| download | go-schwift-46081e2068aeea62783863515ab116bb6af20661.tar.gz | |
test coverage for object upload, download, metadata update
Diffstat (limited to 'object.go')
| -rw-r--r-- | object.go | 39 |
1 files changed, 37 insertions, 2 deletions
@@ -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. // |
