From e6f11e07fded754f5457fb2166972a876f5bed97 Mon Sep 17 00:00:00 2001 From: damonchen Date: Sat, 1 Aug 2015 22:39:33 +0800 Subject: [PATCH 1/4] add PutObjectWithFile for PutObject --- oss/oss.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/oss/oss.go b/oss/oss.go index c8a83b6..e7af929 100644 --- a/oss/oss.go +++ b/oss/oss.go @@ -42,6 +42,7 @@ import ( "hash" "io" "io/ioutil" + "mime/multipart" "net/http" "os" "sort" @@ -508,6 +509,35 @@ func (c *Client) PutObject(opath string, filepath string) (err error) { return } +func (c *Client) PutObjectWithFile(opath string, file multipart.File) (err error) { + if strings.HasPrefix(opath, "/") == false { + opath = "/" + opath + } + + buffer := new(bytes.Buffer) + io.Copy(buffer, file) + + contentType := http.DetectContentType(buffer.Bytes()) + params := map[string]string{} + params["Content-Type"] = contentType + + resp, err := c.doRequest("PUT", opath, opath, params, buffer) + if err != nil { + return + } + + body, _ := ioutil.ReadAll(resp.Body) + defer resp.Body.Close() + + if resp.StatusCode != 200 { + err = errors.New(resp.Status) + fmt.Println(string(body)) + return + } + return + +} + //Get object's meta information by its path. The format of remote path is "/bucketName/objectName". func (c *Client) HeadObject(opath string) (header http.Header, err error) { if strings.HasPrefix(opath, "/") == false { From 467ffd0b073ed3a73b0c7071ddeccaf2ced09416 Mon Sep 17 00:00:00 2001 From: damonchen Date: Thu, 5 Nov 2015 18:32:03 +0800 Subject: [PATCH 2/4] change file to io.Reader --- oss/oss.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/oss/oss.go b/oss/oss.go index e7af929..fe1b2ab 100644 --- a/oss/oss.go +++ b/oss/oss.go @@ -42,7 +42,6 @@ import ( "hash" "io" "io/ioutil" - "mime/multipart" "net/http" "os" "sort" @@ -509,7 +508,7 @@ func (c *Client) PutObject(opath string, filepath string) (err error) { return } -func (c *Client) PutObjectWithFile(opath string, file multipart.File) (err error) { +func (c *Client) PutObjectWithFile(opath string, file io.Reader) (err error) { if strings.HasPrefix(opath, "/") == false { opath = "/" + opath } From 1ba14de17f2c4be5c28dd062a8891e566f52ab89 Mon Sep 17 00:00:00 2001 From: damonchen Date: Mon, 14 Dec 2015 11:31:35 +0800 Subject: [PATCH 3/4] extend put object with file interface with params --- oss/oss.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/oss/oss.go b/oss/oss.go index fe1b2ab..db8f01b 100644 --- a/oss/oss.go +++ b/oss/oss.go @@ -508,7 +508,7 @@ func (c *Client) PutObject(opath string, filepath string) (err error) { return } -func (c *Client) PutObjectWithFile(opath string, file io.Reader) (err error) { +func (c *Client) PutObjectWithFile(opath string, file io.Reader, params map[string]interface{}) (err error) { if strings.HasPrefix(opath, "/") == false { opath = "/" + opath } @@ -516,9 +516,11 @@ func (c *Client) PutObjectWithFile(opath string, file io.Reader) (err error) { buffer := new(bytes.Buffer) io.Copy(buffer, file) - contentType := http.DetectContentType(buffer.Bytes()) - params := map[string]string{} - params["Content-Type"] = contentType + if params == nil { + params = map[string]string{} + contentType := http.DetectContentType(buffer.Bytes()) + params["Content-Type"] = contentType + } resp, err := c.doRequest("PUT", opath, opath, params, buffer) if err != nil { From 57644ae612fe11e435cbdc63b1c0ed266aab46b8 Mon Sep 17 00:00:00 2001 From: damonchen Date: Mon, 14 Dec 2015 15:13:15 +0800 Subject: [PATCH 4/4] fix error type of params --- oss/oss.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oss/oss.go b/oss/oss.go index db8f01b..a33e194 100644 --- a/oss/oss.go +++ b/oss/oss.go @@ -508,7 +508,7 @@ func (c *Client) PutObject(opath string, filepath string) (err error) { return } -func (c *Client) PutObjectWithFile(opath string, file io.Reader, params map[string]interface{}) (err error) { +func (c *Client) PutObjectWithFile(opath string, file io.Reader, params map[string]string) (err error) { if strings.HasPrefix(opath, "/") == false { opath = "/" + opath }