This repository was archived by the owner on Feb 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathstorage.go
95 lines (73 loc) · 2.49 KB
/
storage.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
package storage
import (
"context"
"fmt"
"io"
"time"
"github.com/go-kit/log"
"github.com/meltwater/drone-cache/storage/backend"
)
const DefaultOperationTimeout = 3 * time.Minute
// Storage is a place that files can be written to and read from.
type Storage interface {
// Get writes contents of the given object with given key from remote storage to io.Writer.
Get(p string, w io.Writer) error
// Put writes contents of io.Reader to remote storage at given key location.
Put(p string, r io.Reader) error
// Exists checks if object with given key exists in remote storage.
Exists(p string) (bool, error)
// List lists contents of the given directory by given key from remote storage.
List(p string) ([]backend.FileEntry, error)
// Delete deletes the object from remote storage.
Delete(p string) error
}
// Default Storage implementation.
type storage struct {
logger log.Logger
b backend.Backend
timeout time.Duration
}
// New create a new default storage.
func New(l log.Logger, b backend.Backend, timeout time.Duration) Storage {
return &storage{l, b, timeout}
}
// Get writes contents of the given object with given key from remote storage to io.Writer.
func (s *storage) Get(p string, w io.Writer) error {
ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
defer cancel()
if err := s.b.Get(ctx, p, w); err != nil {
return fmt.Errorf("storage backend get failure, %w", err)
}
return nil
}
// Put writes contents of io.Reader to remote storage at given key location.
func (s *storage) Put(p string, r io.Reader) error {
ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
defer cancel()
if err := s.b.Put(ctx, p, r); err != nil {
return fmt.Errorf("storage backend put failure, %w", err)
}
return nil
}
// Exists checks if object with given key exists in remote storage.
func (s *storage) Exists(p string) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
defer cancel()
ret, err := s.b.Exists(ctx, p)
if err != nil {
return ret, fmt.Errorf("storage backend exists failure, %w", err)
}
return ret, nil
}
// List lists contents of the given directory by given key from remote storage.
func (s *storage) List(p string) ([]backend.FileEntry, error) {
// Implement me!
// Make sure consumer utilizes context.
return []backend.FileEntry{}, nil
}
// Delete deletes the object from remote storage.
func (s *storage) Delete(p string) error {
// Implement me!
// Make sure consumer utilizes context.
return nil
}