2015-09-22 17:47:16 +00:00
|
|
|
// Package googlecloudstorage provides an interface to Google Cloud Storage
|
2014-07-13 16:54:03 +00:00
|
|
|
package googlecloudstorage
|
|
|
|
|
|
|
|
/*
|
2014-07-14 22:35:41 +00:00
|
|
|
Notes
|
2014-07-13 16:54:03 +00:00
|
|
|
|
2014-07-14 22:35:41 +00:00
|
|
|
Can't set Updated but can set Metadata on object creation
|
2014-07-13 16:54:03 +00:00
|
|
|
|
2014-07-14 22:35:41 +00:00
|
|
|
Patch needs full_control not just read_write
|
|
|
|
|
|
|
|
FIXME Patch/Delete/Get isn't working with files with spaces in - giving 404 error
|
|
|
|
- https://code.google.com/p/google-api-go-client/issues/detail?id=64
|
2014-07-13 16:54:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-08-18 07:55:09 +00:00
|
|
|
"log"
|
2014-07-13 16:54:03 +00:00
|
|
|
"net/http"
|
2014-07-14 09:45:28 +00:00
|
|
|
"path"
|
2014-07-13 16:54:03 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-08-18 07:55:09 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google"
|
2014-12-12 20:02:08 +00:00
|
|
|
"google.golang.org/api/googleapi"
|
|
|
|
"google.golang.org/api/storage/v1"
|
2014-07-13 16:54:03 +00:00
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2015-08-18 07:55:09 +00:00
|
|
|
"github.com/ncw/rclone/oauthutil"
|
2014-07-13 16:54:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-08-18 07:55:09 +00:00
|
|
|
rcloneClientID = "202264815644.apps.googleusercontent.com"
|
2015-09-01 21:33:34 +00:00
|
|
|
rcloneClientSecret = "8p/yms3OlNXE9OTDl/HLypf9gdiJ5cT3"
|
2014-07-29 16:50:07 +00:00
|
|
|
timeFormatIn = time.RFC3339
|
|
|
|
timeFormatOut = "2006-01-02T15:04:05.000000000Z07:00"
|
2014-07-13 16:54:03 +00:00
|
|
|
metaMtime = "mtime" // key to store mtime under in metadata
|
|
|
|
listChunks = 256 // chunk size to read directory listings
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Description of how to auth for this app
|
2015-08-18 07:55:09 +00:00
|
|
|
storageConfig = &oauth2.Config{
|
|
|
|
Scopes: []string{storage.DevstorageFullControlScope},
|
|
|
|
Endpoint: google.Endpoint,
|
|
|
|
ClientID: rcloneClientID,
|
2015-09-01 21:33:34 +00:00
|
|
|
ClientSecret: fs.Reveal(rcloneClientSecret),
|
2015-08-18 07:55:09 +00:00
|
|
|
RedirectURL: oauthutil.TitleBarRedirectURL,
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
2015-09-22 17:47:16 +00:00
|
|
|
fs.Register(&fs.Info{
|
2014-07-13 16:54:03 +00:00
|
|
|
Name: "google cloud storage",
|
|
|
|
NewFs: NewFs,
|
|
|
|
Config: func(name string) {
|
2015-08-18 07:55:09 +00:00
|
|
|
err := oauthutil.Config(name, storageConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to configure token: %v", err)
|
|
|
|
}
|
2014-07-13 16:54:03 +00:00
|
|
|
},
|
|
|
|
Options: []fs.Option{{
|
2015-09-16 19:08:40 +00:00
|
|
|
Name: oauthutil.ConfigClientID,
|
2015-10-03 13:23:12 +00:00
|
|
|
Help: "Google Application Client Id - leave blank normally.",
|
2014-07-13 16:54:03 +00:00
|
|
|
}, {
|
2015-09-16 19:08:40 +00:00
|
|
|
Name: oauthutil.ConfigClientSecret,
|
2015-10-03 13:23:12 +00:00
|
|
|
Help: "Google Application Client Secret - leave blank normally.",
|
2014-07-14 22:35:41 +00:00
|
|
|
}, {
|
|
|
|
Name: "project_number",
|
|
|
|
Help: "Project number optional - needed only for list/create/delete buckets - see your developer console.",
|
|
|
|
}, {
|
|
|
|
Name: "object_acl",
|
|
|
|
Help: "Access Control List for new objects.",
|
|
|
|
Examples: []fs.OptionExample{{
|
|
|
|
Value: "authenticatedRead",
|
|
|
|
Help: "Object owner gets OWNER access, and all Authenticated Users get READER access.",
|
|
|
|
}, {
|
|
|
|
Value: "bucketOwnerFullControl",
|
|
|
|
Help: "Object owner gets OWNER access, and project team owners get OWNER access.",
|
|
|
|
}, {
|
|
|
|
Value: "bucketOwnerRead",
|
|
|
|
Help: "Object owner gets OWNER access, and project team owners get READER access.",
|
|
|
|
}, {
|
|
|
|
Value: "private",
|
|
|
|
Help: "Object owner gets OWNER access [default if left blank].",
|
|
|
|
}, {
|
|
|
|
Value: "projectPrivate",
|
|
|
|
Help: "Object owner gets OWNER access, and project team members get access according to their roles.",
|
|
|
|
}, {
|
|
|
|
Value: "publicRead",
|
|
|
|
Help: "Object owner gets OWNER access, and all Users get READER access.",
|
|
|
|
}},
|
|
|
|
}, {
|
|
|
|
Name: "bucket_acl",
|
|
|
|
Help: "Access Control List for new buckets.",
|
|
|
|
Examples: []fs.OptionExample{{
|
|
|
|
Value: "authenticatedRead",
|
|
|
|
Help: "Project team owners get OWNER access, and all Authenticated Users get READER access.",
|
|
|
|
}, {
|
|
|
|
Value: "private",
|
|
|
|
Help: "Project team owners get OWNER access [default if left blank].",
|
|
|
|
}, {
|
|
|
|
Value: "projectPrivate",
|
|
|
|
Help: "Project team members get access according to their roles.",
|
|
|
|
}, {
|
|
|
|
Value: "publicRead",
|
|
|
|
Help: "Project team owners get OWNER access, and all Users get READER access.",
|
|
|
|
}, {
|
|
|
|
Value: "publicReadWrite",
|
|
|
|
Help: "Project team owners get OWNER access, and all Users get WRITER access.",
|
|
|
|
}},
|
2014-07-13 16:54:03 +00:00
|
|
|
}},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// FsStorage represents a remote storage server
|
|
|
|
type FsStorage struct {
|
2015-08-22 15:53:11 +00:00
|
|
|
name string // name of this remote
|
2014-07-13 16:54:03 +00:00
|
|
|
svc *storage.Service // the connection to the storage server
|
|
|
|
client *http.Client // authorized client
|
|
|
|
bucket string // the bucket we are working on
|
|
|
|
root string // the path we are working on if any
|
|
|
|
projectNumber string // used for finding buckets
|
2014-07-14 22:35:41 +00:00
|
|
|
objectAcl string // used when creating new objects
|
|
|
|
bucketAcl string // used when creating new buckets
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FsObjectStorage describes a storage object
|
|
|
|
//
|
|
|
|
// Will definitely have info but maybe not meta
|
|
|
|
type FsObjectStorage struct {
|
|
|
|
storage *FsStorage // what this object is part of
|
|
|
|
remote string // The remote path
|
|
|
|
url string // download path
|
|
|
|
md5sum string // The MD5Sum of the object
|
|
|
|
bytes int64 // Bytes in the object
|
|
|
|
modTime time.Time // Modified time of the object
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Name of the remote (as passed into NewFs)
|
2015-08-22 15:53:11 +00:00
|
|
|
func (f *FsStorage) Name() string {
|
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Root of the remote (as passed into NewFs)
|
2015-09-01 19:45:27 +00:00
|
|
|
func (f *FsStorage) Root() string {
|
|
|
|
if f.root == "" {
|
|
|
|
return f.bucket
|
|
|
|
}
|
|
|
|
return f.bucket + "/" + f.root
|
|
|
|
}
|
|
|
|
|
2014-07-13 16:54:03 +00:00
|
|
|
// String converts this FsStorage to a string
|
|
|
|
func (f *FsStorage) String() string {
|
|
|
|
if f.root == "" {
|
|
|
|
return fmt.Sprintf("Storage bucket %s", f.bucket)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Storage bucket %s path %s", f.bucket, f.root)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pattern to match a storage path
|
|
|
|
var matcher = regexp.MustCompile(`^([^/]*)(.*)$`)
|
|
|
|
|
|
|
|
// parseParse parses a storage 'url'
|
|
|
|
func parsePath(path string) (bucket, directory string, err error) {
|
|
|
|
parts := matcher.FindStringSubmatch(path)
|
|
|
|
if parts == nil {
|
|
|
|
err = fmt.Errorf("Couldn't find bucket in storage path %q", path)
|
|
|
|
} else {
|
|
|
|
bucket, directory = parts[1], parts[2]
|
|
|
|
directory = strings.Trim(directory, "/")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFs contstructs an FsStorage from the path, bucket:path
|
|
|
|
func NewFs(name, root string) (fs.Fs, error) {
|
2015-08-18 07:55:09 +00:00
|
|
|
oAuthClient, err := oauthutil.NewClient(name, storageConfig)
|
2014-07-13 16:54:03 +00:00
|
|
|
if err != nil {
|
2015-08-18 07:55:09 +00:00
|
|
|
log.Fatalf("Failed to configure Google Cloud Storage: %v", err)
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bucket, directory, err := parsePath(root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f := &FsStorage{
|
2015-08-22 15:53:11 +00:00
|
|
|
name: name,
|
2014-07-13 16:54:03 +00:00
|
|
|
bucket: bucket,
|
|
|
|
root: directory,
|
|
|
|
projectNumber: fs.ConfigFile.MustValue(name, "project_number"),
|
2014-07-14 22:35:41 +00:00
|
|
|
objectAcl: fs.ConfigFile.MustValue(name, "object_acl"),
|
|
|
|
bucketAcl: fs.ConfigFile.MustValue(name, "bucket_acl"),
|
|
|
|
}
|
|
|
|
if f.objectAcl == "" {
|
|
|
|
f.objectAcl = "private"
|
|
|
|
}
|
|
|
|
if f.bucketAcl == "" {
|
|
|
|
f.bucketAcl = "private"
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new authorized Drive client.
|
2015-08-18 07:55:09 +00:00
|
|
|
f.client = oAuthClient
|
2014-07-13 16:54:03 +00:00
|
|
|
f.svc, err = storage.New(f.client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Couldn't create Google Cloud Storage client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.root != "" {
|
|
|
|
f.root += "/"
|
|
|
|
// Check to see if the object exists
|
2014-07-14 09:45:28 +00:00
|
|
|
_, err = f.svc.Objects.Get(bucket, directory).Do()
|
|
|
|
if err == nil {
|
|
|
|
remote := path.Base(directory)
|
|
|
|
f.root = path.Dir(directory)
|
|
|
|
if f.root == "." {
|
|
|
|
f.root = ""
|
|
|
|
} else {
|
|
|
|
f.root += "/"
|
|
|
|
}
|
|
|
|
obj := f.NewFsObject(remote)
|
|
|
|
// return a Fs Limited to this object
|
|
|
|
return fs.NewLimited(f, obj), nil
|
|
|
|
}
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an FsObject from a path
|
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2014-07-29 16:50:07 +00:00
|
|
|
func (f *FsStorage) newFsObjectWithInfo(remote string, info *storage.Object) fs.Object {
|
2014-07-13 16:54:03 +00:00
|
|
|
o := &FsObjectStorage{
|
|
|
|
storage: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
|
|
|
if info != nil {
|
|
|
|
o.setMetaData(info)
|
|
|
|
} else {
|
|
|
|
err := o.readMetaData() // reads info and meta, returning an error
|
|
|
|
if err != nil {
|
|
|
|
// logged already FsDebug("Failed to read info: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// NewFsObject returns an FsObject from a path
|
2014-07-13 16:54:03 +00:00
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
|
|
|
func (f *FsStorage) NewFsObject(remote string) fs.Object {
|
2014-07-29 16:50:07 +00:00
|
|
|
return f.newFsObjectWithInfo(remote, nil)
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// list the objects into the function supplied
|
|
|
|
//
|
|
|
|
// If directories is set it only sends directories
|
|
|
|
func (f *FsStorage) list(directories bool, fn func(string, *storage.Object)) {
|
|
|
|
list := f.svc.Objects.List(f.bucket).Prefix(f.root).MaxResults(listChunks)
|
|
|
|
if directories {
|
|
|
|
list = list.Delimiter("/")
|
|
|
|
}
|
|
|
|
rootLength := len(f.root)
|
|
|
|
for {
|
|
|
|
objects, err := list.Do()
|
|
|
|
if err != nil {
|
|
|
|
fs.Stats.Error()
|
2015-08-08 19:10:31 +00:00
|
|
|
fs.ErrorLog(f, "Couldn't read bucket %q: %s", f.bucket, err)
|
2014-07-13 16:54:03 +00:00
|
|
|
return
|
|
|
|
}
|
2014-07-28 17:04:52 +00:00
|
|
|
if !directories {
|
|
|
|
for _, object := range objects.Items {
|
|
|
|
if !strings.HasPrefix(object.Name, f.root) {
|
|
|
|
fs.Log(f, "Odd name received %q", object.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
remote := object.Name[rootLength:]
|
|
|
|
fn(remote, object)
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
2014-07-28 17:04:52 +00:00
|
|
|
} else {
|
|
|
|
var object storage.Object
|
|
|
|
for _, prefix := range objects.Prefixes {
|
|
|
|
if !strings.HasSuffix(prefix, "/") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fn(prefix[:len(prefix)-1], &object)
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if objects.NextPageToken == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
list.PageToken(objects.NextPageToken)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// List walks the path returning a channel of FsObjects
|
2014-07-13 16:54:03 +00:00
|
|
|
func (f *FsStorage) List() fs.ObjectsChan {
|
|
|
|
out := make(fs.ObjectsChan, fs.Config.Checkers)
|
|
|
|
if f.bucket == "" {
|
|
|
|
// Return no objects at top level list
|
|
|
|
close(out)
|
|
|
|
fs.Stats.Error()
|
2015-08-08 19:10:31 +00:00
|
|
|
fs.ErrorLog(f, "Can't list objects at root - choose a bucket using lsd")
|
2014-07-13 16:54:03 +00:00
|
|
|
} else {
|
|
|
|
// List the objects
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
f.list(false, func(remote string, object *storage.Object) {
|
2014-07-29 16:50:07 +00:00
|
|
|
if fs := f.newFsObjectWithInfo(remote, object); fs != nil {
|
2014-07-13 16:54:03 +00:00
|
|
|
out <- fs
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// ListDir lists the buckets
|
2014-07-13 16:54:03 +00:00
|
|
|
func (f *FsStorage) ListDir() fs.DirChan {
|
|
|
|
out := make(fs.DirChan, fs.Config.Checkers)
|
|
|
|
if f.bucket == "" {
|
|
|
|
// List the buckets
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
if f.projectNumber == "" {
|
|
|
|
fs.Stats.Error()
|
2015-08-08 19:10:31 +00:00
|
|
|
fs.ErrorLog(f, "Can't list buckets without project number")
|
2014-07-13 16:54:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
listBuckets := f.svc.Buckets.List(f.projectNumber).MaxResults(listChunks)
|
|
|
|
for {
|
|
|
|
buckets, err := listBuckets.Do()
|
|
|
|
if err != nil {
|
|
|
|
fs.Stats.Error()
|
2015-08-08 19:10:31 +00:00
|
|
|
fs.ErrorLog(f, "Couldn't list buckets: %v", err)
|
2014-07-13 16:54:03 +00:00
|
|
|
break
|
|
|
|
} else {
|
|
|
|
for _, bucket := range buckets.Items {
|
|
|
|
out <- &fs.Dir{
|
|
|
|
Name: bucket.Name,
|
|
|
|
Bytes: 0,
|
|
|
|
Count: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if buckets.NextPageToken == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
listBuckets.PageToken(buckets.NextPageToken)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
// List the directories in the path in the bucket
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
f.list(true, func(remote string, object *storage.Object) {
|
|
|
|
out <- &fs.Dir{
|
|
|
|
Name: remote,
|
|
|
|
Bytes: int64(object.Size),
|
|
|
|
Count: 0,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the object into the bucket
|
|
|
|
//
|
|
|
|
// Copy the reader in to the new object which is returned
|
|
|
|
//
|
|
|
|
// The new object may have been created if an error is returned
|
|
|
|
func (f *FsStorage) Put(in io.Reader, remote string, modTime time.Time, size int64) (fs.Object, error) {
|
|
|
|
// Temporary FsObject under construction
|
2014-12-23 11:03:34 +00:00
|
|
|
o := &FsObjectStorage{storage: f, remote: remote}
|
|
|
|
return o, o.Update(in, modTime, size)
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates the bucket if it doesn't exist
|
|
|
|
func (f *FsStorage) Mkdir() error {
|
|
|
|
_, err := f.svc.Buckets.Get(f.bucket).Do()
|
|
|
|
if err == nil {
|
|
|
|
// Bucket already exists
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.projectNumber == "" {
|
|
|
|
return fmt.Errorf("Can't make bucket without project number")
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket := storage.Bucket{
|
|
|
|
Name: f.bucket,
|
|
|
|
}
|
2014-07-14 22:35:41 +00:00
|
|
|
_, err = f.svc.Buckets.Insert(f.projectNumber, &bucket).PredefinedAcl(f.bucketAcl).Do()
|
2014-07-13 16:54:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir deletes the bucket
|
|
|
|
//
|
|
|
|
// Returns an error if it isn't empty: Error 409: The bucket you tried
|
|
|
|
// to delete was not empty.
|
|
|
|
func (f *FsStorage) Rmdir() error {
|
|
|
|
return f.svc.Buckets.Delete(f.bucket).Do()
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Precision returns the precision
|
|
|
|
func (f *FsStorage) Precision() time.Duration {
|
2014-07-13 16:54:03 +00:00
|
|
|
return time.Nanosecond
|
|
|
|
}
|
|
|
|
|
2015-02-14 18:48:08 +00:00
|
|
|
// Copy src to this remote using server side copy operations.
|
|
|
|
//
|
|
|
|
// This is stored with the remote path given
|
|
|
|
//
|
|
|
|
// It returns the destination Object and a possible error
|
|
|
|
//
|
|
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
|
|
//
|
|
|
|
// If it isn't possible then return fs.ErrorCantCopy
|
|
|
|
func (f *FsStorage) Copy(src fs.Object, remote string) (fs.Object, error) {
|
|
|
|
srcObj, ok := src.(*FsObjectStorage)
|
|
|
|
if !ok {
|
|
|
|
fs.Debug(src, "Can't copy - not same remote type")
|
|
|
|
return nil, fs.ErrorCantCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
// Temporary FsObject under construction
|
|
|
|
dstObj := &FsObjectStorage{storage: f, remote: remote}
|
|
|
|
|
|
|
|
srcBucket := srcObj.storage.bucket
|
|
|
|
srcObject := srcObj.storage.root + srcObj.remote
|
|
|
|
dstBucket := f.bucket
|
|
|
|
dstObject := f.root + remote
|
|
|
|
newObject, err := f.svc.Objects.Copy(srcBucket, srcObject, dstBucket, dstObject, nil).Do()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Set the metadata for the new object while we have it
|
|
|
|
dstObj.setMetaData(newObject)
|
|
|
|
return dstObj, nil
|
|
|
|
}
|
|
|
|
|
2014-07-13 16:54:03 +00:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Fs returns the parent Fs
|
2014-07-13 16:54:03 +00:00
|
|
|
func (o *FsObjectStorage) Fs() fs.Fs {
|
|
|
|
return o.storage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a string version
|
|
|
|
func (o *FsObjectStorage) String() string {
|
|
|
|
if o == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return o.remote
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Remote returns the remote path
|
2014-07-13 16:54:03 +00:00
|
|
|
func (o *FsObjectStorage) Remote() string {
|
|
|
|
return o.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
// Md5sum returns the Md5sum of an object returning a lowercase hex string
|
|
|
|
func (o *FsObjectStorage) Md5sum() (string, error) {
|
|
|
|
return o.md5sum, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
|
|
|
func (o *FsObjectStorage) Size() int64 {
|
|
|
|
return o.bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
// setMetaData sets the fs data from a storage.Object
|
|
|
|
func (o *FsObjectStorage) setMetaData(info *storage.Object) {
|
|
|
|
o.url = info.MediaLink
|
|
|
|
o.bytes = int64(info.Size)
|
|
|
|
|
|
|
|
// Read md5sum
|
|
|
|
md5sumData, err := base64.StdEncoding.DecodeString(info.Md5Hash)
|
|
|
|
if err != nil {
|
|
|
|
fs.Log(o, "Bad MD5 decode: %v", err)
|
|
|
|
} else {
|
|
|
|
o.md5sum = hex.EncodeToString(md5sumData)
|
|
|
|
}
|
|
|
|
|
|
|
|
// read mtime out of metadata if available
|
|
|
|
mtimeString, ok := info.Metadata[metaMtime]
|
|
|
|
if ok {
|
2014-07-29 16:50:07 +00:00
|
|
|
modTime, err := time.Parse(timeFormatIn, mtimeString)
|
2014-07-13 16:54:03 +00:00
|
|
|
if err == nil {
|
|
|
|
o.modTime = modTime
|
|
|
|
return
|
|
|
|
}
|
2015-09-22 17:47:16 +00:00
|
|
|
fs.Debug(o, "Failed to read mtime from metadata: %s", err)
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to the Updated time
|
2014-07-29 16:50:07 +00:00
|
|
|
modTime, err := time.Parse(timeFormatIn, info.Updated)
|
2014-07-13 16:54:03 +00:00
|
|
|
if err != nil {
|
|
|
|
fs.Log(o, "Bad time decode: %v", err)
|
|
|
|
} else {
|
|
|
|
o.modTime = modTime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// readMetaData gets the metadata if it hasn't already been fetched
|
|
|
|
//
|
|
|
|
// it also sets the info
|
|
|
|
func (o *FsObjectStorage) readMetaData() (err error) {
|
|
|
|
if !o.modTime.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
object, err := o.storage.svc.Objects.Get(o.storage.bucket, o.storage.root+o.remote).Do()
|
|
|
|
if err != nil {
|
|
|
|
fs.Debug(o, "Failed to read info: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.setMetaData(object)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime returns the modification time of the object
|
|
|
|
//
|
|
|
|
// It attempts to read the objects mtime and if that isn't present the
|
|
|
|
// LastModified returned in the http headers
|
|
|
|
func (o *FsObjectStorage) ModTime() time.Time {
|
|
|
|
err := o.readMetaData()
|
|
|
|
if err != nil {
|
|
|
|
// fs.Log(o, "Failed to read metadata: %s", err)
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
return o.modTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns metadata for an object
|
|
|
|
func metadataFromModTime(modTime time.Time) map[string]string {
|
|
|
|
metadata := make(map[string]string, 1)
|
2014-07-29 16:50:07 +00:00
|
|
|
metadata[metaMtime] = modTime.Format(timeFormatOut)
|
2014-07-13 16:54:03 +00:00
|
|
|
return metadata
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// SetModTime sets the modification time of the local fs object
|
2014-07-13 16:54:03 +00:00
|
|
|
func (o *FsObjectStorage) SetModTime(modTime time.Time) {
|
|
|
|
// This only adds metadata so will perserve other metadata
|
|
|
|
object := storage.Object{
|
|
|
|
Bucket: o.storage.bucket,
|
|
|
|
Name: o.storage.root + o.remote,
|
|
|
|
Metadata: metadataFromModTime(modTime),
|
|
|
|
}
|
2014-07-28 19:07:02 +00:00
|
|
|
newObject, err := o.storage.svc.Objects.Patch(o.storage.bucket, o.storage.root+o.remote, &object).Do()
|
2014-07-13 16:54:03 +00:00
|
|
|
if err != nil {
|
|
|
|
fs.Stats.Error()
|
2015-08-08 19:10:31 +00:00
|
|
|
fs.ErrorLog(o, "Failed to update remote mtime: %s", err)
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
2014-07-28 19:07:02 +00:00
|
|
|
o.setMetaData(newObject)
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Storable returns a boolean as to whether this object is storable
|
2014-07-13 16:54:03 +00:00
|
|
|
func (o *FsObjectStorage) Storable() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open an object for read
|
|
|
|
func (o *FsObjectStorage) Open() (in io.ReadCloser, err error) {
|
2014-07-15 10:18:43 +00:00
|
|
|
// This is slightly complicated by Go here insisting on
|
|
|
|
// decoding the %2F in URLs into / which is legal in http, but
|
|
|
|
// unfortunately not what the storage server wants.
|
|
|
|
//
|
|
|
|
// So first encode all the % into their encoded form
|
|
|
|
// URL will decode them giving our original escaped string
|
|
|
|
url := strings.Replace(o.url, "%", "%25", -1)
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// SetOpaque sets Opaque such that HTTP requests to it don't
|
|
|
|
// alter any hex-escaped characters
|
|
|
|
googleapi.SetOpaque(req.URL)
|
2014-07-13 16:54:03 +00:00
|
|
|
req.Header.Set("User-Agent", fs.UserAgent)
|
|
|
|
res, err := o.storage.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if res.StatusCode != 200 {
|
2014-07-25 17:19:49 +00:00
|
|
|
_ = res.Body.Close() // ignore error
|
2014-07-13 16:54:03 +00:00
|
|
|
return nil, fmt.Errorf("Bad response: %d: %s", res.StatusCode, res.Status)
|
|
|
|
}
|
|
|
|
return res.Body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the object with the contents of the io.Reader, modTime and size
|
|
|
|
//
|
|
|
|
// The new object may have been created if an error is returned
|
|
|
|
func (o *FsObjectStorage) Update(in io.Reader, modTime time.Time, size int64) error {
|
|
|
|
object := storage.Object{
|
2014-07-14 11:44:31 +00:00
|
|
|
Bucket: o.storage.bucket,
|
|
|
|
Name: o.storage.root + o.remote,
|
2015-03-01 12:38:31 +00:00
|
|
|
ContentType: fs.MimeType(o),
|
2014-07-14 11:44:31 +00:00
|
|
|
Size: uint64(size),
|
2014-07-29 16:50:07 +00:00
|
|
|
Updated: modTime.Format(timeFormatOut), // Doesn't get set
|
2014-07-14 11:44:31 +00:00
|
|
|
Metadata: metadataFromModTime(modTime),
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
2014-07-16 11:12:36 +00:00
|
|
|
newObject, err := o.storage.svc.Objects.Insert(o.storage.bucket, &object).Media(in).Name(object.Name).PredefinedAcl(o.storage.objectAcl).Do()
|
2014-07-21 20:25:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-16 11:12:36 +00:00
|
|
|
// Set the metadata for the new object while we have it
|
|
|
|
o.setMetaData(newObject)
|
2014-07-21 20:25:46 +00:00
|
|
|
return nil
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an object
|
|
|
|
func (o *FsObjectStorage) Remove() error {
|
2014-07-14 22:35:41 +00:00
|
|
|
return o.storage.svc.Objects.Delete(o.storage.bucket, o.storage.root+o.remote).Do()
|
2014-07-13 16:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
|
|
|
var _ fs.Fs = &FsStorage{}
|
2015-02-14 18:48:08 +00:00
|
|
|
var _ fs.Copier = &FsStorage{}
|
2014-07-13 16:54:03 +00:00
|
|
|
var _ fs.Object = &FsObjectStorage{}
|