2019-02-04 16:56:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package testplanet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-08-13 18:29:02 +01:00
|
|
|
"io"
|
2020-10-29 11:58:36 +00:00
|
|
|
"runtime/pprof"
|
2019-04-09 18:01:45 +01:00
|
|
|
"strconv"
|
2019-04-25 09:17:26 +01:00
|
|
|
"time"
|
2019-02-04 16:56:10 +00:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2023-02-17 16:06:24 +00:00
|
|
|
"storj.io/common/grant"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/macaroon"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
|
|
|
"storj.io/common/rpc"
|
|
|
|
"storj.io/common/storj"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-07-24 10:40:17 +01:00
|
|
|
"storj.io/storj/satellite/console"
|
2020-03-30 10:08:02 +01:00
|
|
|
"storj.io/uplink"
|
2021-04-30 14:41:20 +01:00
|
|
|
"storj.io/uplink/private/metaclient"
|
2020-02-21 14:07:29 +00:00
|
|
|
"storj.io/uplink/private/piecestore"
|
2020-05-26 09:05:43 +01:00
|
|
|
"storj.io/uplink/private/testuplink"
|
2019-02-04 16:56:10 +00:00
|
|
|
)
|
|
|
|
|
2023-02-17 16:06:24 +00:00
|
|
|
// UplinkConfig testplanet configuration for uplink.
|
|
|
|
type UplinkConfig struct {
|
|
|
|
DefaultPathCipher storj.CipherSuite
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:27:24 +01:00
|
|
|
// Uplink is a registered user on all satellites,
|
|
|
|
// which contains the necessary accesses and project info.
|
2019-02-04 16:56:10 +00:00
|
|
|
type Uplink struct {
|
2020-05-21 12:22:37 +01:00
|
|
|
Log *zap.Logger
|
|
|
|
Identity *identity.FullIdentity
|
|
|
|
Dialer rpc.Dialer
|
2020-07-20 10:40:12 +01:00
|
|
|
Config uplink.Config
|
2019-09-12 11:38:49 +01:00
|
|
|
|
2020-04-16 13:12:46 +01:00
|
|
|
APIKey map[storj.NodeID]*macaroon.APIKey
|
2020-05-26 09:05:43 +01:00
|
|
|
Access map[storj.NodeID]*uplink.Access
|
2021-06-09 12:39:38 +01:00
|
|
|
User map[storj.NodeID]UserLogin
|
2020-04-16 13:12:46 +01:00
|
|
|
|
|
|
|
// Projects is indexed by the satellite number.
|
|
|
|
Projects []*Project
|
|
|
|
}
|
|
|
|
|
|
|
|
// Project contains all necessary information about a user.
|
|
|
|
type Project struct {
|
|
|
|
client *Uplink
|
|
|
|
|
|
|
|
ID uuid.UUID
|
|
|
|
Owner ProjectOwner
|
|
|
|
|
|
|
|
Satellite Peer
|
|
|
|
APIKey string
|
|
|
|
|
|
|
|
RawAPIKey *macaroon.APIKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectOwner contains information about the project owner.
|
|
|
|
type ProjectOwner struct {
|
|
|
|
ID uuid.UUID
|
|
|
|
Email string
|
|
|
|
}
|
|
|
|
|
2021-06-09 12:39:38 +01:00
|
|
|
// UserLogin contains information about the user login.
|
|
|
|
type UserLogin struct {
|
|
|
|
Email string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
2020-04-16 13:12:46 +01:00
|
|
|
// DialMetainfo dials the satellite with the appropriate api key.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (project *Project) DialMetainfo(ctx context.Context) (_ *metaclient.Client, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-04-16 13:12:46 +01:00
|
|
|
return project.client.DialMetainfo(ctx, project.Satellite, project.RawAPIKey)
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:22:37 +01:00
|
|
|
// newUplinks creates initializes uplinks, requires peer to have at least one satellite.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (planet *Planet) newUplinks(ctx context.Context, prefix string, count int) (_ []*Uplink, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-06-21 14:39:43 +01:00
|
|
|
var xs []*Uplink
|
|
|
|
for i := 0; i < count; i++ {
|
2020-10-29 11:58:36 +00:00
|
|
|
name := prefix + strconv.Itoa(i)
|
2023-02-17 16:06:24 +00:00
|
|
|
|
|
|
|
log := planet.log.Named(name)
|
|
|
|
|
2020-10-29 11:58:36 +00:00
|
|
|
var uplink *Uplink
|
|
|
|
var err error
|
|
|
|
pprof.Do(ctx, pprof.Labels("peer", name), func(ctx context.Context) {
|
2023-02-17 16:06:24 +00:00
|
|
|
uplink, err = planet.newUplink(ctx, i, log, name)
|
2020-10-29 11:58:36 +00:00
|
|
|
})
|
2019-06-21 14:39:43 +01:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return nil, errs.Wrap(err)
|
2019-06-21 14:39:43 +01:00
|
|
|
}
|
|
|
|
xs = append(xs, uplink)
|
|
|
|
}
|
|
|
|
|
|
|
|
return xs, nil
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:22:37 +01:00
|
|
|
// newUplink creates a new uplink.
|
2023-02-17 16:06:24 +00:00
|
|
|
func (planet *Planet) newUplink(ctx context.Context, index int, log *zap.Logger, name string) (_ *Uplink, err error) {
|
2021-09-29 08:06:51 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-02-04 16:56:10 +00:00
|
|
|
identity, err := planet.NewIdentity()
|
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return nil, errs.Wrap(err)
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
tlsOptions, err := tlsopts.NewOptions(identity, tlsopts.Config{
|
2019-04-09 18:01:45 +01:00
|
|
|
PeerIDVersions: strconv.Itoa(int(planet.config.IdentityVersion.Number)),
|
2019-08-19 23:10:38 +01:00
|
|
|
}, nil)
|
2019-02-11 11:17:32 +00:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return nil, errs.Wrap(err)
|
2019-02-11 11:17:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
planetUplink := &Uplink{
|
2020-05-21 12:22:37 +01:00
|
|
|
Log: planet.log.Named(name),
|
|
|
|
Identity: identity,
|
|
|
|
APIKey: map[storj.NodeID]*macaroon.APIKey{},
|
2020-05-26 09:05:43 +01:00
|
|
|
Access: map[storj.NodeID]*uplink.Access{},
|
2021-06-09 12:39:38 +01:00
|
|
|
User: map[storj.NodeID]UserLogin{},
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
planetUplink.Log.Debug("id=" + identity.ID.String())
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
planetUplink.Dialer = rpc.NewDefaultDialer(tlsOptions)
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-02-05 17:22:17 +00:00
|
|
|
for j, satellite := range planet.Satellites {
|
2020-07-24 10:40:17 +01:00
|
|
|
consoleAPI := satellite.API.Console
|
2019-02-05 17:22:17 +00:00
|
|
|
|
|
|
|
projectName := fmt.Sprintf("%s_%d", name, j)
|
2020-07-24 10:40:17 +01:00
|
|
|
user, err := satellite.AddUser(ctx, console.CreateUser{
|
|
|
|
FullName: fmt.Sprintf("User %s", projectName),
|
|
|
|
Email: fmt.Sprintf("user@%s.test", projectName),
|
|
|
|
}, 10)
|
2020-04-06 19:29:32 +01:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return nil, errs.Wrap(err)
|
2020-04-06 19:29:32 +01:00
|
|
|
}
|
|
|
|
|
2021-06-09 12:39:38 +01:00
|
|
|
planetUplink.User[satellite.ID()] = UserLogin{
|
|
|
|
Email: user.Email,
|
|
|
|
Password: user.FullName,
|
|
|
|
}
|
|
|
|
|
2020-05-19 14:36:01 +01:00
|
|
|
project, err := satellite.AddProject(ctx, user.ID, projectName)
|
2020-04-06 19:29:32 +01:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return nil, errs.Wrap(err)
|
2020-04-06 19:29:32 +01:00
|
|
|
}
|
|
|
|
|
2022-06-05 23:41:38 +01:00
|
|
|
userCtx, err := satellite.UserContext(ctx, user.ID)
|
2019-02-05 17:22:17 +00:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return nil, errs.Wrap(err)
|
2019-02-05 17:22:17 +00:00
|
|
|
}
|
2022-06-05 23:41:38 +01:00
|
|
|
_, apiKey, err := consoleAPI.Service.CreateAPIKey(userCtx, project.ID, "root")
|
2019-02-05 17:22:17 +00:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return nil, errs.Wrap(err)
|
2019-02-05 17:22:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
planetUplink.APIKey[satellite.ID()] = apiKey
|
2020-04-16 13:12:46 +01:00
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
planetUplink.Projects = append(planetUplink.Projects, &Project{
|
|
|
|
client: planetUplink,
|
2020-04-16 13:12:46 +01:00
|
|
|
|
|
|
|
ID: project.ID,
|
|
|
|
Owner: ProjectOwner{
|
2020-05-19 14:36:01 +01:00
|
|
|
ID: user.ID,
|
|
|
|
Email: user.Email,
|
2020-04-16 13:12:46 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
Satellite: satellite,
|
2020-05-19 14:36:01 +01:00
|
|
|
APIKey: apiKey.Serialize(),
|
2020-04-16 13:12:46 +01:00
|
|
|
|
2020-05-19 14:36:01 +01:00
|
|
|
RawAPIKey: apiKey,
|
2020-04-16 13:12:46 +01:00
|
|
|
})
|
2023-02-17 16:06:24 +00:00
|
|
|
|
|
|
|
var config UplinkConfig
|
|
|
|
if planet.config.Reconfigure.Uplink != nil {
|
|
|
|
planet.config.Reconfigure.Uplink(log, index, &config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create access grant manually to avoid dialing satellite for
|
|
|
|
// project id and deriving key with argon2.IDKey method
|
|
|
|
encAccess := grant.NewEncryptionAccessWithDefaultKey(&storj.Key{})
|
|
|
|
if config.DefaultPathCipher == storj.EncUnspecified {
|
|
|
|
encAccess.SetDefaultPathCipher(storj.EncAESGCM)
|
|
|
|
} else {
|
|
|
|
encAccess.SetDefaultPathCipher(config.DefaultPathCipher)
|
|
|
|
}
|
|
|
|
|
|
|
|
grantAccess := grant.Access{
|
|
|
|
SatelliteAddress: satellite.URL(),
|
|
|
|
APIKey: apiKey,
|
|
|
|
EncAccess: encAccess,
|
|
|
|
}
|
|
|
|
|
|
|
|
serializedAccess, err := grantAccess.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
access, err := uplink.ParseAccess(serializedAccess)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
planetUplink.Access[satellite.ID()] = access
|
2019-02-05 17:22:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
planet.Uplinks = append(planet.Uplinks, planetUplink)
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
return planetUplink, nil
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// ID returns uplink id.
|
2020-05-20 14:11:54 +01:00
|
|
|
func (client *Uplink) ID() storj.NodeID { return client.Identity.ID }
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Addr returns uplink address.
|
2020-05-20 14:11:54 +01:00
|
|
|
func (client *Uplink) Addr() string { return "" }
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Shutdown shuts down all uplink dependencies.
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) Shutdown() error { return nil }
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// DialMetainfo dials destination with apikey and returns metainfo Client.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (client *Uplink) DialMetainfo(ctx context.Context, destination Peer, apikey *macaroon.APIKey) (_ *metaclient.Client, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2021-04-30 14:41:20 +01:00
|
|
|
return metaclient.DialNodeURL(ctx, client.Dialer, destination.NodeURL().String(), apikey, "Test/1.0")
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialPiecestore dials destination storagenode and returns a piecestore client.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (client *Uplink) DialPiecestore(ctx context.Context, destination Peer) (_ *piecestore.Client, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2021-05-06 14:53:55 +01:00
|
|
|
return piecestore.Dial(ctx, client.Dialer, destination.NodeURL(), piecestore.DefaultConfig)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 11:06:06 +00:00
|
|
|
// OpenProject opens project with predefined access grant and gives access to pure uplink API.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (client *Uplink) OpenProject(ctx context.Context, satellite *Satellite) (_ *uplink.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-12-04 11:06:06 +00:00
|
|
|
_, found := testuplink.GetMaxSegmentSize(ctx)
|
|
|
|
if !found {
|
|
|
|
ctx = testuplink.WithMaxSegmentSize(ctx, satellite.Config.Metainfo.MaxSegmentSize)
|
|
|
|
}
|
|
|
|
return uplink.OpenProject(ctx, client.Access[satellite.ID()])
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Upload data to specific satellite.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (client *Uplink) Upload(ctx context.Context, satellite *Satellite, bucket string, path storj.Path, data []byte) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-07-23 15:58:45 +01:00
|
|
|
return client.UploadWithExpiration(ctx, satellite, bucket, path, data, time.Time{})
|
2019-05-08 12:11:59 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// UploadWithExpiration data to specific satellite and expiration time.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (client *Uplink) UploadWithExpiration(ctx context.Context, satellite *Satellite, bucketName string, path storj.Path, data []byte, expiration time.Time) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
_, found := testuplink.GetMaxSegmentSize(ctx)
|
|
|
|
if !found {
|
|
|
|
ctx = testuplink.WithMaxSegmentSize(ctx, satellite.Config.Metainfo.MaxSegmentSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := client.GetProject(ctx, satellite)
|
2019-03-21 14:26:56 +00:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return errs.Wrap(err)
|
2019-03-21 14:26:56 +00:00
|
|
|
}
|
2020-03-30 10:08:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
2019-03-21 14:26:56 +00:00
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
_, err = project.EnsureBucket(ctx, bucketName)
|
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return errs.Wrap(err)
|
2020-03-30 10:08:02 +01:00
|
|
|
}
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
upload, err := project.UploadObject(ctx, bucketName, path, &uplink.UploadOptions{
|
|
|
|
Expires: expiration,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return errs.Wrap(err)
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
_, err = io.Copy(upload, bytes.NewReader(data))
|
|
|
|
if err != nil {
|
|
|
|
abortErr := upload.Abort()
|
|
|
|
err = errs.Combine(err, abortErr)
|
2023-01-31 13:44:59 +00:00
|
|
|
return errs.Wrap(err)
|
2020-03-30 10:08:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return upload.Commit()
|
2019-07-23 15:58:45 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Download data from specific satellite.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (client *Uplink) Download(ctx context.Context, satellite *Satellite, bucketName string, path storj.Path) (_ []byte, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
project, err := client.GetProject(ctx, satellite)
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return nil, errs.Wrap(err)
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
2020-03-30 10:08:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
download, err := project.DownloadObject(ctx, bucketName, path, nil)
|
2019-07-01 15:35:10 +01:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return nil, errs.Wrap(err)
|
2019-07-01 15:35:10 +01:00
|
|
|
}
|
2020-03-30 10:08:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, download.Close()) }()
|
2019-07-01 15:35:10 +01:00
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
data, err := io.ReadAll(download)
|
2019-07-23 15:58:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
return data, nil
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// DownloadStream returns stream for downloading data.
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) DownloadStream(ctx context.Context, satellite *Satellite, bucketName string, path storj.Path) (_ io.ReadCloser, cleanup func() error, err error) {
|
2021-09-29 08:06:51 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
project, err := client.GetProject(ctx, satellite)
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return nil, nil, errs.Wrap(err)
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
cleanup = func() error {
|
|
|
|
err = errs.Combine(err,
|
|
|
|
project.Close(),
|
|
|
|
)
|
2023-01-31 13:44:59 +00:00
|
|
|
return errs.Wrap(err)
|
2019-07-23 15:58:45 +01:00
|
|
|
}
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
downloader, err := project.DownloadObject(ctx, bucketName, path, nil)
|
2019-08-13 18:29:02 +01:00
|
|
|
return downloader, cleanup, err
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// DownloadStreamRange returns stream for downloading data.
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) DownloadStreamRange(ctx context.Context, satellite *Satellite, bucketName string, path storj.Path, start, limit int64) (_ io.ReadCloser, cleanup func() error, err error) {
|
2021-09-29 08:06:51 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
project, err := client.GetProject(ctx, satellite)
|
2019-08-13 18:29:02 +01:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return nil, nil, errs.Wrap(err)
|
2019-08-13 18:29:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup = func() error {
|
2023-01-31 13:44:59 +00:00
|
|
|
return errs.Combine(err, project.Close())
|
2019-08-13 18:29:02 +01:00
|
|
|
}
|
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
downloader, err := project.DownloadObject(ctx, bucketName, path, &uplink.DownloadOptions{
|
|
|
|
Offset: start,
|
|
|
|
Length: limit,
|
|
|
|
})
|
2023-01-31 13:44:59 +00:00
|
|
|
return downloader, cleanup, errs.Wrap(err)
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// DeleteObject deletes an object at the path in a bucket.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (client *Uplink) DeleteObject(ctx context.Context, satellite *Satellite, bucketName string, path storj.Path) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
project, err := client.GetProject(ctx, satellite)
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return errs.Wrap(err)
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
2020-03-30 10:08:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
_, err = project.DeleteObject(ctx, bucketName, path)
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return errs.Wrap(err)
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
2023-01-31 13:44:59 +00:00
|
|
|
return errs.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 12:33:03 +01:00
|
|
|
// CopyObject copies an object.
|
|
|
|
func (client *Uplink) CopyObject(ctx context.Context, satellite *Satellite, oldBucket, oldKey, newBucket, newKey string) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
project, err := client.GetProject(ctx, satellite)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
|
|
|
|
|
|
|
_, err = project.CopyObject(ctx, oldBucket, oldKey, newBucket, newKey, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// CreateBucket creates a new bucket.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (client *Uplink) CreateBucket(ctx context.Context, satellite *Satellite, bucketName string) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
project, err := client.GetProject(ctx, satellite)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return errs.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
_, err = project.CreateBucket(ctx, bucketName)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return errs.Wrap(err)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 12:18:18 +00:00
|
|
|
// DeleteBucket deletes a bucket.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (client *Uplink) DeleteBucket(ctx context.Context, satellite *Satellite, bucketName string) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
project, err := client.GetProject(ctx, satellite)
|
2020-02-10 12:18:18 +00:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return errs.Wrap(err)
|
2020-02-10 12:18:18 +00:00
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
_, err = project.DeleteBucket(ctx, bucketName)
|
2020-02-10 12:18:18 +00:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return errs.Wrap(err)
|
2020-02-10 12:18:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-05 22:24:46 +00:00
|
|
|
// ListBuckets returns a list of all buckets in a project.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (client *Uplink) ListBuckets(ctx context.Context, satellite *Satellite) (_ []*uplink.Bucket, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2021-01-05 22:24:46 +00:00
|
|
|
var buckets = []*uplink.Bucket{}
|
|
|
|
project, err := client.GetProject(ctx, satellite)
|
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return buckets, errs.Wrap(err)
|
2021-01-05 22:24:46 +00:00
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
|
|
|
|
|
|
|
iter := project.ListBuckets(ctx, &uplink.ListBucketsOptions{})
|
|
|
|
for iter.Next() {
|
|
|
|
buckets = append(buckets, iter.Item())
|
|
|
|
}
|
|
|
|
return buckets, iter.Err()
|
|
|
|
}
|
|
|
|
|
2021-05-12 11:21:15 +01:00
|
|
|
// ListObjects returns a list of all objects in a bucket.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (client *Uplink) ListObjects(ctx context.Context, satellite *Satellite, bucketName string) (_ []*uplink.Object, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2021-05-12 11:21:15 +01:00
|
|
|
var objects = []*uplink.Object{}
|
|
|
|
project, err := client.GetProject(ctx, satellite)
|
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return objects, errs.Wrap(err)
|
2021-05-12 11:21:15 +01:00
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
|
|
|
|
|
|
|
iter := project.ListObjects(ctx, bucketName, &uplink.ListObjectsOptions{})
|
|
|
|
for iter.Next() {
|
|
|
|
objects = append(objects, iter.Item())
|
|
|
|
}
|
|
|
|
return objects, iter.Err()
|
|
|
|
}
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
// GetProject returns a uplink.Project which allows interactions with a specific project.
|
2021-09-29 08:06:51 +01:00
|
|
|
func (client *Uplink) GetProject(ctx context.Context, satellite *Satellite) (_ *uplink.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
access := client.Access[satellite.ID()]
|
2020-03-30 10:08:02 +01:00
|
|
|
|
2020-07-20 10:40:12 +01:00
|
|
|
project, err := client.Config.OpenProject(ctx, access)
|
2020-03-30 10:08:02 +01:00
|
|
|
if err != nil {
|
2023-01-31 13:44:59 +00:00
|
|
|
return nil, errs.Wrap(err)
|
2020-03-30 10:08:02 +01:00
|
|
|
}
|
|
|
|
return project, nil
|
|
|
|
}
|