|
| 1 | +package swift |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "crypto/tls" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "path" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/aws/aws-sdk-go/aws" |
| 13 | + "github.com/gophercloud/gophercloud" |
| 14 | + "github.com/gophercloud/gophercloud/openstack" |
| 15 | + "github.com/gophercloud/gophercloud/openstack/objectstorage/v1/objects" |
| 16 | + "github.com/gophercloud/gophercloud/pagination" |
| 17 | + "github.com/larrabee/ratelimit" |
| 18 | + |
| 19 | + "github.com/larrabee/s3sync/storage" |
| 20 | +) |
| 21 | + |
| 22 | +// Storage configuration. |
| 23 | +type Storage struct { |
| 24 | + conn *gophercloud.ServiceClient |
| 25 | + bucket string |
| 26 | + prefix string |
| 27 | + ctx context.Context |
| 28 | + rlBucket ratelimit.Bucket |
| 29 | +} |
| 30 | + |
| 31 | +// NewStorage return new configured S3 storage. |
| 32 | +// |
| 33 | +// You should always create new storage with this constructor. |
| 34 | +func NewStorage(user, key, tenant, domain, authUrl string, bucketName, prefix string, skipSSLVerify bool) (*Storage, error) { |
| 35 | + st := &Storage{ |
| 36 | + ctx: context.TODO(), |
| 37 | + rlBucket: ratelimit.NewFakeBucket(), |
| 38 | + bucket: bucketName, |
| 39 | + prefix: prefix, |
| 40 | + } |
| 41 | + |
| 42 | + auth := gophercloud.AuthOptions{ |
| 43 | + IdentityEndpoint: authUrl, |
| 44 | + Username: user, |
| 45 | + Password: key, |
| 46 | + TenantName: tenant, |
| 47 | + DomainName: domain, |
| 48 | + } |
| 49 | + |
| 50 | + provider, err := openstack.AuthenticatedClient(auth) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + if skipSSLVerify { |
| 56 | + tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} |
| 57 | + provider.HTTPClient = http.Client{Transport: tr} |
| 58 | + } |
| 59 | + |
| 60 | + client, err := openstack.NewObjectStorageV1(provider, gophercloud.EndpointOpts{}) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + st.conn = client |
| 66 | + |
| 67 | + return st, nil |
| 68 | +} |
| 69 | + |
| 70 | +// WithContext add's context to storage. |
| 71 | +func (st *Storage) WithContext(ctx context.Context) { |
| 72 | + st.ctx = ctx |
| 73 | + st.conn.Context = ctx |
| 74 | +} |
| 75 | + |
| 76 | +// WithRateLimit set rate limit (bytes/sec) for storage. |
| 77 | +func (st *Storage) WithRateLimit(limit int) error { |
| 78 | + bucket, err := ratelimit.NewBucketWithRate(float64(limit), int64(limit)) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + st.rlBucket = bucket |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// List S3 bucket and send founded objects to chan. |
| 87 | +func (st *Storage) List(output chan<- *storage.Object) error { |
| 88 | + opts := &objects.ListOpts{Full: true, Prefix: st.prefix} |
| 89 | + pager := objects.List(st.conn, st.bucket, opts) |
| 90 | + |
| 91 | + err := pager.EachPage(func(page pagination.Page) (bool, error) { |
| 92 | + objectList, err := objects.ExtractInfo(page) |
| 93 | + if err != nil { |
| 94 | + return false, err |
| 95 | + } |
| 96 | + for _, n := range objectList { |
| 97 | + key := strings.Replace(n.Name, st.prefix, "", 1) |
| 98 | + key = strings.TrimLeft(key, "/") |
| 99 | + output <- &storage.Object{ |
| 100 | + Key: &key, |
| 101 | + ETag: &n.Hash, |
| 102 | + ContentType: &n.ContentType, |
| 103 | + Mtime: &n.LastModified, |
| 104 | + ContentLength: &n.Bytes, |
| 105 | + IsLatest: aws.Bool(true), |
| 106 | + } |
| 107 | + } |
| 108 | + return true, nil |
| 109 | + }) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + storage.Log.Debugf("Listing bucket finished") |
| 115 | + return nil |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | +// PutObject saves object to S3. |
| 120 | +// PutObject ignore VersionId, it always save object as latest version. |
| 121 | +func (st *Storage) PutObject(obj *storage.Object) error { |
| 122 | + opts := objects.CreateOpts{} |
| 123 | + if obj.ContentType != nil { |
| 124 | + opts.ContentType = *obj.ContentType |
| 125 | + } |
| 126 | + if obj.Content != nil { |
| 127 | + opts.Content = bytes.NewReader(*obj.Content) |
| 128 | + } |
| 129 | + if obj.ContentDisposition != nil { |
| 130 | + opts.ContentDisposition = *obj.ContentDisposition |
| 131 | + } |
| 132 | + if obj.ContentEncoding != nil { |
| 133 | + opts.ContentEncoding = *obj.ContentEncoding |
| 134 | + } |
| 135 | + if obj.CacheControl != nil { |
| 136 | + opts.CacheControl = *obj.CacheControl |
| 137 | + } |
| 138 | + |
| 139 | + res := objects.Create(st.conn, st.bucket, path.Join(st.prefix, *obj.Key), opts) |
| 140 | + if res.Err != nil { |
| 141 | + return res.Err |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +// GetObjectContent read object content and metadata from S3. |
| 148 | +func (st *Storage) GetObjectContent(obj *storage.Object) error { |
| 149 | + opts := objects.DownloadOpts{} |
| 150 | + |
| 151 | + // Download everything into a DownloadResult struct |
| 152 | + res := objects.Download(st.conn, st.bucket, path.Join(st.prefix, *obj.Key), opts) |
| 153 | + if res.Err != nil { |
| 154 | + return res.Err |
| 155 | + } |
| 156 | + defer res.Body.Close() |
| 157 | + |
| 158 | + header, err := res.Extract() |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + buf := bytes.NewBuffer(make([]byte, 0, header.ContentLength)) |
| 164 | + if _, err := io.Copy(ratelimit.NewWriter(buf, st.rlBucket), res.Body); err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + |
| 168 | + data := buf.Bytes() |
| 169 | + obj.Content = &data |
| 170 | + obj.ContentType = &header.ContentType |
| 171 | + obj.ContentLength = &header.ContentLength |
| 172 | + obj.ContentDisposition = &header.ContentDisposition |
| 173 | + obj.ContentEncoding = &header.ContentEncoding |
| 174 | + obj.ETag = storage.StrongEtag(&header.ETag) |
| 175 | + obj.Mtime = &header.LastModified |
| 176 | + |
| 177 | + return nil |
| 178 | +} |
| 179 | + |
| 180 | +// GetObjectACL read object ACL from S3. |
| 181 | +func (st *Storage) GetObjectACL(obj *storage.Object) error { |
| 182 | + return nil |
| 183 | +} |
| 184 | + |
| 185 | +// GetObjectMeta update object metadata from S3. |
| 186 | +func (st *Storage) GetObjectMeta(obj *storage.Object) error { |
| 187 | + opts := objects.GetOpts{} |
| 188 | + |
| 189 | + res := objects.Get(st.conn, st.bucket, path.Join(st.prefix, *obj.Key), opts) |
| 190 | + if res.Err != nil { |
| 191 | + return res.Err |
| 192 | + } |
| 193 | + |
| 194 | + header, err := res.Extract() |
| 195 | + if err != nil { |
| 196 | + return err |
| 197 | + } |
| 198 | + |
| 199 | + obj.ContentType = &header.ContentType |
| 200 | + obj.ContentLength = &header.ContentLength |
| 201 | + obj.ContentDisposition = &header.ContentDisposition |
| 202 | + obj.ContentEncoding = &header.ContentEncoding |
| 203 | + obj.ETag = storage.StrongEtag(&header.ETag) |
| 204 | + obj.Mtime = &header.LastModified |
| 205 | + |
| 206 | + return nil |
| 207 | +} |
| 208 | + |
| 209 | +// DeleteObject remove object from S3. |
| 210 | +func (st *Storage) DeleteObject(obj *storage.Object) error { |
| 211 | + opts := objects.DeleteOpts{} |
| 212 | + |
| 213 | + res := objects.Delete(st.conn, st.bucket, path.Join(st.prefix, *obj.Key), opts) |
| 214 | + if res.Err != nil { |
| 215 | + return res.Err |
| 216 | + } |
| 217 | + |
| 218 | + return nil |
| 219 | +} |
0 commit comments