This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathstorageObject.go
67 lines (57 loc) · 1.65 KB
/
storageObject.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package gcp
import (
"context"
"io/ioutil"
"cloud.google.com/go/storage"
"github.com/puppetlabs/wash/activity"
"github.com/puppetlabs/wash/plugin"
)
type storageObject struct {
plugin.EntryBase
*storage.ObjectHandle
}
func newStorageObject(name string, object *storage.ObjectHandle, attrs *storage.ObjectAttrs) *storageObject {
obj := &storageObject{EntryBase: plugin.NewEntry(name), ObjectHandle: object}
obj.SetPartialMetadata(attrs).
Attributes().
SetCrtime(attrs.Created).
SetCtime(attrs.Updated).
SetMtime(attrs.Updated).
SetSize(uint64(attrs.Size))
return obj
}
func (s *storageObject) Schema() *plugin.EntrySchema {
return plugin.
NewEntrySchema(s, "object").
SetDescription(storageObjectDescription).
SetPartialMetadataSchema(storage.ObjectAttrs{})
}
func (s *storageObject) Read(ctx context.Context, size int64, offset int64) ([]byte, error) {
rdr, err := s.NewRangeReader(context.Background(), offset, int64(size))
if err != nil {
return nil, err
}
defer func() {
if err := rdr.Close(); err != nil {
activity.Record(ctx, "Error closing GCP storage object range reader: %v", err)
}
}()
return ioutil.ReadAll(rdr)
}
func (s *storageObject) Write(ctx context.Context, p []byte) error {
wr := s.ObjectHandle.NewWriter(ctx)
if _, err := wr.Write(p); err != nil {
wr.Close()
return err
}
// When Close fails we can assume the object update failed.
return wr.Close()
}
func (s *storageObject) Delete(ctx context.Context) (bool, error) {
err := s.ObjectHandle.Delete(ctx)
return true, err
}
const storageObjectDescription = `
This is a Storage object. See the bucket's docs for more details
on why we have this kind of entry.
`