2021-03-31 14:55:22 +03:00
|
|
|
// Copyright © 2021 Vasily Ovchinnikov <vasily@remerge.io>.
|
|
|
|
//
|
|
|
|
// The code in this file is derived from afero fork github.com/Zatte/afero by Mikael Rapp
|
|
|
|
// licensed under Apache License 2.0.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
package gcsfs
|
2021-03-31 14:55:22 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
2022-12-12 18:01:24 +03:00
|
|
|
"git.internal/re/afero"
|
2021-03-31 14:55:22 +03:00
|
|
|
"github.com/googleapis/google-cloud-go-testing/storage/stiface"
|
2021-04-12 21:01:12 +03:00
|
|
|
|
2021-03-31 14:55:22 +03:00
|
|
|
"google.golang.org/api/option"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GcsFs struct {
|
2021-12-27 20:55:57 +03:00
|
|
|
source *Fs
|
2021-03-31 14:55:22 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
// NewGcsFS creates a GCS file system, automatically instantiating and decorating the storage client.
|
2021-03-31 14:55:22 +03:00
|
|
|
// You can provide additional options to be passed to the client creation, as per
|
|
|
|
// cloud.google.com/go/storage documentation
|
2021-12-27 20:55:57 +03:00
|
|
|
func NewGcsFS(ctx context.Context, opts ...option.ClientOption) (afero.Fs, error) {
|
2021-10-05 19:17:13 +03:00
|
|
|
if json := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON"); json != "" {
|
|
|
|
opts = append(opts, option.WithCredentialsJSON([]byte(json)))
|
|
|
|
}
|
2021-03-31 14:55:22 +03:00
|
|
|
client, err := storage.NewClient(ctx, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
return NewGcsFSFromClient(ctx, client)
|
2021-03-31 14:55:22 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
// NewGcsFSWithSeparator is the same as NewGcsFS, but the files system will use the provided folder separator.
|
2021-12-27 20:55:57 +03:00
|
|
|
func NewGcsFSWithSeparator(ctx context.Context, folderSeparator string, opts ...option.ClientOption) (afero.Fs, error) {
|
2021-03-31 14:55:22 +03:00
|
|
|
client, err := storage.NewClient(ctx, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
return NewGcsFSFromClientWithSeparator(ctx, client, folderSeparator)
|
2021-03-31 14:55:22 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
// NewGcsFSFromClient creates a GCS file system from a given storage client
|
2021-12-27 20:55:57 +03:00
|
|
|
func NewGcsFSFromClient(ctx context.Context, client *storage.Client) (afero.Fs, error) {
|
2021-03-31 14:55:22 +03:00
|
|
|
c := stiface.AdaptClient(client)
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
return &GcsFs{NewGcsFs(ctx, c)}, nil
|
2021-03-31 14:55:22 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 21:01:12 +03:00
|
|
|
// NewGcsFSFromClientWithSeparator is the same as NewGcsFSFromClient, but the file system will use the provided folder separator.
|
2021-12-27 20:55:57 +03:00
|
|
|
func NewGcsFSFromClientWithSeparator(ctx context.Context, client *storage.Client, folderSeparator string) (afero.Fs, error) {
|
2021-03-31 14:55:22 +03:00
|
|
|
c := stiface.AdaptClient(client)
|
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
return &GcsFs{NewGcsFsWithSeparator(ctx, c, folderSeparator)}, nil
|
2021-03-31 14:55:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wraps gcs.GcsFs and convert some return types to afero interfaces.
|
2021-04-12 21:01:12 +03:00
|
|
|
|
2021-03-31 14:55:22 +03:00
|
|
|
func (fs *GcsFs) Name() string {
|
|
|
|
return fs.source.Name()
|
|
|
|
}
|
2022-12-12 18:01:24 +03:00
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *GcsFs) Create(name string) (afero.File, error) {
|
2021-03-31 14:55:22 +03:00
|
|
|
return fs.source.Create(name)
|
|
|
|
}
|
2022-12-12 18:01:24 +03:00
|
|
|
|
2021-03-31 14:55:22 +03:00
|
|
|
func (fs *GcsFs) Mkdir(name string, perm os.FileMode) error {
|
|
|
|
return fs.source.Mkdir(name, perm)
|
|
|
|
}
|
2022-12-12 18:01:24 +03:00
|
|
|
|
2021-03-31 14:55:22 +03:00
|
|
|
func (fs *GcsFs) MkdirAll(path string, perm os.FileMode) error {
|
|
|
|
return fs.source.MkdirAll(path, perm)
|
|
|
|
}
|
2022-12-12 18:01:24 +03:00
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *GcsFs) Open(name string) (afero.File, error) {
|
2021-03-31 14:55:22 +03:00
|
|
|
return fs.source.Open(name)
|
|
|
|
}
|
2022-12-12 18:01:24 +03:00
|
|
|
|
2021-12-27 20:55:57 +03:00
|
|
|
func (fs *GcsFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
|
2021-03-31 14:55:22 +03:00
|
|
|
return fs.source.OpenFile(name, flag, perm)
|
|
|
|
}
|
2022-12-12 18:01:24 +03:00
|
|
|
|
2021-03-31 14:55:22 +03:00
|
|
|
func (fs *GcsFs) Remove(name string) error {
|
|
|
|
return fs.source.Remove(name)
|
|
|
|
}
|
2022-12-12 18:01:24 +03:00
|
|
|
|
2021-03-31 14:55:22 +03:00
|
|
|
func (fs *GcsFs) RemoveAll(path string) error {
|
|
|
|
return fs.source.RemoveAll(path)
|
|
|
|
}
|
2022-12-12 18:01:24 +03:00
|
|
|
|
2021-03-31 14:55:22 +03:00
|
|
|
func (fs *GcsFs) Rename(oldname, newname string) error {
|
|
|
|
return fs.source.Rename(oldname, newname)
|
|
|
|
}
|
2022-12-12 18:01:24 +03:00
|
|
|
|
2021-03-31 14:55:22 +03:00
|
|
|
func (fs *GcsFs) Stat(name string) (os.FileInfo, error) {
|
|
|
|
return fs.source.Stat(name)
|
|
|
|
}
|
2022-12-12 18:01:24 +03:00
|
|
|
|
2021-03-31 14:55:22 +03:00
|
|
|
func (fs *GcsFs) Chmod(name string, mode os.FileMode) error {
|
|
|
|
return fs.source.Chmod(name, mode)
|
|
|
|
}
|
2022-12-12 18:01:24 +03:00
|
|
|
|
2021-03-31 14:55:22 +03:00
|
|
|
func (fs *GcsFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
|
|
|
|
return fs.source.Chtimes(name, atime, mtime)
|
|
|
|
}
|
2022-12-12 18:01:24 +03:00
|
|
|
|
2021-03-31 14:55:22 +03:00
|
|
|
func (fs *GcsFs) Chown(name string, uid, gid int) error {
|
|
|
|
return fs.source.Chown(name, uid, gid)
|
|
|
|
}
|