Skip to content

Commit

Permalink
Merge pull request #6 from ckiosidis/avoid-double-write-to-gcs
Browse files Browse the repository at this point in the history
combine write and update to one request
  • Loading branch information
EngHabu authored Jul 15, 2022
2 parents 9c3f5f9 + 6237dc1 commit e9c5e6f
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions google/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,25 @@ func (c *Container) Put(name string, r io.Reader, size int64, metadata map[strin
}

w := obj.NewWriter(c.ctx)
w.ObjectAttrs.Metadata = merge(w.ObjectAttrs.Metadata, mdPrepped)
if _, err := io.Copy(w, r); err != nil {
return nil, err
}
w.Close()

attr, err := obj.Update(c.ctx, storage.ObjectAttrsToUpdate{Metadata: mdPrepped})
if err != nil {
if err = w.Close(); err != nil {
return nil, err
}

return c.convertToStowItem(attr)
return c.convertToStowItem(w.Attrs())
}

func merge(metadata ...map[string]string) map[string]string {
res := map[string]string{}
for _, mt := range metadata {
for k, v := range mt {
res[k] = v
}
}
return res
}

func (c *Container) convertToStowItem(attr *storage.ObjectAttrs) (stow.Item, error) {
Expand Down

0 comments on commit e9c5e6f

Please sign in to comment.