2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-05-09 00:49:54 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
captplanet (#159)
* captplanet
I kind of went overboard this weekend.
The major goal of this changeset is to provide an environment
for local development where all of the various services can
be easily run together. Developing on Storj v3 should be as
easy as running a setup command and a run command!
To do this, this changeset introduces a new tool called
captplanet, which combines the powers of the Overlay Cache,
the PointerDB, the PieceStore, Kademlia, the Minio Gateway,
etc.
Running 40 farmers and a heavy client inside the same process
forced a rethinking of the "services" that we had. To
avoid confusion by reusing prior terms, this changeset
introduces two new types: Providers and Responsibilities.
I wanted to avoid as many merge conflicts as possible, so
I left the existing Services and code for now, but if people
like this route we can clean up the duplication.
A Responsibility is a collection of gRPC methods and
corresponding state. The following systems are examples of
Responsibilities:
* Kademlia
* OverlayCache
* PointerDB
* StatDB
* PieceStore
* etc.
A Provider is a collection of Responsibilities that
share an Identity, such as:
* The heavy client
* The farmer
* The gateway
An Identity is a public/private key pair, a node id, etc.
Farmers all need different Identities, so captplanet
needs to support running multiple concurrent Providers
with different Identities.
Each Responsibility and Provider should allow for configuration
of multiple copies on its own so creating Responsibilities and
Providers use a new workflow.
To make a Responsibility, one should create a "config"
struct, such as:
```
type Config struct {
RepairThreshold int `help:"If redundancy falls below this number of
pieces, repair is triggered" default:"30"`
SuccessThreshold int `help:"If redundancy is above this number then
no additional uploads are needed" default:"40"`
}
```
To use "config" structs, this changeset introduces another
new library called 'cfgstruct', which allows for the configuration
of arbitrary structs through flagsets, and thus through cobra and
viper.
cfgstruct relies on Go's "struct tags" feature to document
help information and default values. Config structs can be
configured via cfgstruct.Bind for binding the struct to a flagset.
Because this configuration system makes setup and configuration
easier *in general*, additional commands are provided that allow
for easy standup of separate Providers. Please make sure to
check out:
* cmd/captplanet/farmer/main.go (a new farmer binary)
* cmd/captplanet/hc/main.go (a new heavy client binary)
* cmd/captplanet/gw/main.go (a new minio gateway binary)
Usage:
```
$ go install -v storj.io/storj/cmd/captplanet
$ captplanet setup
$ captplanet run
```
Configuration is placed by default in `~/.storj/capt/`
Other changes:
* introduces new config structs for currently existing
Responsibilities that conform to the new Responsibility
interface. Please see the `pkg/*/config.go` files for
examples.
* integrates the PointerDB API key with other global
configuration via flags, instead of through environment
variables through viper like it's been doing. (ultimately
this should also change to use the PointerDB config
struct but this is an okay shortterm solution).
* changes the Overlay cache to use a URL for database
configuration instead of separate redis and bolt config
settings.
* stubs out some peer identity skeleton code (but not the
meat).
* Fixes the SegmentStore to use the overlay client and
pointerdb clients instead of gRPC client code directly
* Leaves a very clear spot where we need to tie the object to
stream to segment store together. There's sort of a "golden
spike" opportunity to connect all the train tracks together
at the bottom of pkg/miniogw/config.go, labeled with a
bunch of TODOs.
Future stuff:
* I now prefer this design over the original
pkg/process.Service thing I had been pushing before (sorry!)
* The experience of trying to have multiple farmers
configurable concurrently led me to prefer config structs
over global flags (I finally came around) or using viper
directly. I think global flags are okay sometimes but in
general going forward we should try and get all relevant
config into config structs.
* If you all like this direction, I think we can go delete my
old Service interfaces and a bunch of flags and clean up a
bunch of stuff.
* If you don't like this direction, it's no sweat at all, and
despite how much code there is here I'm not very tied to any
of this! Considering a lot of this was written between midnight
and 6 am, it might not be any good!
* bind tests
2018-07-24 17:08:28 +01:00
|
|
|
package miniogw
|
2018-05-09 00:49:54 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-12-07 18:31:29 +00:00
|
|
|
"encoding/hex"
|
2018-05-09 00:49:54 +01:00
|
|
|
"io"
|
2018-10-25 21:28:16 +01:00
|
|
|
"strings"
|
2018-05-09 00:49:54 +01:00
|
|
|
|
2018-06-01 13:51:13 +01:00
|
|
|
minio "github.com/minio/minio/cmd"
|
2018-05-09 00:49:54 +01:00
|
|
|
"github.com/minio/minio/pkg/auth"
|
|
|
|
"github.com/minio/minio/pkg/hash"
|
2018-07-12 18:29:02 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-06-01 13:51:13 +01:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
"storj.io/storj/lib/uplink"
|
2018-10-25 21:28:16 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/memory"
|
2019-12-18 13:35:28 +00:00
|
|
|
"storj.io/storj/uplink/metainfo/kvmetainfo"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/uplink/storage/streams"
|
|
|
|
"storj.io/storj/uplink/stream"
|
2018-06-01 13:51:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-07-12 18:29:02 +01:00
|
|
|
mon = monkit.Package()
|
2018-12-07 18:31:29 +00:00
|
|
|
|
|
|
|
// Error is the errs class of standard End User Client errors
|
2018-07-12 18:29:02 +01:00
|
|
|
Error = errs.Class("Storj Gateway error")
|
2018-05-09 00:49:54 +01:00
|
|
|
)
|
|
|
|
|
captplanet (#159)
* captplanet
I kind of went overboard this weekend.
The major goal of this changeset is to provide an environment
for local development where all of the various services can
be easily run together. Developing on Storj v3 should be as
easy as running a setup command and a run command!
To do this, this changeset introduces a new tool called
captplanet, which combines the powers of the Overlay Cache,
the PointerDB, the PieceStore, Kademlia, the Minio Gateway,
etc.
Running 40 farmers and a heavy client inside the same process
forced a rethinking of the "services" that we had. To
avoid confusion by reusing prior terms, this changeset
introduces two new types: Providers and Responsibilities.
I wanted to avoid as many merge conflicts as possible, so
I left the existing Services and code for now, but if people
like this route we can clean up the duplication.
A Responsibility is a collection of gRPC methods and
corresponding state. The following systems are examples of
Responsibilities:
* Kademlia
* OverlayCache
* PointerDB
* StatDB
* PieceStore
* etc.
A Provider is a collection of Responsibilities that
share an Identity, such as:
* The heavy client
* The farmer
* The gateway
An Identity is a public/private key pair, a node id, etc.
Farmers all need different Identities, so captplanet
needs to support running multiple concurrent Providers
with different Identities.
Each Responsibility and Provider should allow for configuration
of multiple copies on its own so creating Responsibilities and
Providers use a new workflow.
To make a Responsibility, one should create a "config"
struct, such as:
```
type Config struct {
RepairThreshold int `help:"If redundancy falls below this number of
pieces, repair is triggered" default:"30"`
SuccessThreshold int `help:"If redundancy is above this number then
no additional uploads are needed" default:"40"`
}
```
To use "config" structs, this changeset introduces another
new library called 'cfgstruct', which allows for the configuration
of arbitrary structs through flagsets, and thus through cobra and
viper.
cfgstruct relies on Go's "struct tags" feature to document
help information and default values. Config structs can be
configured via cfgstruct.Bind for binding the struct to a flagset.
Because this configuration system makes setup and configuration
easier *in general*, additional commands are provided that allow
for easy standup of separate Providers. Please make sure to
check out:
* cmd/captplanet/farmer/main.go (a new farmer binary)
* cmd/captplanet/hc/main.go (a new heavy client binary)
* cmd/captplanet/gw/main.go (a new minio gateway binary)
Usage:
```
$ go install -v storj.io/storj/cmd/captplanet
$ captplanet setup
$ captplanet run
```
Configuration is placed by default in `~/.storj/capt/`
Other changes:
* introduces new config structs for currently existing
Responsibilities that conform to the new Responsibility
interface. Please see the `pkg/*/config.go` files for
examples.
* integrates the PointerDB API key with other global
configuration via flags, instead of through environment
variables through viper like it's been doing. (ultimately
this should also change to use the PointerDB config
struct but this is an okay shortterm solution).
* changes the Overlay cache to use a URL for database
configuration instead of separate redis and bolt config
settings.
* stubs out some peer identity skeleton code (but not the
meat).
* Fixes the SegmentStore to use the overlay client and
pointerdb clients instead of gRPC client code directly
* Leaves a very clear spot where we need to tie the object to
stream to segment store together. There's sort of a "golden
spike" opportunity to connect all the train tracks together
at the bottom of pkg/miniogw/config.go, labeled with a
bunch of TODOs.
Future stuff:
* I now prefer this design over the original
pkg/process.Service thing I had been pushing before (sorry!)
* The experience of trying to have multiple farmers
configurable concurrently led me to prefer config structs
over global flags (I finally came around) or using viper
directly. I think global flags are okay sometimes but in
general going forward we should try and get all relevant
config into config structs.
* If you all like this direction, I think we can go delete my
old Service interfaces and a bunch of flags and clean up a
bunch of stuff.
* If you don't like this direction, it's no sweat at all, and
despite how much code there is here I'm not very tied to any
of this! Considering a lot of this was written between midnight
and 6 am, it might not be any good!
* bind tests
2018-07-24 17:08:28 +01:00
|
|
|
// NewStorjGateway creates a *Storj object from an existing ObjectStore
|
2019-06-28 06:18:24 +01:00
|
|
|
func NewStorjGateway(project *uplink.Project, access *uplink.EncryptionAccess, pathCipher storj.CipherSuite, encryption storj.EncryptionParameters, redundancy storj.RedundancyScheme, segmentSize memory.Size) *Gateway {
|
2018-12-07 18:31:29 +00:00
|
|
|
return &Gateway{
|
2019-04-15 21:13:02 +01:00
|
|
|
project: project,
|
2019-06-28 06:18:24 +01:00
|
|
|
access: access,
|
2019-04-15 21:13:02 +01:00
|
|
|
pathCipher: pathCipher,
|
|
|
|
encryption: encryption,
|
|
|
|
redundancy: redundancy,
|
|
|
|
segmentSize: segmentSize,
|
|
|
|
multipart: NewMultipartUploads(),
|
2018-12-07 18:31:29 +00:00
|
|
|
}
|
2018-05-09 00:49:54 +01:00
|
|
|
}
|
|
|
|
|
2018-12-07 18:31:29 +00:00
|
|
|
// Gateway is the implementation of a minio cmd.Gateway
|
|
|
|
type Gateway struct {
|
2019-04-15 21:13:02 +01:00
|
|
|
project *uplink.Project
|
2019-06-28 06:18:24 +01:00
|
|
|
access *uplink.EncryptionAccess
|
2019-04-15 21:13:02 +01:00
|
|
|
pathCipher storj.CipherSuite
|
|
|
|
encryption storj.EncryptionParameters
|
|
|
|
redundancy storj.RedundancyScheme
|
|
|
|
segmentSize memory.Size
|
|
|
|
multipart *MultipartUploads
|
2018-06-01 13:51:13 +01:00
|
|
|
}
|
2018-05-09 00:49:54 +01:00
|
|
|
|
|
|
|
// Name implements cmd.Gateway
|
2018-12-07 18:31:29 +00:00
|
|
|
func (gateway *Gateway) Name() string {
|
2018-05-09 00:49:54 +01:00
|
|
|
return "storj"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGatewayLayer implements cmd.Gateway
|
2018-12-07 18:31:29 +00:00
|
|
|
func (gateway *Gateway) NewGatewayLayer(creds auth.Credentials) (minio.ObjectLayer, error) {
|
|
|
|
return &gatewayLayer{gateway: gateway}, nil
|
2018-05-09 00:49:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Production implements cmd.Gateway
|
2018-12-07 18:31:29 +00:00
|
|
|
func (gateway *Gateway) Production() bool {
|
2018-05-09 00:49:54 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-12-07 18:31:29 +00:00
|
|
|
type gatewayLayer struct {
|
2018-06-01 13:51:13 +01:00
|
|
|
minio.GatewayUnsupported
|
2018-12-07 18:31:29 +00:00
|
|
|
gateway *Gateway
|
2018-05-09 00:49:54 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
func (layer *gatewayLayer) DeleteBucket(ctx context.Context, bucketName string) (err error) {
|
2018-07-12 18:29:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
empty, err := layer.bucketEmpty(ctx, bucketName)
|
2018-08-16 15:32:28 +01:00
|
|
|
if err != nil {
|
2019-04-15 21:13:02 +01:00
|
|
|
return convertError(err, bucketName, "")
|
2018-08-16 15:32:28 +01:00
|
|
|
}
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
if !empty {
|
|
|
|
return minio.BucketNotEmpty{Bucket: bucketName}
|
2018-08-16 15:32:28 +01:00
|
|
|
}
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
err = layer.gateway.project.DeleteBucket(ctx, bucketName)
|
|
|
|
|
|
|
|
return convertError(err, bucketName, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (layer *gatewayLayer) bucketEmpty(ctx context.Context, bucketName string) (empty bool, err error) {
|
2019-06-27 18:36:51 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
bucket, err := layer.gateway.project.OpenBucket(ctx, bucketName, layer.gateway.access)
|
2019-04-15 21:13:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, convertError(err, bucketName, "")
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, bucket.Close()) }()
|
|
|
|
|
|
|
|
list, err := bucket.ListObjects(ctx, &storj.ListOptions{Direction: storj.After, Recursive: true, Limit: 1})
|
|
|
|
if err != nil {
|
|
|
|
return false, convertError(err, bucketName, "")
|
|
|
|
}
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
return len(list.Items) == 0, nil
|
2018-05-09 00:49:54 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
func (layer *gatewayLayer) DeleteObject(ctx context.Context, bucketName, objectPath string) (err error) {
|
2018-07-12 18:29:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
bucket, err := layer.gateway.project.OpenBucket(ctx, bucketName, layer.gateway.access)
|
2019-04-15 21:13:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return convertError(err, bucketName, "")
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, bucket.Close()) }()
|
|
|
|
|
|
|
|
err = bucket.DeleteObject(ctx, objectPath)
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
return convertError(err, bucketName, objectPath)
|
2018-05-09 00:49:54 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
func (layer *gatewayLayer) GetBucketInfo(ctx context.Context, bucketName string) (bucketInfo minio.BucketInfo, err error) {
|
2018-07-12 18:29:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
bucket, _, err := layer.gateway.project.GetBucketInfo(ctx, bucketName)
|
2018-08-23 01:19:46 +01:00
|
|
|
|
2018-08-16 15:32:28 +01:00
|
|
|
if err != nil {
|
2019-04-15 21:13:02 +01:00
|
|
|
return minio.BucketInfo{}, convertError(err, bucketName, "")
|
2018-08-16 15:32:28 +01:00
|
|
|
}
|
2018-08-23 01:19:46 +01:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
return minio.BucketInfo{Name: bucket.Name, Created: bucket.Created}, nil
|
2018-05-09 00:49:54 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
func (layer *gatewayLayer) GetObject(ctx context.Context, bucketName, objectPath string, startOffset int64, length int64, writer io.Writer, etag string) (err error) {
|
2018-07-12 18:29:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
bucket, err := layer.gateway.project.OpenBucket(ctx, bucketName, layer.gateway.access)
|
2018-08-16 15:32:28 +01:00
|
|
|
if err != nil {
|
2019-04-15 21:13:02 +01:00
|
|
|
return convertError(err, bucketName, "")
|
2018-08-16 15:32:28 +01:00
|
|
|
}
|
2019-04-15 21:13:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, bucket.Close()) }()
|
2018-08-29 16:00:05 +01:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
object, err := bucket.OpenObject(ctx, objectPath)
|
|
|
|
if err != nil {
|
|
|
|
return convertError(err, bucketName, objectPath)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, object.Close()) }()
|
|
|
|
|
|
|
|
if startOffset < 0 || length < -1 || startOffset+length > object.Meta.Size {
|
2018-12-07 18:31:29 +00:00
|
|
|
return minio.InvalidRange{
|
|
|
|
OffsetBegin: startOffset,
|
|
|
|
OffsetEnd: startOffset + length,
|
2019-04-15 21:13:02 +01:00
|
|
|
ResourceSize: object.Meta.Size,
|
2018-12-07 18:31:29 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-29 16:00:05 +01:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
reader, err := object.DownloadRange(ctx, startOffset, length)
|
2018-07-12 18:29:02 +01:00
|
|
|
if err != nil {
|
2019-04-15 21:13:02 +01:00
|
|
|
return convertError(err, bucketName, objectPath)
|
2018-07-12 18:29:02 +01:00
|
|
|
}
|
2019-04-15 21:13:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, reader.Close()) }()
|
2018-08-29 16:00:05 +01:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
_, err = io.Copy(writer, reader)
|
2018-08-29 16:00:05 +01:00
|
|
|
|
2018-07-12 18:29:02 +01:00
|
|
|
return err
|
2018-05-09 00:49:54 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
func (layer *gatewayLayer) GetObjectInfo(ctx context.Context, bucketName, objectPath string) (objInfo minio.ObjectInfo, err error) {
|
2018-07-12 18:29:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
bucket, err := layer.gateway.project.OpenBucket(ctx, bucketName, layer.gateway.access)
|
2018-08-16 15:32:28 +01:00
|
|
|
if err != nil {
|
2019-04-15 21:13:02 +01:00
|
|
|
return minio.ObjectInfo{}, convertError(err, bucketName, "")
|
2018-07-12 18:29:02 +01:00
|
|
|
}
|
2019-04-15 21:13:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, bucket.Close()) }()
|
|
|
|
|
|
|
|
object, err := bucket.OpenObject(ctx, objectPath)
|
|
|
|
if err != nil {
|
|
|
|
return minio.ObjectInfo{}, convertError(err, bucketName, objectPath)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, object.Close()) }()
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2018-07-12 18:29:02 +01:00
|
|
|
return minio.ObjectInfo{
|
2019-04-15 21:13:02 +01:00
|
|
|
Name: object.Meta.Path,
|
|
|
|
Bucket: object.Meta.Bucket,
|
|
|
|
ModTime: object.Meta.Modified,
|
|
|
|
Size: object.Meta.Size,
|
|
|
|
ETag: hex.EncodeToString(object.Meta.Checksum),
|
|
|
|
ContentType: object.Meta.ContentType,
|
|
|
|
UserDefined: object.Meta.Metadata,
|
2018-07-12 18:29:02 +01:00
|
|
|
}, err
|
2018-05-09 00:49:54 +01:00
|
|
|
}
|
|
|
|
|
2018-12-07 18:31:29 +00:00
|
|
|
func (layer *gatewayLayer) ListBuckets(ctx context.Context) (bucketItems []minio.BucketInfo, err error) {
|
2018-07-12 18:29:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2018-08-16 15:32:28 +01:00
|
|
|
startAfter := ""
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2019-07-12 13:57:02 +01:00
|
|
|
listOpts := storj.BucketListOptions{
|
|
|
|
Direction: storj.Forward,
|
|
|
|
Cursor: startAfter,
|
|
|
|
}
|
2018-08-16 15:32:28 +01:00
|
|
|
for {
|
2019-07-12 13:57:02 +01:00
|
|
|
list, err := layer.gateway.project.ListBuckets(ctx, &listOpts)
|
2018-08-16 15:32:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-07 18:31:29 +00:00
|
|
|
|
|
|
|
for _, item := range list.Items {
|
|
|
|
bucketItems = append(bucketItems, minio.BucketInfo{Name: item.Name, Created: item.Created})
|
|
|
|
}
|
|
|
|
|
|
|
|
if !list.More {
|
2018-08-16 15:32:28 +01:00
|
|
|
break
|
|
|
|
}
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2019-07-12 13:57:02 +01:00
|
|
|
listOpts = listOpts.NextPage(list)
|
2018-08-16 15:32:28 +01:00
|
|
|
}
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2018-08-16 15:32:28 +01:00
|
|
|
return bucketItems, err
|
2018-05-09 00:49:54 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
func (layer *gatewayLayer) ListObjects(ctx context.Context, bucketName, prefix, marker, delimiter string, maxKeys int) (result minio.ListObjectsInfo, err error) {
|
2018-07-12 18:29:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-09-08 18:10:58 +01:00
|
|
|
|
|
|
|
if delimiter != "" && delimiter != "/" {
|
2018-12-07 18:31:29 +00:00
|
|
|
return minio.ListObjectsInfo{}, minio.UnsupportedDelimiter{Delimiter: delimiter}
|
2018-09-08 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
bucket, err := layer.gateway.project.OpenBucket(ctx, bucketName, layer.gateway.access)
|
2019-04-15 21:13:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return minio.ListObjectsInfo{}, convertError(err, bucketName, "")
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, bucket.Close()) }()
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
startAfter := marker
|
2018-09-09 22:51:33 +01:00
|
|
|
recursive := delimiter == ""
|
|
|
|
|
2018-09-08 18:10:58 +01:00
|
|
|
var objects []minio.ObjectInfo
|
|
|
|
var prefixes []string
|
2018-12-07 18:31:29 +00:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
list, err := bucket.ListObjects(ctx, &storj.ListOptions{
|
2018-12-07 18:31:29 +00:00
|
|
|
Direction: storj.After,
|
|
|
|
Cursor: startAfter,
|
|
|
|
Prefix: prefix,
|
|
|
|
Recursive: recursive,
|
|
|
|
Limit: maxKeys,
|
|
|
|
})
|
2018-07-27 13:34:40 +01:00
|
|
|
if err != nil {
|
2019-04-15 21:13:02 +01:00
|
|
|
return result, convertError(err, bucketName, "")
|
2018-07-27 13:34:40 +01:00
|
|
|
}
|
2018-12-07 18:31:29 +00:00
|
|
|
|
|
|
|
if len(list.Items) > 0 {
|
|
|
|
for _, item := range list.Items {
|
2018-09-09 22:51:33 +01:00
|
|
|
path := item.Path
|
2018-10-25 21:28:16 +01:00
|
|
|
if recursive && prefix != "" {
|
|
|
|
path = storj.JoinPaths(strings.TrimSuffix(prefix, "/"), path)
|
2018-09-09 22:51:33 +01:00
|
|
|
}
|
2018-09-08 18:10:58 +01:00
|
|
|
if item.IsPrefix {
|
2018-10-25 21:28:16 +01:00
|
|
|
prefixes = append(prefixes, path)
|
2018-09-08 18:10:58 +01:00
|
|
|
continue
|
2018-07-27 13:34:40 +01:00
|
|
|
}
|
2018-09-08 18:10:58 +01:00
|
|
|
objects = append(objects, minio.ObjectInfo{
|
2018-10-25 21:28:16 +01:00
|
|
|
Name: path,
|
2019-04-15 21:13:02 +01:00
|
|
|
Bucket: item.Bucket.Name,
|
2018-12-07 18:31:29 +00:00
|
|
|
ModTime: item.Modified,
|
|
|
|
Size: item.Size,
|
|
|
|
ETag: hex.EncodeToString(item.Checksum),
|
|
|
|
ContentType: item.ContentType,
|
|
|
|
UserDefined: item.Metadata,
|
2018-09-08 18:10:58 +01:00
|
|
|
})
|
2018-07-27 13:34:40 +01:00
|
|
|
}
|
2018-12-07 18:31:29 +00:00
|
|
|
startAfter = list.Items[len(list.Items)-1].Path
|
2018-07-27 13:34:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
result = minio.ListObjectsInfo{
|
2018-12-07 18:31:29 +00:00
|
|
|
IsTruncated: list.More,
|
2018-09-08 18:10:58 +01:00
|
|
|
Objects: objects,
|
|
|
|
Prefixes: prefixes,
|
2018-07-27 13:34:40 +01:00
|
|
|
}
|
2018-12-07 18:31:29 +00:00
|
|
|
if list.More {
|
2018-10-25 21:28:16 +01:00
|
|
|
result.NextMarker = startAfter
|
2018-07-27 13:34:40 +01:00
|
|
|
}
|
|
|
|
|
2018-07-12 18:29:02 +01:00
|
|
|
return result, err
|
2018-05-09 00:49:54 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 16:26:53 +01:00
|
|
|
// ListObjectsV2 - Not implemented stub
|
2019-04-15 21:13:02 +01:00
|
|
|
func (layer *gatewayLayer) ListObjectsV2(ctx context.Context, bucketName, prefix, continuationToken, delimiter string, maxKeys int, fetchOwner bool, startAfter string) (result minio.ListObjectsV2Info, err error) {
|
2018-09-18 16:26:53 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if delimiter != "" && delimiter != "/" {
|
2018-12-07 18:31:29 +00:00
|
|
|
return minio.ListObjectsV2Info{ContinuationToken: continuationToken}, minio.UnsupportedDelimiter{Delimiter: delimiter}
|
2018-09-18 16:26:53 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
bucket, err := layer.gateway.project.OpenBucket(ctx, bucketName, layer.gateway.access)
|
2019-04-15 21:13:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return minio.ListObjectsV2Info{}, convertError(err, bucketName, "")
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, bucket.Close()) }()
|
|
|
|
|
2018-09-18 16:26:53 +01:00
|
|
|
recursive := delimiter == ""
|
|
|
|
var nextContinuationToken string
|
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
var startAfterPath storj.Path
|
2018-09-18 16:26:53 +01:00
|
|
|
if continuationToken != "" {
|
2018-10-25 21:28:16 +01:00
|
|
|
startAfterPath = continuationToken
|
2018-09-18 16:26:53 +01:00
|
|
|
}
|
2018-10-25 21:28:16 +01:00
|
|
|
if startAfterPath == "" && startAfter != "" {
|
|
|
|
startAfterPath = startAfter
|
2018-09-18 16:26:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var objects []minio.ObjectInfo
|
|
|
|
var prefixes []string
|
2018-12-07 18:31:29 +00:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
list, err := bucket.ListObjects(ctx, &storj.ListOptions{
|
2018-12-07 18:31:29 +00:00
|
|
|
Direction: storj.After,
|
|
|
|
Cursor: startAfterPath,
|
|
|
|
Prefix: prefix,
|
|
|
|
Recursive: recursive,
|
|
|
|
Limit: maxKeys,
|
|
|
|
})
|
2018-09-18 16:26:53 +01:00
|
|
|
if err != nil {
|
2019-04-15 21:13:02 +01:00
|
|
|
return minio.ListObjectsV2Info{ContinuationToken: continuationToken}, convertError(err, bucketName, "")
|
2018-09-18 16:26:53 +01:00
|
|
|
}
|
|
|
|
|
2018-12-07 18:31:29 +00:00
|
|
|
if len(list.Items) > 0 {
|
|
|
|
for _, item := range list.Items {
|
2018-09-18 16:26:53 +01:00
|
|
|
path := item.Path
|
2018-10-25 21:28:16 +01:00
|
|
|
if recursive && prefix != "" {
|
|
|
|
path = storj.JoinPaths(strings.TrimSuffix(prefix, "/"), path)
|
2018-09-18 16:26:53 +01:00
|
|
|
}
|
|
|
|
if item.IsPrefix {
|
2018-10-25 21:28:16 +01:00
|
|
|
prefixes = append(prefixes, path)
|
2018-09-18 16:26:53 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
objects = append(objects, minio.ObjectInfo{
|
2018-10-25 21:28:16 +01:00
|
|
|
Name: path,
|
2019-04-15 21:13:02 +01:00
|
|
|
Bucket: item.Bucket.Name,
|
2018-12-07 18:31:29 +00:00
|
|
|
ModTime: item.Modified,
|
|
|
|
Size: item.Size,
|
|
|
|
ETag: hex.EncodeToString(item.Checksum),
|
|
|
|
ContentType: item.ContentType,
|
|
|
|
UserDefined: item.Metadata,
|
2018-09-18 16:26:53 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-07 18:31:29 +00:00
|
|
|
nextContinuationToken = list.Items[len(list.Items)-1].Path + "\x00"
|
2018-09-18 16:26:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
result = minio.ListObjectsV2Info{
|
2018-12-07 18:31:29 +00:00
|
|
|
IsTruncated: list.More,
|
2018-09-18 16:26:53 +01:00
|
|
|
ContinuationToken: continuationToken,
|
|
|
|
Objects: objects,
|
|
|
|
Prefixes: prefixes,
|
|
|
|
}
|
2018-12-07 18:31:29 +00:00
|
|
|
if list.More {
|
2018-09-18 16:26:53 +01:00
|
|
|
result.NextContinuationToken = nextContinuationToken
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
func (layer *gatewayLayer) MakeBucketWithLocation(ctx context.Context, bucketName string, location string) (err error) {
|
2018-07-12 18:29:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-08-16 15:32:28 +01:00
|
|
|
// TODO: This current strategy of calling bs.Get
|
|
|
|
// to check if a bucket exists, then calling bs.Put
|
|
|
|
// if not, can create a race condition if two people
|
|
|
|
// call MakeBucketWithLocation at the same time and
|
|
|
|
// therefore try to Put a bucket at the same time.
|
|
|
|
// The reason for the Get call to check if the
|
|
|
|
// bucket already exists is to match S3 CLI behavior.
|
2019-04-15 21:13:02 +01:00
|
|
|
_, _, err = layer.gateway.project.GetBucketInfo(ctx, bucketName)
|
2018-08-16 15:32:28 +01:00
|
|
|
if err == nil {
|
2019-04-15 21:13:02 +01:00
|
|
|
return minio.BucketAlreadyExists{Bucket: bucketName}
|
2018-08-16 15:32:28 +01:00
|
|
|
}
|
2018-12-07 18:31:29 +00:00
|
|
|
|
2018-11-21 11:17:28 +00:00
|
|
|
if !storj.ErrBucketNotFound.Has(err) {
|
2019-04-15 21:13:02 +01:00
|
|
|
return convertError(err, bucketName, "")
|
2018-08-16 15:32:28 +01:00
|
|
|
}
|
2018-12-07 18:31:29 +00:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
cfg := uplink.BucketConfig{
|
|
|
|
PathCipher: layer.gateway.pathCipher,
|
|
|
|
EncryptionParameters: layer.gateway.encryption,
|
|
|
|
}
|
|
|
|
cfg.Volatile.RedundancyScheme = layer.gateway.redundancy
|
|
|
|
cfg.Volatile.SegmentsSize = layer.gateway.segmentSize
|
|
|
|
|
|
|
|
_, err = layer.gateway.project.CreateBucket(ctx, bucketName, &cfg)
|
2018-12-07 18:31:29 +00:00
|
|
|
|
2018-08-16 15:32:28 +01:00
|
|
|
return err
|
2018-06-01 13:51:13 +01:00
|
|
|
}
|
|
|
|
|
2018-12-07 18:31:29 +00:00
|
|
|
func (layer *gatewayLayer) CopyObject(ctx context.Context, srcBucket, srcObject, destBucket, destObject string, srcInfo minio.ObjectInfo) (objInfo minio.ObjectInfo, err error) {
|
2018-07-12 18:29:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-08-29 16:00:05 +01:00
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
bucket, err := layer.gateway.project.OpenBucket(ctx, srcBucket, layer.gateway.access)
|
2019-04-15 21:13:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return minio.ObjectInfo{}, convertError(err, srcBucket, "")
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, bucket.Close()) }()
|
|
|
|
|
|
|
|
object, err := bucket.OpenObject(ctx, srcObject)
|
2018-08-29 16:00:05 +01:00
|
|
|
if err != nil {
|
2018-12-07 18:31:29 +00:00
|
|
|
return minio.ObjectInfo{}, convertError(err, srcBucket, srcObject)
|
2018-08-29 16:00:05 +01:00
|
|
|
}
|
2019-04-15 21:13:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, object.Close()) }()
|
2018-08-29 16:00:05 +01:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
reader, err := object.DownloadRange(ctx, 0, -1)
|
|
|
|
if err != nil {
|
|
|
|
return minio.ObjectInfo{}, convertError(err, srcBucket, srcObject)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, reader.Close()) }()
|
2018-08-29 16:00:05 +01:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
opts := uplink.UploadOptions{
|
|
|
|
ContentType: object.Meta.ContentType,
|
|
|
|
Metadata: object.Meta.Metadata,
|
|
|
|
Expires: object.Meta.Expires,
|
2018-06-01 13:51:13 +01:00
|
|
|
}
|
2019-04-15 21:13:02 +01:00
|
|
|
opts.Volatile.EncryptionParameters = object.Meta.Volatile.EncryptionParameters
|
|
|
|
opts.Volatile.RedundancyScheme = object.Meta.Volatile.RedundancyScheme
|
2018-08-29 16:00:05 +01:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
return layer.putObject(ctx, destBucket, destObject, reader, &opts)
|
2018-08-29 16:00:05 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
func (layer *gatewayLayer) putObject(ctx context.Context, bucketName, objectPath string, reader io.Reader, opts *uplink.UploadOptions) (objInfo minio.ObjectInfo, err error) {
|
2018-08-29 16:00:05 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-06-28 06:18:24 +01:00
|
|
|
bucket, err := layer.gateway.project.OpenBucket(ctx, bucketName, layer.gateway.access)
|
2018-08-16 15:32:28 +01:00
|
|
|
if err != nil {
|
2019-04-15 21:13:02 +01:00
|
|
|
return minio.ObjectInfo{}, convertError(err, bucketName, "")
|
2018-08-16 15:32:28 +01:00
|
|
|
}
|
2019-04-15 21:13:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, bucket.Close()) }()
|
2018-12-07 18:31:29 +00:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
err = bucket.UploadObject(ctx, objectPath, reader, opts)
|
2018-12-07 18:31:29 +00:00
|
|
|
if err != nil {
|
2019-04-15 21:13:02 +01:00
|
|
|
return minio.ObjectInfo{}, convertError(err, bucketName, "")
|
2018-12-07 18:31:29 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
object, err := bucket.OpenObject(ctx, objectPath)
|
2018-12-07 18:31:29 +00:00
|
|
|
if err != nil {
|
2019-04-15 21:13:02 +01:00
|
|
|
return minio.ObjectInfo{}, convertError(err, bucketName, objectPath)
|
2018-12-07 18:31:29 +00:00
|
|
|
}
|
2019-04-15 21:13:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, object.Close()) }()
|
2018-12-07 18:31:29 +00:00
|
|
|
|
2018-06-01 13:51:13 +01:00
|
|
|
return minio.ObjectInfo{
|
2019-04-15 21:13:02 +01:00
|
|
|
Name: object.Meta.Path,
|
|
|
|
Bucket: object.Meta.Bucket,
|
|
|
|
ModTime: object.Meta.Modified,
|
|
|
|
Size: object.Meta.Size,
|
|
|
|
ETag: hex.EncodeToString(object.Meta.Checksum),
|
|
|
|
ContentType: object.Meta.ContentType,
|
|
|
|
UserDefined: object.Meta.Metadata,
|
2018-12-07 18:31:29 +00:00
|
|
|
}, nil
|
2018-05-09 00:49:54 +01:00
|
|
|
}
|
|
|
|
|
2019-12-18 13:35:28 +00:00
|
|
|
func upload(ctx context.Context, streams streams.Store, mutableObject kvmetainfo.MutableObject, reader io.Reader) error {
|
2018-12-07 18:31:29 +00:00
|
|
|
mutableStream, err := mutableObject.CreateStream(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
upload := stream.NewUpload(ctx, mutableStream, streams)
|
|
|
|
|
|
|
|
_, err = io.Copy(upload, reader)
|
2018-09-21 20:44:45 +01:00
|
|
|
|
2019-09-16 21:00:50 +01:00
|
|
|
return errs.Wrap(errs.Combine(err, upload.Close()))
|
2018-12-07 18:31:29 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
func (layer *gatewayLayer) PutObject(ctx context.Context, bucketName, objectPath string, data *hash.Reader, metadata map[string]string) (objInfo minio.ObjectInfo, err error) {
|
2018-08-29 16:00:05 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-07 18:31:29 +00:00
|
|
|
|
|
|
|
contentType := metadata["content-type"]
|
2018-08-29 16:00:05 +01:00
|
|
|
delete(metadata, "content-type")
|
2018-12-07 18:31:29 +00:00
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
opts := uplink.UploadOptions{
|
|
|
|
ContentType: contentType,
|
|
|
|
Metadata: metadata,
|
2018-08-29 16:00:05 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 21:13:02 +01:00
|
|
|
return layer.putObject(ctx, bucketName, objectPath, data, &opts)
|
2018-08-29 16:00:05 +01:00
|
|
|
}
|
|
|
|
|
2018-12-07 18:31:29 +00:00
|
|
|
func (layer *gatewayLayer) Shutdown(ctx context.Context) (err error) {
|
2018-07-12 18:29:02 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-07-27 19:13:08 +01:00
|
|
|
return nil
|
2018-05-09 00:49:54 +01:00
|
|
|
}
|
|
|
|
|
2018-12-07 18:31:29 +00:00
|
|
|
func (layer *gatewayLayer) StorageInfo(context.Context) minio.StorageInfo {
|
2018-06-01 13:51:13 +01:00
|
|
|
return minio.StorageInfo{}
|
2018-05-09 00:49:54 +01:00
|
|
|
}
|
2018-11-21 11:17:28 +00:00
|
|
|
|
2018-12-07 18:31:29 +00:00
|
|
|
func convertError(err error, bucket, object string) error {
|
|
|
|
if storj.ErrNoBucket.Has(err) {
|
|
|
|
return minio.BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:17:28 +00:00
|
|
|
if storj.ErrBucketNotFound.Has(err) {
|
|
|
|
return minio.BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
|
2018-12-07 18:31:29 +00:00
|
|
|
if storj.ErrNoPath.Has(err) {
|
|
|
|
return minio.ObjectNameInvalid{Bucket: bucket, Object: object}
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:17:28 +00:00
|
|
|
if storj.ErrObjectNotFound.Has(err) {
|
|
|
|
return minio.ObjectNotFound{Bucket: bucket, Object: object}
|
|
|
|
}
|
2018-12-07 18:31:29 +00:00
|
|
|
|
2018-11-21 11:17:28 +00:00
|
|
|
return err
|
|
|
|
}
|