Skip to content
This repository has been archived by the owner on Apr 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #7 from nelsonken/revert-4-master
Browse files Browse the repository at this point in the history
Revert "解决HeadObject没有返回header问题"
  • Loading branch information
nelsonken authored Mar 31, 2018
2 parents ed6aa5f + c8b50a3 commit a04ca2e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
Binary file removed .DS_Store
Binary file not shown.
20 changes: 9 additions & 11 deletions cos/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/xml"
"fmt"
"io"
"net/http"
"os"
"strings"
)
Expand All @@ -31,13 +30,18 @@ type ObjectSlice struct {
}

// 获得云存储上文件信息
func (b *Bucket) HeadObject(ctx context.Context, object string) (http.Header, error) {
func (b *Bucket) HeadObject(ctx context.Context, object string) error {
resq, err := b.conn.Do(ctx, "HEAD", b.Name, object, nil, nil, nil)
if err == nil {
defer resq.Body.Close()
} else {
for k, v := range resq.Header {
value := fmt.Sprintf("%s", v)
fmt.Printf("%-18s: %s\n", k, strings.Replace(strings.Replace(value, "[", "", -1), "]", "", -1))
}
}

return resq.Header, err
return err
}

// UploadObject 上传文件
Expand Down Expand Up @@ -76,19 +80,13 @@ func (b *Bucket) DeleteObject(ctx context.Context, obj string) error {
}

// DownloadObject 下载对象
func (b *Bucket) DownloadObject(ctx context.Context, object string, filePath string) error {
func (b *Bucket) DownloadObject(ctx context.Context, object string, w io.Writer) error {
res, err := b.conn.Do(ctx, "GET", b.Name, object, nil, nil, nil)
if err != nil {
return err
}

fd, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.FileMode(0664))
if err != nil {
return err
}
defer fd.Close()

_, err = io.Copy(fd, res.Body)
_, err = io.Copy(w, res.Body)

return err
}
Expand Down

0 comments on commit a04ca2e

Please sign in to comment.