| 1 |
// Copyright 2018 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package stiface |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
|
| 20 |
"cloud.google.com/go/storage" |
| 21 |
) |
| 22 |
|
| 23 |
// AdaptClient adapts a storage.Client so that it satisfies the Client |
| 24 |
// interface. |
| 25 |
func AdaptClient(c *storage.Client) Client { |
| 26 |
return client{c} |
| 27 |
} |
| 28 |
|
| 29 |
type ( |
| 30 |
client struct{ *storage.Client } |
| 31 |
bucketHandle struct{ *storage.BucketHandle } |
| 32 |
objectHandle struct{ *storage.ObjectHandle } |
| 33 |
bucketIterator struct{ *storage.BucketIterator } |
| 34 |
objectIterator struct{ *storage.ObjectIterator } |
| 35 |
reader struct{ *storage.Reader } |
| 36 |
writer struct{ *storage.Writer } |
| 37 |
copier struct{ *storage.Copier } |
| 38 |
composer struct{ *storage.Composer } |
| 39 |
aclHandle struct{ *storage.ACLHandle } |
| 40 |
) |
| 41 |
|
| 42 |
func (client) embedToIncludeNewMethods() {} |
| 43 |
func (bucketHandle) embedToIncludeNewMethods() {} |
| 44 |
func (objectHandle) embedToIncludeNewMethods() {} |
| 45 |
func (bucketIterator) embedToIncludeNewMethods() {} |
| 46 |
func (objectIterator) embedToIncludeNewMethods() {} |
| 47 |
func (writer) embedToIncludeNewMethods() {} |
| 48 |
func (reader) embedToIncludeNewMethods() {} |
| 49 |
func (copier) embedToIncludeNewMethods() {} |
| 50 |
func (composer) embedToIncludeNewMethods() {} |
| 51 |
func (aclHandle) embedToIncludeNewMethods() {} |
| 52 |
|
| 53 |
func (c client) Bucket(name string) BucketHandle { |
| 54 |
return bucketHandle{c.Client.Bucket(name)} |
| 55 |
} |
| 56 |
|
| 57 |
func (c client) Buckets(ctx context.Context, projectID string) BucketIterator { |
| 58 |
return bucketIterator{c.Client.Buckets(ctx, projectID)} |
| 59 |
} |
| 60 |
|
| 61 |
func (b bucketHandle) Object(name string) ObjectHandle { |
| 62 |
return objectHandle{b.BucketHandle.Object(name)} |
| 63 |
} |
| 64 |
|
| 65 |
func (b bucketHandle) If(conds storage.BucketConditions) BucketHandle { |
| 66 |
return bucketHandle{b.BucketHandle.If(conds)} |
| 67 |
} |
| 68 |
|
| 69 |
func (b bucketHandle) Objects(ctx context.Context, q *storage.Query) ObjectIterator { |
| 70 |
return objectIterator{b.BucketHandle.Objects(ctx, q)} |
| 71 |
} |
| 72 |
|
| 73 |
func (b bucketHandle) DefaultObjectACL() ACLHandle { |
| 74 |
return aclHandle{b.BucketHandle.DefaultObjectACL()} |
| 75 |
} |
| 76 |
|
| 77 |
func (b bucketHandle) ACL() ACLHandle { |
| 78 |
return aclHandle{b.BucketHandle.ACL()} |
| 79 |
} |
| 80 |
|
| 81 |
func (b bucketHandle) UserProject(projectID string) BucketHandle { |
| 82 |
return bucketHandle{b.BucketHandle.UserProject(projectID)} |
| 83 |
} |
| 84 |
|
| 85 |
func (bi bucketIterator) SetPrefix(s string) { |
| 86 |
bi.BucketIterator.Prefix = s |
| 87 |
} |
| 88 |
|
| 89 |
func (o objectHandle) ACL() ACLHandle { |
| 90 |
return aclHandle{o.ObjectHandle.ACL()} |
| 91 |
} |
| 92 |
|
| 93 |
func (o objectHandle) Generation(gen int64) ObjectHandle { |
| 94 |
return objectHandle{o.ObjectHandle.Generation(gen)} |
| 95 |
} |
| 96 |
|
| 97 |
func (o objectHandle) If(conds storage.Conditions) ObjectHandle { |
| 98 |
return objectHandle{o.ObjectHandle.If(conds)} |
| 99 |
} |
| 100 |
|
| 101 |
func (o objectHandle) Key(encryptionKey []byte) ObjectHandle { |
| 102 |
return objectHandle{o.ObjectHandle.Key(encryptionKey)} |
| 103 |
} |
| 104 |
|
| 105 |
func (o objectHandle) ReadCompressed(compressed bool) ObjectHandle { |
| 106 |
return objectHandle{o.ObjectHandle.ReadCompressed(compressed)} |
| 107 |
} |
| 108 |
|
| 109 |
func (o objectHandle) NewReader(ctx context.Context) (Reader, error) { |
| 110 |
r, err := o.ObjectHandle.NewReader(ctx) |
| 111 |
if err != nil { |
| 112 |
return nil, err |
| 113 |
} |
| 114 |
return reader{r}, nil |
| 115 |
} |
| 116 |
|
| 117 |
func (o objectHandle) NewRangeReader(ctx context.Context, offset, length int64) (Reader, error) { |
| 118 |
r, err := o.ObjectHandle.NewRangeReader(ctx, offset, length) |
| 119 |
if err != nil { |
| 120 |
return nil, err |
| 121 |
} |
| 122 |
return reader{r}, nil |
| 123 |
} |
| 124 |
|
| 125 |
func (o objectHandle) NewWriter(ctx context.Context) Writer { |
| 126 |
return writer{o.ObjectHandle.NewWriter(ctx)} |
| 127 |
} |
| 128 |
|
| 129 |
func (o objectHandle) CopierFrom(src ObjectHandle) Copier { |
| 130 |
return copier{o.ObjectHandle.CopierFrom(src.(objectHandle).ObjectHandle)} |
| 131 |
} |
| 132 |
|
| 133 |
func (o objectHandle) ComposerFrom(srcs ...ObjectHandle) Composer { |
| 134 |
objs := make([]*storage.ObjectHandle, len(srcs)) |
| 135 |
for i, s := range srcs { |
| 136 |
objs[i] = s.(objectHandle).ObjectHandle |
| 137 |
} |
| 138 |
return composer{o.ObjectHandle.ComposerFrom(objs...)} |
| 139 |
} |
| 140 |
|
| 141 |
func (w writer) ObjectAttrs() *storage.ObjectAttrs { |
| 142 |
return &w.Writer.ObjectAttrs |
| 143 |
} |
| 144 |
|
| 145 |
func (w writer) SetChunkSize(s int) { |
| 146 |
w.ChunkSize = s |
| 147 |
} |
| 148 |
|
| 149 |
func (w writer) SetProgressFunc(f func(int64)) { |
| 150 |
w.ProgressFunc = f |
| 151 |
} |
| 152 |
|
| 153 |
func (w writer) SetCRC32C(c uint32) { |
| 154 |
w.CRC32C = c |
| 155 |
w.SendCRC32C = true |
| 156 |
} |
| 157 |
|
| 158 |
func (c copier) ObjectAttrs() *storage.ObjectAttrs { |
| 159 |
return &c.Copier.ObjectAttrs |
| 160 |
} |
| 161 |
|
| 162 |
func (c copier) SetRewriteToken(t string) { |
| 163 |
c.RewriteToken = t |
| 164 |
} |
| 165 |
|
| 166 |
func (c copier) SetProgressFunc(f func(copiedBytes, totalBytes uint64)) { |
| 167 |
c.ProgressFunc = f |
| 168 |
} |
| 169 |
|
| 170 |
func (c copier) SetDestinationKMSKeyName(k string) { |
| 171 |
c.DestinationKMSKeyName = k |
| 172 |
} |
| 173 |
|
| 174 |
func (c composer) ObjectAttrs() *storage.ObjectAttrs { |
| 175 |
return &c.Composer.ObjectAttrs |
| 176 |
} |