-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobject.go
93 lines (79 loc) · 1.85 KB
/
object.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package bfs
import (
"context"
"fmt"
"net/url"
"path"
"strings"
)
// Object is a handle for a single file/object on a Bucket.
type Object struct {
name string
bucket Bucket
reclaimed bool
}
// NewObject inits a new object from an URL string
func NewObject(ctx context.Context, fullURL string) (*Object, error) {
// parse URL
u, err := url.Parse(fullURL)
if err != nil {
return nil, err
}
// store full path name and unset
name := strings.TrimPrefix(path.Clean(u.Path), "/")
if name == "." {
return nil, fmt.Errorf("bfs: invalid URL path %q", u.Path)
}
u.Path = "/"
// resolve bucket
bucket, err := Resolve(ctx, u)
if err != nil {
return nil, err
}
return &Object{
name: name,
bucket: bucket,
}, nil
}
// NewObjectFromBucket inits a new object from an existing bucket.
func NewObjectFromBucket(bucket Bucket, name string) *Object {
return &Object{
name: name,
bucket: bucket,
reclaimed: true,
}
}
// NewInMemObject returns a new in-memory object.
func NewInMemObject(name string) *Object {
return &Object{
name: name,
bucket: NewInMem(),
}
}
// Name returns an object's name.
func (o *Object) Name() string {
return o.name
}
// Head returns an object's meta info.
func (o *Object) Head(ctx context.Context) (*MetaInfo, error) {
return o.bucket.Head(ctx, o.name)
}
// Open opens an object for reading.
func (o *Object) Open(ctx context.Context) (Reader, error) {
return o.bucket.Open(ctx, o.name)
}
// Create creates/opens a object for writing.
func (o *Object) Create(ctx context.Context, opts *WriteOptions) (Writer, error) {
return o.bucket.Create(ctx, o.name, opts)
}
// Remove removes a object.
func (o *Object) Remove(ctx context.Context) error {
return o.bucket.Remove(ctx, o.name)
}
// Close closes the object.
func (o *Object) Close() error {
if o.reclaimed {
return nil
}
return o.bucket.Close()
}