1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package gcsfs
18
19 import (
20 "context"
21 "os"
22 "time"
23
24 "cloud.google.com/go/storage"
25 "github.com/googleapis/google-cloud-go-testing/storage/stiface"
26 "github.com/spf13/afero"
27
28 "google.golang.org/api/option"
29 )
30
31 type GcsFs struct {
32 source *Fs
33 }
34
35
36
37
38 func NewGcsFS(ctx context.Context, opts ...option.ClientOption) (afero.Fs, error) {
39 if json := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON"); json != "" {
40 opts = append(opts, option.WithCredentialsJSON([]byte(json)))
41 }
42 client, err := storage.NewClient(ctx, opts...)
43 if err != nil {
44 return nil, err
45 }
46
47 return NewGcsFSFromClient(ctx, client)
48 }
49
50
51 func NewGcsFSWithSeparator(ctx context.Context, folderSeparator string, opts ...option.ClientOption) (afero.Fs, error) {
52 client, err := storage.NewClient(ctx, opts...)
53 if err != nil {
54 return nil, err
55 }
56
57 return NewGcsFSFromClientWithSeparator(ctx, client, folderSeparator)
58 }
59
60
61 func NewGcsFSFromClient(ctx context.Context, client *storage.Client) (afero.Fs, error) {
62 c := stiface.AdaptClient(client)
63
64 return &GcsFs{NewGcsFs(ctx, c)}, nil
65 }
66
67
68 func NewGcsFSFromClientWithSeparator(ctx context.Context, client *storage.Client, folderSeparator string) (afero.Fs, error) {
69 c := stiface.AdaptClient(client)
70
71 return &GcsFs{NewGcsFsWithSeparator(ctx, c, folderSeparator)}, nil
72 }
73
74
75
76 func (fs *GcsFs) Name() string {
77 return fs.source.Name()
78 }
79
80 func (fs *GcsFs) Create(name string) (afero.File, error) {
81 return fs.source.Create(name)
82 }
83
84 func (fs *GcsFs) Mkdir(name string, perm os.FileMode) error {
85 return fs.source.Mkdir(name, perm)
86 }
87
88 func (fs *GcsFs) MkdirAll(path string, perm os.FileMode) error {
89 return fs.source.MkdirAll(path, perm)
90 }
91
92 func (fs *GcsFs) Open(name string) (afero.File, error) {
93 return fs.source.Open(name)
94 }
95
96 func (fs *GcsFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
97 return fs.source.OpenFile(name, flag, perm)
98 }
99
100 func (fs *GcsFs) Remove(name string) error {
101 return fs.source.Remove(name)
102 }
103
104 func (fs *GcsFs) RemoveAll(path string) error {
105 return fs.source.RemoveAll(path)
106 }
107
108 func (fs *GcsFs) Rename(oldname, newname string) error {
109 return fs.source.Rename(oldname, newname)
110 }
111
112 func (fs *GcsFs) Stat(name string) (os.FileInfo, error) {
113 return fs.source.Stat(name)
114 }
115
116 func (fs *GcsFs) Chmod(name string, mode os.FileMode) error {
117 return fs.source.Chmod(name, mode)
118 }
119
120 func (fs *GcsFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
121 return fs.source.Chtimes(name, atime, mtime)
122 }
123
124 func (fs *GcsFs) Chown(name string, uid, gid int) error {
125 return fs.source.Chown(name, uid, gid)
126 }
127
View as plain text