Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PutObjectWithFile for PutObject #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions oss/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,37 @@ 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) {
if strings.HasPrefix(opath, "/") == false {
opath = "/" + opath
}

buffer := new(bytes.Buffer)
io.Copy(buffer, file)

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 {
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 {
Expand Down