2022-01-27 00:01:03 +00:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metabase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-17 16:19:44 +01:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2022-02-16 23:24:38 +00:00
|
|
|
"time"
|
2022-01-27 00:01:03 +00:00
|
|
|
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/uuid"
|
2022-02-16 23:24:38 +00:00
|
|
|
"storj.io/private/dbutil/pgutil"
|
2022-01-27 00:01:03 +00:00
|
|
|
"storj.io/private/dbutil/txutil"
|
|
|
|
"storj.io/private/tagsql"
|
|
|
|
)
|
|
|
|
|
2022-09-14 12:55:58 +01:00
|
|
|
// BeginCopyObjectResult holds data needed to begin copy object.
|
|
|
|
type BeginCopyObjectResult BeginMoveCopyResults
|
2022-01-27 00:01:03 +00:00
|
|
|
|
|
|
|
// BeginCopyObject holds all data needed begin copy object method.
|
|
|
|
type BeginCopyObject struct {
|
|
|
|
ObjectLocation
|
2022-06-22 12:33:03 +01:00
|
|
|
|
|
|
|
// VerifyLimits holds a callback by which the caller can interrupt the copy
|
|
|
|
// if it turns out the copy would exceed a limit.
|
|
|
|
VerifyLimits func(encryptedObjectSize int64, nSegments int64) error
|
2022-01-27 00:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BeginCopyObject collects all data needed to begin object copy procedure.
|
2022-09-14 12:55:58 +01:00
|
|
|
func (db *DB) BeginCopyObject(ctx context.Context, opts BeginCopyObject) (_ BeginCopyObjectResult, err error) {
|
|
|
|
result, err := db.beginMoveCopyObject(ctx, opts.ObjectLocation, CopySegmentLimit, opts.VerifyLimits)
|
2022-01-27 00:01:03 +00:00
|
|
|
if err != nil {
|
2022-08-08 16:14:08 +01:00
|
|
|
return BeginCopyObjectResult{}, err
|
2022-01-27 00:01:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 12:55:58 +01:00
|
|
|
return BeginCopyObjectResult(result), nil
|
2022-01-27 00:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FinishCopyObject holds all data needed to finish object copy.
|
|
|
|
type FinishCopyObject struct {
|
|
|
|
ObjectStream
|
2022-03-04 11:28:04 +00:00
|
|
|
NewBucket string
|
|
|
|
NewEncryptedObjectKey ObjectKey
|
|
|
|
NewStreamID uuid.UUID
|
|
|
|
|
|
|
|
OverrideMetadata bool
|
|
|
|
NewEncryptedMetadata []byte
|
2022-03-23 16:06:14 +00:00
|
|
|
NewEncryptedMetadataKeyNonce storj.Nonce
|
2022-01-27 00:01:03 +00:00
|
|
|
NewEncryptedMetadataKey []byte
|
2022-03-04 11:28:04 +00:00
|
|
|
|
|
|
|
NewSegmentKeys []EncryptedKeyAndNonce
|
2022-06-22 12:33:03 +01:00
|
|
|
|
2023-10-17 16:19:44 +01:00
|
|
|
// NewDisallowDelete indicates whether the user is allowed to delete an existing unversioned object.
|
|
|
|
NewDisallowDelete bool
|
|
|
|
|
|
|
|
// NewVersioned indicates that the object allows multiple versions.
|
|
|
|
NewVersioned bool
|
|
|
|
|
2022-06-22 12:33:03 +01:00
|
|
|
// VerifyLimits holds a callback by which the caller can interrupt the copy
|
|
|
|
// if it turns out completing the copy would exceed a limit.
|
|
|
|
// It will be called only once.
|
|
|
|
VerifyLimits func(encryptedObjectSize int64, nSegments int64) error
|
2022-01-27 00:01:03 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 15:24:16 +00:00
|
|
|
// NewLocation returns the new object location.
|
|
|
|
func (finishCopy FinishCopyObject) NewLocation() ObjectLocation {
|
|
|
|
return ObjectLocation{
|
|
|
|
ProjectID: finishCopy.ProjectID,
|
|
|
|
BucketName: finishCopy.NewBucket,
|
|
|
|
ObjectKey: finishCopy.NewEncryptedObjectKey,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 00:01:03 +00:00
|
|
|
// Verify verifies metabase.FinishCopyObject data.
|
|
|
|
func (finishCopy FinishCopyObject) Verify() error {
|
|
|
|
if err := finishCopy.ObjectStream.Verify(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case len(finishCopy.NewBucket) == 0:
|
|
|
|
return ErrInvalidRequest.New("NewBucket is missing")
|
2022-03-16 10:17:27 +00:00
|
|
|
case finishCopy.NewStreamID.IsZero():
|
|
|
|
return ErrInvalidRequest.New("NewStreamID is missing")
|
2022-01-27 00:01:03 +00:00
|
|
|
case finishCopy.ObjectStream.StreamID == finishCopy.NewStreamID:
|
|
|
|
return ErrInvalidRequest.New("StreamIDs are identical")
|
|
|
|
case len(finishCopy.NewEncryptedObjectKey) == 0:
|
|
|
|
return ErrInvalidRequest.New("NewEncryptedObjectKey is missing")
|
2022-03-04 11:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if finishCopy.OverrideMetadata {
|
2022-03-23 16:06:14 +00:00
|
|
|
if finishCopy.NewEncryptedMetadata == nil && (!finishCopy.NewEncryptedMetadataKeyNonce.IsZero() || finishCopy.NewEncryptedMetadataKey != nil) {
|
2022-03-04 11:28:04 +00:00
|
|
|
return ErrInvalidRequest.New("EncryptedMetadataNonce and EncryptedMetadataEncryptedKey must be not set if EncryptedMetadata is not set")
|
2022-03-23 16:06:14 +00:00
|
|
|
} else if finishCopy.NewEncryptedMetadata != nil && (finishCopy.NewEncryptedMetadataKeyNonce.IsZero() || finishCopy.NewEncryptedMetadataKey == nil) {
|
2022-03-04 11:28:04 +00:00
|
|
|
return ErrInvalidRequest.New("EncryptedMetadataNonce and EncryptedMetadataEncryptedKey must be set if EncryptedMetadata is set")
|
|
|
|
}
|
2022-03-30 12:12:08 +01:00
|
|
|
} else {
|
|
|
|
switch {
|
|
|
|
case finishCopy.NewEncryptedMetadataKeyNonce.IsZero() && len(finishCopy.NewEncryptedMetadataKey) != 0:
|
|
|
|
return ErrInvalidRequest.New("EncryptedMetadataKeyNonce is missing")
|
|
|
|
case len(finishCopy.NewEncryptedMetadataKey) == 0 && !finishCopy.NewEncryptedMetadataKeyNonce.IsZero():
|
|
|
|
return ErrInvalidRequest.New("EncryptedMetadataKey is missing")
|
|
|
|
}
|
2022-01-27 00:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FinishCopyObject accepts new encryption keys for copied object and insert the corresponding new object ObjectKey and segments EncryptedKey.
|
2022-05-17 16:25:48 +01:00
|
|
|
// It returns the object at the destination location.
|
2022-02-24 10:54:57 +00:00
|
|
|
func (db *DB) FinishCopyObject(ctx context.Context, opts FinishCopyObject) (object Object, err error) {
|
2022-01-27 00:01:03 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.Verify(); err != nil {
|
2022-02-24 10:54:57 +00:00
|
|
|
return Object{}, err
|
2022-01-27 00:01:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 16:25:48 +01:00
|
|
|
newObject := Object{}
|
2022-06-24 00:15:42 +01:00
|
|
|
var copyMetadata []byte
|
2022-02-18 00:03:50 +00:00
|
|
|
|
2023-10-31 15:24:16 +00:00
|
|
|
var precommit precommitConstraintResult
|
2022-06-24 00:15:42 +01:00
|
|
|
err = txutil.WithTx(ctx, db.db, nil, func(ctx context.Context, tx tagsql.Tx) (err error) {
|
2023-10-17 16:19:44 +01:00
|
|
|
sourceObject, err := getObjectExactVersion(ctx, tx, opts)
|
2022-06-24 00:15:42 +01:00
|
|
|
if err != nil {
|
2023-10-17 16:19:44 +01:00
|
|
|
if ErrObjectNotFound.Has(err) {
|
|
|
|
return ErrObjectNotFound.New("source object not found")
|
|
|
|
}
|
2022-05-17 16:25:48 +01:00
|
|
|
return err
|
2022-02-18 00:03:50 +00:00
|
|
|
}
|
2023-10-17 16:19:44 +01:00
|
|
|
if sourceObject.StreamID != opts.StreamID {
|
|
|
|
// TODO(versioning): should we report it as "not found" instead?
|
|
|
|
return ErrObjectNotFound.New("object was changed during copy")
|
2022-09-13 10:15:40 +01:00
|
|
|
}
|
2023-10-17 16:19:44 +01:00
|
|
|
|
2022-06-22 12:33:03 +01:00
|
|
|
if opts.VerifyLimits != nil {
|
|
|
|
err := opts.VerifyLimits(sourceObject.TotalEncryptedSize, int64(sourceObject.SegmentCount))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 16:25:48 +01:00
|
|
|
if int(sourceObject.SegmentCount) != len(opts.NewSegmentKeys) {
|
|
|
|
return ErrInvalidRequest.New("wrong number of segments keys received (received %d, need %d)", len(opts.NewSegmentKeys), sourceObject.SegmentCount)
|
2022-06-24 00:15:42 +01:00
|
|
|
}
|
2022-01-27 00:01:03 +00:00
|
|
|
|
2022-06-24 00:15:42 +01:00
|
|
|
var newSegments struct {
|
|
|
|
Positions []int64
|
|
|
|
EncryptedKeys [][]byte
|
|
|
|
EncryptedKeyNonces [][]byte
|
|
|
|
}
|
2022-01-27 00:01:03 +00:00
|
|
|
|
2022-06-24 00:15:42 +01:00
|
|
|
for _, u := range opts.NewSegmentKeys {
|
|
|
|
newSegments.EncryptedKeys = append(newSegments.EncryptedKeys, u.EncryptedKey)
|
|
|
|
newSegments.EncryptedKeyNonces = append(newSegments.EncryptedKeyNonces, u.EncryptedKeyNonce)
|
|
|
|
newSegments.Positions = append(newSegments.Positions, int64(u.Position.Encode()))
|
|
|
|
}
|
2022-01-27 00:01:03 +00:00
|
|
|
|
2022-05-17 16:25:48 +01:00
|
|
|
positions := make([]int64, sourceObject.SegmentCount)
|
2022-01-27 00:01:03 +00:00
|
|
|
|
2022-05-17 16:25:48 +01:00
|
|
|
rootPieceIDs := make([][]byte, sourceObject.SegmentCount)
|
2022-02-16 23:24:38 +00:00
|
|
|
|
2022-05-17 16:25:48 +01:00
|
|
|
expiresAts := make([]*time.Time, sourceObject.SegmentCount)
|
|
|
|
encryptedSizes := make([]int32, sourceObject.SegmentCount)
|
|
|
|
plainSizes := make([]int32, sourceObject.SegmentCount)
|
|
|
|
plainOffsets := make([]int64, sourceObject.SegmentCount)
|
|
|
|
inlineDatas := make([][]byte, sourceObject.SegmentCount)
|
2023-06-24 19:31:21 +01:00
|
|
|
placementConstraints := make([]storj.PlacementConstraint, sourceObject.SegmentCount)
|
|
|
|
remoteAliasPiecesLists := make([][]byte, sourceObject.SegmentCount)
|
2022-02-16 23:24:38 +00:00
|
|
|
|
2022-05-17 16:25:48 +01:00
|
|
|
redundancySchemes := make([]int64, sourceObject.SegmentCount)
|
2023-06-24 19:31:21 +01:00
|
|
|
|
2023-08-04 10:27:08 +01:00
|
|
|
err = withRows(db.db.QueryContext(ctx, `
|
2023-06-24 19:31:21 +01:00
|
|
|
SELECT
|
|
|
|
position,
|
|
|
|
expires_at,
|
|
|
|
root_piece_id,
|
|
|
|
encrypted_size, plain_offset, plain_size,
|
|
|
|
redundancy,
|
|
|
|
remote_alias_pieces,
|
|
|
|
placement,
|
|
|
|
inline_data
|
|
|
|
FROM segments
|
|
|
|
WHERE stream_id = $1
|
|
|
|
ORDER BY position ASC
|
|
|
|
LIMIT $2
|
2022-05-17 16:25:48 +01:00
|
|
|
`, sourceObject.StreamID, sourceObject.SegmentCount))(func(rows tagsql.Rows) error {
|
2023-08-04 10:27:08 +01:00
|
|
|
index := 0
|
|
|
|
for rows.Next() {
|
|
|
|
err := rows.Scan(
|
|
|
|
&positions[index],
|
|
|
|
&expiresAts[index],
|
|
|
|
&rootPieceIDs[index],
|
|
|
|
&encryptedSizes[index], &plainOffsets[index], &plainSizes[index],
|
|
|
|
&redundancySchemes[index],
|
|
|
|
&remoteAliasPiecesLists[index],
|
|
|
|
&placementConstraints[index],
|
|
|
|
&inlineDatas[index],
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-06-24 00:15:42 +01:00
|
|
|
return err
|
|
|
|
}
|
2023-08-04 10:27:08 +01:00
|
|
|
index++
|
|
|
|
}
|
2022-06-24 00:15:42 +01:00
|
|
|
|
2023-08-04 10:27:08 +01:00
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-23 23:31:12 +01:00
|
|
|
|
2023-08-04 10:27:08 +01:00
|
|
|
if index != int(sourceObject.SegmentCount) {
|
|
|
|
return Error.New("could not load all of the segment information")
|
|
|
|
}
|
2023-06-24 19:31:21 +01:00
|
|
|
|
2023-08-04 10:27:08 +01:00
|
|
|
return nil
|
|
|
|
})
|
2023-06-24 19:31:21 +01:00
|
|
|
|
2022-06-24 00:15:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.New("unable to copy object: %w", err)
|
2022-01-27 00:01:03 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 00:15:42 +01:00
|
|
|
onlyInlineSegments := true
|
|
|
|
for index := range positions {
|
|
|
|
if newSegments.Positions[index] != positions[index] {
|
|
|
|
return Error.New("missing new segment keys for segment %d", positions[index])
|
|
|
|
}
|
|
|
|
if onlyInlineSegments && (encryptedSizes[index] > 0) && len(inlineDatas[index]) == 0 {
|
|
|
|
onlyInlineSegments = false
|
|
|
|
}
|
2022-01-27 00:01:03 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 00:15:42 +01:00
|
|
|
if opts.OverrideMetadata {
|
|
|
|
copyMetadata = opts.NewEncryptedMetadata
|
2022-05-17 16:25:48 +01:00
|
|
|
} else {
|
|
|
|
copyMetadata = sourceObject.EncryptedMetadata
|
2022-02-24 00:17:35 +00:00
|
|
|
}
|
2022-01-27 00:01:03 +00:00
|
|
|
|
2023-10-31 15:24:16 +00:00
|
|
|
precommit, err = db.precommitConstraint(ctx, precommitConstraint{
|
|
|
|
Location: opts.NewLocation(),
|
|
|
|
Versioned: opts.NewVersioned,
|
|
|
|
DisallowDelete: opts.NewDisallowDelete,
|
|
|
|
}, tx)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
2022-05-17 16:25:48 +01:00
|
|
|
}
|
2022-03-04 11:28:04 +00:00
|
|
|
|
2023-10-17 16:19:44 +01:00
|
|
|
newStatus := committedWhereVersioned(opts.NewVersioned)
|
|
|
|
|
2022-01-27 00:01:03 +00:00
|
|
|
// TODO we need to handle metadata correctly (copy from original object or replace)
|
2022-06-13 18:47:07 +01:00
|
|
|
row := tx.QueryRowContext(ctx, `
|
2022-02-24 10:54:57 +00:00
|
|
|
INSERT INTO objects (
|
|
|
|
project_id, bucket_name, object_key, version, stream_id,
|
2023-10-17 16:19:44 +01:00
|
|
|
status, expires_at, segment_count,
|
2022-02-24 10:54:57 +00:00
|
|
|
encryption,
|
|
|
|
encrypted_metadata, encrypted_metadata_nonce, encrypted_metadata_encrypted_key,
|
|
|
|
total_plain_size, total_encrypted_size, fixed_segment_size,
|
|
|
|
zombie_deletion_deadline
|
|
|
|
) VALUES (
|
|
|
|
$1, $2, $3, $4, $5,
|
2023-10-17 16:19:44 +01:00
|
|
|
$6, $7, $8,
|
|
|
|
$9,
|
|
|
|
$10, $11, $12,
|
|
|
|
$13, $14, $15, null
|
2022-05-17 16:25:48 +01:00
|
|
|
)
|
2022-03-23 22:00:40 +00:00
|
|
|
RETURNING
|
2022-05-17 16:25:48 +01:00
|
|
|
created_at`,
|
2023-10-31 15:24:16 +00:00
|
|
|
opts.ProjectID, []byte(opts.NewBucket), opts.NewEncryptedObjectKey, precommit.HighestVersion+1, opts.NewStreamID,
|
2023-10-17 16:19:44 +01:00
|
|
|
newStatus, sourceObject.ExpiresAt, sourceObject.SegmentCount,
|
2022-05-17 16:25:48 +01:00
|
|
|
encryptionParameters{&sourceObject.Encryption},
|
2022-03-04 11:28:04 +00:00
|
|
|
copyMetadata, opts.NewEncryptedMetadataKeyNonce, opts.NewEncryptedMetadataKey,
|
2022-05-17 16:25:48 +01:00
|
|
|
sourceObject.TotalPlainSize, sourceObject.TotalEncryptedSize, sourceObject.FixedSegmentSize,
|
2022-01-27 00:01:03 +00:00
|
|
|
)
|
2022-03-23 22:00:40 +00:00
|
|
|
|
2022-05-17 16:25:48 +01:00
|
|
|
newObject = sourceObject
|
2023-10-31 15:24:16 +00:00
|
|
|
newObject.Version = precommit.HighestVersion + 1
|
2023-10-17 16:19:44 +01:00
|
|
|
newObject.Status = newStatus
|
2022-09-15 21:32:56 +01:00
|
|
|
|
2022-05-17 16:25:48 +01:00
|
|
|
err = row.Scan(&newObject.CreatedAt)
|
2022-03-23 22:00:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.New("unable to copy object: %w", err)
|
|
|
|
}
|
2022-04-17 19:58:19 +01:00
|
|
|
|
2022-06-13 18:47:07 +01:00
|
|
|
_, err = tx.ExecContext(ctx, `
|
2022-03-23 22:00:40 +00:00
|
|
|
INSERT INTO segments (
|
2022-04-17 19:58:19 +01:00
|
|
|
stream_id, position, expires_at,
|
2022-03-23 22:00:40 +00:00
|
|
|
encrypted_key_nonce, encrypted_key,
|
|
|
|
root_piece_id,
|
|
|
|
redundancy,
|
|
|
|
encrypted_size, plain_offset, plain_size,
|
2023-06-24 19:31:21 +01:00
|
|
|
remote_alias_pieces, placement,
|
2022-03-23 22:00:40 +00:00
|
|
|
inline_data
|
|
|
|
) SELECT
|
2022-04-17 19:58:19 +01:00
|
|
|
$1, UNNEST($2::INT8[]), UNNEST($3::timestamptz[]),
|
|
|
|
UNNEST($4::BYTEA[]), UNNEST($5::BYTEA[]),
|
|
|
|
UNNEST($6::BYTEA[]),
|
|
|
|
UNNEST($7::INT8[]),
|
|
|
|
UNNEST($8::INT4[]), UNNEST($9::INT8[]), UNNEST($10::INT4[]),
|
2023-06-24 19:31:21 +01:00
|
|
|
UNNEST($11::BYTEA[]), UNNEST($12::INT2[]),
|
|
|
|
UNNEST($13::BYTEA[])
|
2022-04-17 19:58:19 +01:00
|
|
|
`, opts.NewStreamID, pgutil.Int8Array(newSegments.Positions), pgutil.NullTimestampTZArray(expiresAts),
|
2022-02-16 23:24:38 +00:00
|
|
|
pgutil.ByteaArray(newSegments.EncryptedKeyNonces), pgutil.ByteaArray(newSegments.EncryptedKeys),
|
|
|
|
pgutil.ByteaArray(rootPieceIDs),
|
|
|
|
pgutil.Int8Array(redundancySchemes),
|
|
|
|
pgutil.Int4Array(encryptedSizes), pgutil.Int8Array(plainOffsets), pgutil.Int4Array(plainSizes),
|
2023-06-24 19:31:21 +01:00
|
|
|
pgutil.ByteaArray(remoteAliasPiecesLists), pgutil.PlacementConstraintArray(placementConstraints),
|
2022-02-16 23:24:38 +00:00
|
|
|
pgutil.ByteaArray(inlineDatas),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("unable to copy segments: %w", err)
|
2022-01-27 00:01:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 00:17:35 +00:00
|
|
|
if onlyInlineSegments {
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-17 16:25:48 +01:00
|
|
|
|
2022-01-27 00:01:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 00:03:50 +00:00
|
|
|
|
2022-01-27 00:01:03 +00:00
|
|
|
if err != nil {
|
2022-02-24 10:54:57 +00:00
|
|
|
return Object{}, err
|
2022-01-27 00:01:03 +00:00
|
|
|
}
|
2022-02-24 10:54:57 +00:00
|
|
|
|
2022-05-17 16:25:48 +01:00
|
|
|
newObject.StreamID = opts.NewStreamID
|
|
|
|
newObject.BucketName = opts.NewBucket
|
|
|
|
newObject.ObjectKey = opts.NewEncryptedObjectKey
|
|
|
|
newObject.EncryptedMetadata = copyMetadata
|
|
|
|
newObject.EncryptedMetadataEncryptedKey = opts.NewEncryptedMetadataKey
|
2022-03-23 16:06:14 +00:00
|
|
|
if !opts.NewEncryptedMetadataKeyNonce.IsZero() {
|
2022-05-17 16:25:48 +01:00
|
|
|
newObject.EncryptedMetadataNonce = opts.NewEncryptedMetadataKeyNonce[:]
|
2022-03-23 16:06:14 +00:00
|
|
|
}
|
2022-02-24 10:54:57 +00:00
|
|
|
|
2023-10-31 15:24:16 +00:00
|
|
|
precommit.submitMetrics()
|
2022-01-27 00:01:03 +00:00
|
|
|
mon.Meter("finish_copy_object").Mark(1)
|
|
|
|
|
2022-05-17 16:25:48 +01:00
|
|
|
return newObject, nil
|
2022-01-27 00:01:03 +00:00
|
|
|
}
|
2022-03-23 22:00:40 +00:00
|
|
|
|
2023-10-17 16:19:44 +01:00
|
|
|
// getObjectExactVersion returns object information for exact version.
|
|
|
|
func getObjectExactVersion(ctx context.Context, tx tagsql.Tx, opts FinishCopyObject) (_ Object, err error) {
|
2022-05-17 16:25:48 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2022-03-23 22:00:40 +00:00
|
|
|
|
2023-10-17 16:19:44 +01:00
|
|
|
if err := opts.Verify(); err != nil {
|
|
|
|
return Object{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(ver): should we allow copying delete markers?
|
|
|
|
object := Object{}
|
|
|
|
err = tx.QueryRowContext(ctx, `
|
2022-05-17 16:25:48 +01:00
|
|
|
SELECT
|
2023-10-17 16:19:44 +01:00
|
|
|
stream_id, status,
|
|
|
|
created_at, expires_at,
|
2022-05-17 16:25:48 +01:00
|
|
|
segment_count,
|
2023-10-17 16:19:44 +01:00
|
|
|
encrypted_metadata_nonce, encrypted_metadata, encrypted_metadata_encrypted_key,
|
2022-05-17 16:25:48 +01:00
|
|
|
total_plain_size, total_encrypted_size, fixed_segment_size,
|
2023-10-17 16:19:44 +01:00
|
|
|
encryption
|
2022-05-17 16:25:48 +01:00
|
|
|
FROM objects
|
|
|
|
WHERE
|
2023-10-17 16:19:44 +01:00
|
|
|
(project_id, bucket_name, object_key, version) = ($1, $2, $3, $4) AND
|
|
|
|
status IN `+statusesCommitted+` AND
|
|
|
|
(expires_at IS NULL OR expires_at > now())`,
|
|
|
|
opts.ProjectID, []byte(opts.BucketName), opts.ObjectKey, opts.Version).
|
|
|
|
Scan(
|
|
|
|
&object.StreamID, &object.Status,
|
|
|
|
&object.CreatedAt, &object.ExpiresAt,
|
|
|
|
&object.SegmentCount,
|
|
|
|
&object.EncryptedMetadataNonce, &object.EncryptedMetadata, &object.EncryptedMetadataEncryptedKey,
|
|
|
|
&object.TotalPlainSize, &object.TotalEncryptedSize, &object.FixedSegmentSize,
|
|
|
|
encryptionParameters{&object.Encryption},
|
2022-05-17 16:25:48 +01:00
|
|
|
)
|
2023-10-17 16:19:44 +01:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return Object{}, ErrObjectNotFound.Wrap(Error.Wrap(err))
|
2022-03-23 22:00:40 +00:00
|
|
|
}
|
2023-10-17 16:19:44 +01:00
|
|
|
return Object{}, Error.New("unable to query object status: %w", err)
|
2022-03-23 22:00:40 +00:00
|
|
|
}
|
2022-05-17 16:25:48 +01:00
|
|
|
|
2023-10-17 16:19:44 +01:00
|
|
|
object.ProjectID = opts.ProjectID
|
|
|
|
object.BucketName = opts.BucketName
|
|
|
|
object.ObjectKey = opts.ObjectKey
|
|
|
|
object.Version = opts.Version
|
2022-05-17 16:25:48 +01:00
|
|
|
|
2023-10-17 16:19:44 +01:00
|
|
|
return object, nil
|
2022-03-23 22:00:40 +00:00
|
|
|
}
|