This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
fs.go
116 lines (101 loc) · 4.04 KB
/
fs.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Package storage provides types and functionality for abstracting storage systems
// (local, in memory, Google Cloud storage) into a common interface.
package storage
import (
"context"
"io"
"time"
)
// File contains the metadata required to define a file (for reading).
type File struct {
io.ReadCloser // Underlying data.
Attributes
}
// Attributes represents the metadata of a File
// Inspired from gocloud.dev/blob.Attributes
type Attributes struct {
// ContentType is the MIME type of the blob object. It will not be empty.
ContentType string
// ContentEncoding specifies the encoding used for the blob's content, if any.
ContentEncoding string
// Metadata holds key/value pairs associated with the blob.
// Keys are guaranteed to be in lowercase, even if the backend provider
// has case-sensitive keys (although note that Metadata written via
// this package will always be lowercased). If there are duplicate
// case-insensitive keys (e.g., "foo" and "FOO"), only one value
// will be kept, and it is undefined which one.
Metadata map[string]string
// ModTime is the time the blob object was last modified.
ModTime time.Time
// CreationTime is the time the blob object was created.
CreationTime time.Time
// Size is the size of the object in bytes.
Size int64
}
// ReaderOptions are used to modify the behaviour of read operations.
// Inspired from gocloud.dev/blob.ReaderOptions
// It is provided for future extensibility.
type ReaderOptions struct {
// ReadCompressed controls whether the file must be uncompressed based on Content-Encoding.
// Only respected by Google Cloud Storage: https://cloud.google.com/storage/docs/transcoding
// Common pitfall: https://github.com/googleapis/google-cloud-go/issues/1743
ReadCompressed bool
}
// WriterOptions are used to modify the behaviour of write operations.
// Inspired from gocloud.dev/blob.WriterOptions
// Not all options are supported by all FS
type WriterOptions struct {
Attributes Attributes
// BufferSize changes the default size in bytes of the chunks that
// Writer will upload in a single request; larger blobs will be split into
// multiple requests.
//
// This option may be ignored by some drivers.
//
// If 0, the driver will choose a reasonable default.
//
// If the Writer is used to do many small writes concurrently, using a
// smaller BufferSize may reduce memory usage.
BufferSize int
}
// DefaultSignedURLExpiry is the default duration for SignedURLOptions.Expiry.
const (
DefaultSignedURLExpiry = 1 * time.Hour
DefaultSignedURLMethod = "GET"
)
// SignedURLOptions are used to modify the behaviour of write operations.
// Inspired from gocloud.dev/blob.SignedURLOptions
// Not all options are supported by all FS
type SignedURLOptions struct {
// Expiry sets how long the returned URL is valid for.
// Defaults to DefaultSignedURLExpiry.
Expiry time.Duration
// Method is the HTTP method that can be used on the URL; one of "GET", "PUT",
// or "DELETE". Defaults to "GET".
Method string
}
func (o *SignedURLOptions) applyDefaults() {
if o.Expiry == 0 {
o.Expiry = DefaultSignedURLExpiry
}
if o.Method == "" {
o.Method = DefaultSignedURLMethod
}
}
// FS is an interface which defines a virtual filesystem.
type FS interface {
Walker
// Open opens an existing file at path in the filesystem. Callers must close the
// File when done to release all underlying resources.
Open(ctx context.Context, path string, options *ReaderOptions) (*File, error)
// Attributes returns attributes about a path
Attributes(ctx context.Context, path string, options *ReaderOptions) (*Attributes, error)
// Create makes a new file at path in the filesystem. Callers must close the
// returned WriteCloser and check the error to be sure that the file
// was successfully written.
Create(ctx context.Context, path string, options *WriterOptions) (io.WriteCloser, error)
// Delete removes a path from the filesystem.
Delete(ctx context.Context, path string) error
// URL resolves a path to an addressable URL
URL(ctx context.Context, path string, options *SignedURLOptions) (string, error)
}