2021-11-25 15:05:58 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-24 11:30:37 +00:00
|
|
|
"encoding/hex"
|
2022-03-02 16:09:02 +00:00
|
|
|
stderrors "errors"
|
|
|
|
"fmt"
|
2021-11-25 15:05:58 +00:00
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-12-09 13:30:51 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api"
|
2022-12-14 16:58:00 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api/errors"
|
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api/layer/encryption"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/user"
|
2022-08-01 16:52:09 +00:00
|
|
|
"github.com/minio/sio"
|
2021-11-25 15:05:58 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
UploadIDAttributeName = "S3-Upload-Id"
|
|
|
|
UploadPartNumberAttributeName = "S3-Upload-Part-Number"
|
2022-06-02 15:26:26 +00:00
|
|
|
UploadCompletedParts = "S3-Completed-Parts"
|
2021-11-25 15:05:58 +00:00
|
|
|
|
2022-05-23 14:34:13 +00:00
|
|
|
metaPrefix = "meta-"
|
|
|
|
aclPrefix = "acl-"
|
|
|
|
|
2021-11-25 15:05:58 +00:00
|
|
|
MaxSizeUploadsList = 1000
|
|
|
|
MaxSizePartsList = 1000
|
|
|
|
UploadMinPartNumber = 1
|
|
|
|
UploadMaxPartNumber = 10000
|
|
|
|
uploadMinSize = 5 * 1048576 // 5MB
|
|
|
|
uploadMaxSize = 5 * 1073741824 // 5GB
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
UploadInfoParams struct {
|
2022-08-01 16:52:09 +00:00
|
|
|
UploadID string
|
|
|
|
Bkt *data.BucketInfo
|
|
|
|
Key string
|
2022-08-11 08:48:58 +00:00
|
|
|
Encryption encryption.Params
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 14:34:13 +00:00
|
|
|
CreateMultipartParams struct {
|
2022-08-17 11:18:36 +00:00
|
|
|
Info *UploadInfoParams
|
|
|
|
Header map[string]string
|
|
|
|
Data *UploadData
|
|
|
|
CopiesNumber uint32
|
2022-05-24 14:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UploadData struct {
|
2022-05-23 14:34:13 +00:00
|
|
|
TagSet map[string]string
|
|
|
|
ACLHeaders map[string]string
|
|
|
|
}
|
|
|
|
|
2021-11-25 15:05:58 +00:00
|
|
|
UploadPartParams struct {
|
2022-08-17 11:18:36 +00:00
|
|
|
Info *UploadInfoParams
|
|
|
|
PartNumber int
|
|
|
|
Size int64
|
|
|
|
Reader io.Reader
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UploadCopyParams struct {
|
2022-08-17 11:18:36 +00:00
|
|
|
Info *UploadInfoParams
|
|
|
|
SrcObjInfo *data.ObjectInfo
|
|
|
|
SrcBktInfo *data.BucketInfo
|
|
|
|
PartNumber int
|
|
|
|
Range *RangeParams
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CompleteMultipartParams struct {
|
|
|
|
Info *UploadInfoParams
|
|
|
|
Parts []*CompletedPart
|
|
|
|
}
|
|
|
|
|
|
|
|
CompletedPart struct {
|
|
|
|
ETag string
|
|
|
|
PartNumber int
|
|
|
|
}
|
|
|
|
|
2022-08-01 16:52:09 +00:00
|
|
|
EncryptedPart struct {
|
|
|
|
Part
|
|
|
|
EncryptedSize int64
|
|
|
|
}
|
|
|
|
|
2021-11-25 15:05:58 +00:00
|
|
|
Part struct {
|
|
|
|
ETag string
|
|
|
|
LastModified string
|
|
|
|
PartNumber int
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
|
|
|
|
ListMultipartUploadsParams struct {
|
|
|
|
Bkt *data.BucketInfo
|
|
|
|
Delimiter string
|
|
|
|
EncodingType string
|
|
|
|
KeyMarker string
|
|
|
|
MaxUploads int
|
|
|
|
Prefix string
|
|
|
|
UploadIDMarker string
|
|
|
|
}
|
|
|
|
|
|
|
|
ListPartsParams struct {
|
|
|
|
Info *UploadInfoParams
|
|
|
|
MaxParts int
|
|
|
|
PartNumberMarker int
|
|
|
|
}
|
|
|
|
|
|
|
|
ListPartsInfo struct {
|
|
|
|
Parts []*Part
|
2022-05-25 17:25:43 +00:00
|
|
|
Owner user.ID
|
2021-11-25 15:05:58 +00:00
|
|
|
NextPartNumberMarker int
|
|
|
|
IsTruncated bool
|
|
|
|
}
|
|
|
|
|
|
|
|
ListMultipartUploadsInfo struct {
|
|
|
|
Prefixes []string
|
|
|
|
Uploads []*UploadInfo
|
|
|
|
IsTruncated bool
|
|
|
|
NextKeyMarker string
|
|
|
|
NextUploadIDMarker string
|
|
|
|
}
|
|
|
|
UploadInfo struct {
|
|
|
|
IsDir bool
|
|
|
|
Key string
|
|
|
|
UploadID string
|
2022-05-25 17:25:43 +00:00
|
|
|
Owner user.ID
|
2021-11-25 15:05:58 +00:00
|
|
|
Created time.Time
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-05-23 14:34:13 +00:00
|
|
|
func (n *layer) CreateMultipartUpload(ctx context.Context, p *CreateMultipartParams) error {
|
2022-05-24 14:55:56 +00:00
|
|
|
metaSize := len(p.Header)
|
|
|
|
if p.Data != nil {
|
|
|
|
metaSize += len(p.Data.ACLHeaders)
|
|
|
|
metaSize += len(p.Data.TagSet)
|
|
|
|
}
|
|
|
|
|
2022-05-23 14:34:13 +00:00
|
|
|
info := &data.MultipartInfo{
|
2022-08-17 11:18:36 +00:00
|
|
|
Key: p.Info.Key,
|
|
|
|
UploadID: p.Info.UploadID,
|
|
|
|
Owner: n.Owner(ctx),
|
2022-11-08 09:12:55 +00:00
|
|
|
Created: TimeNow(ctx),
|
2022-08-17 11:18:36 +00:00
|
|
|
Meta: make(map[string]string, metaSize),
|
|
|
|
CopiesNumber: p.CopiesNumber,
|
2022-05-23 14:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range p.Header {
|
|
|
|
info.Meta[metaPrefix+key] = val
|
|
|
|
}
|
|
|
|
|
2022-05-24 14:55:56 +00:00
|
|
|
if p.Data != nil {
|
|
|
|
for key, val := range p.Data.ACLHeaders {
|
|
|
|
info.Meta[aclPrefix+key] = val
|
|
|
|
}
|
2022-05-23 14:34:13 +00:00
|
|
|
|
2022-05-24 14:55:56 +00:00
|
|
|
for key, val := range p.Data.TagSet {
|
|
|
|
info.Meta[tagPrefix+key] = val
|
|
|
|
}
|
2022-05-23 14:34:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 16:52:09 +00:00
|
|
|
if p.Info.Encryption.Enabled() {
|
|
|
|
if err := addEncryptionHeaders(info.Meta, p.Info.Encryption); err != nil {
|
|
|
|
return fmt.Errorf("add encryption header: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
return n.treeService.CreateMultipartUpload(ctx, p.Info.Bkt, info)
|
2022-05-23 14:34:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 11:30:37 +00:00
|
|
|
func (n *layer) UploadPart(ctx context.Context, p *UploadPartParams) (string, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
multipartInfo, err := n.treeService.GetMultipartUpload(ctx, p.Info.Bkt, p.Info.Key, p.Info.UploadID)
|
2022-05-24 11:30:37 +00:00
|
|
|
if err != nil {
|
|
|
|
if stderrors.Is(err, ErrNodeNotFound) {
|
|
|
|
return "", errors.GetAPIError(errors.ErrNoSuchUpload)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
2022-05-24 11:30:37 +00:00
|
|
|
return "", err
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.Size > uploadMaxSize {
|
2022-05-24 11:30:37 +00:00
|
|
|
return "", errors.GetAPIError(errors.ErrEntityTooLarge)
|
|
|
|
}
|
|
|
|
|
|
|
|
objInfo, err := n.uploadPart(ctx, multipartInfo, p)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 11:30:37 +00:00
|
|
|
return objInfo.HashSum, nil
|
|
|
|
}
|
2021-11-25 15:05:58 +00:00
|
|
|
|
2022-05-24 11:30:37 +00:00
|
|
|
func (n *layer) uploadPart(ctx context.Context, multipartInfo *data.MultipartInfo, p *UploadPartParams) (*data.ObjectInfo, error) {
|
2022-08-11 08:48:58 +00:00
|
|
|
encInfo := FormEncryptionInfo(multipartInfo.Meta)
|
2022-08-01 16:52:09 +00:00
|
|
|
if err := p.Info.Encryption.MatchObjectEncryption(encInfo); err != nil {
|
|
|
|
n.log.Warn("mismatched obj encryptionInfo", zap.Error(err))
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidEncryptionParameters)
|
|
|
|
}
|
|
|
|
|
2022-05-24 11:30:37 +00:00
|
|
|
bktInfo := p.Info.Bkt
|
|
|
|
prm := PrmObjectCreate{
|
2022-08-11 22:48:56 +00:00
|
|
|
Container: bktInfo.CID,
|
|
|
|
Creator: bktInfo.Owner,
|
|
|
|
Attributes: make([][2]string, 2),
|
|
|
|
Payload: p.Reader,
|
2022-11-08 09:12:55 +00:00
|
|
|
CreationTime: TimeNow(ctx),
|
2022-08-17 11:18:36 +00:00
|
|
|
CopiesNumber: multipartInfo.CopiesNumber,
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 16:52:09 +00:00
|
|
|
decSize := p.Size
|
|
|
|
if p.Info.Encryption.Enabled() {
|
|
|
|
r, encSize, err := encryptionReader(p.Reader, uint64(p.Size), p.Info.Encryption.Key())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create ecnrypted reader: %w", err)
|
|
|
|
}
|
|
|
|
prm.Attributes = append(prm.Attributes, [2]string{AttributeDecryptedSize, strconv.FormatInt(p.Size, 10)})
|
|
|
|
prm.Payload = r
|
|
|
|
p.Size = int64(encSize)
|
|
|
|
}
|
|
|
|
|
2022-05-24 11:30:37 +00:00
|
|
|
prm.Attributes[0][0], prm.Attributes[0][1] = UploadIDAttributeName, p.Info.UploadID
|
|
|
|
prm.Attributes[1][0], prm.Attributes[1][1] = UploadPartNumberAttributeName, strconv.Itoa(p.PartNumber)
|
|
|
|
|
|
|
|
id, hash, err := n.objectPutAndHash(ctx, prm, bktInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:30:51 +00:00
|
|
|
reqInfo := api.GetReqInfo(ctx)
|
|
|
|
n.log.Debug("upload part",
|
|
|
|
zap.String("reqId", reqInfo.RequestID),
|
|
|
|
zap.String("bucket", bktInfo.Name), zap.Stringer("cid", bktInfo.CID),
|
|
|
|
zap.String("multipart upload", p.Info.UploadID),
|
|
|
|
zap.Int("part number", p.PartNumber), zap.String("object", p.Info.Key), zap.Stringer("oid", id))
|
|
|
|
|
2022-05-24 11:30:37 +00:00
|
|
|
partInfo := &data.PartInfo{
|
|
|
|
Key: p.Info.Key,
|
|
|
|
UploadID: p.Info.UploadID,
|
|
|
|
Number: p.PartNumber,
|
2022-06-27 09:33:36 +00:00
|
|
|
OID: id,
|
2022-08-01 16:52:09 +00:00
|
|
|
Size: decSize,
|
2022-05-31 12:38:06 +00:00
|
|
|
ETag: hex.EncodeToString(hash),
|
2022-11-08 09:12:55 +00:00
|
|
|
Created: prm.CreationTime,
|
2022-05-24 11:30:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
oldPartID, err := n.treeService.AddPart(ctx, bktInfo, multipartInfo.ID, partInfo)
|
2022-06-27 09:33:36 +00:00
|
|
|
oldPartIDNotFound := stderrors.Is(err, ErrNoNodeToRemove)
|
|
|
|
if err != nil && !oldPartIDNotFound {
|
2022-05-24 11:30:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-27 09:33:36 +00:00
|
|
|
if !oldPartIDNotFound {
|
|
|
|
if err = n.objectDelete(ctx, bktInfo, oldPartID); err != nil {
|
2022-05-24 11:30:37 +00:00
|
|
|
n.log.Error("couldn't delete old part object", zap.Error(err),
|
|
|
|
zap.String("cnrID", bktInfo.CID.EncodeToString()),
|
|
|
|
zap.String("bucket name", bktInfo.Name),
|
|
|
|
zap.String("objID", oldPartID.EncodeToString()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
objInfo := &data.ObjectInfo{
|
2022-06-27 09:33:36 +00:00
|
|
|
ID: id,
|
2022-05-24 11:30:37 +00:00
|
|
|
CID: bktInfo.CID,
|
|
|
|
|
|
|
|
Owner: bktInfo.Owner,
|
|
|
|
Bucket: bktInfo.Name,
|
2022-05-31 12:38:06 +00:00
|
|
|
Size: partInfo.Size,
|
|
|
|
Created: partInfo.Created,
|
|
|
|
HashSum: partInfo.ETag,
|
2022-05-24 11:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return objInfo, nil
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) UploadPartCopy(ctx context.Context, p *UploadCopyParams) (*data.ObjectInfo, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
multipartInfo, err := n.treeService.GetMultipartUpload(ctx, p.Info.Bkt, p.Info.Key, p.Info.UploadID)
|
2022-05-24 11:30:37 +00:00
|
|
|
if err != nil {
|
|
|
|
if stderrors.Is(err, ErrNodeNotFound) {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrNoSuchUpload)
|
|
|
|
}
|
2021-11-25 15:05:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-25 15:59:36 +00:00
|
|
|
size := p.SrcObjInfo.Size
|
2021-11-25 15:05:58 +00:00
|
|
|
if p.Range != nil {
|
2022-05-25 15:59:36 +00:00
|
|
|
size = int64(p.Range.End - p.Range.Start + 1)
|
2021-11-25 15:05:58 +00:00
|
|
|
if p.Range.End > uint64(p.SrcObjInfo.Size) {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidCopyPartRangeSource)
|
|
|
|
}
|
2022-05-25 15:59:36 +00:00
|
|
|
}
|
|
|
|
if size > uploadMaxSize {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrEntityTooLarge)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 12:16:01 +00:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
|
|
|
|
go func() {
|
2022-05-24 11:30:37 +00:00
|
|
|
err = n.GetObject(ctx, &GetObjectParams{
|
2022-04-18 12:16:01 +00:00
|
|
|
ObjectInfo: p.SrcObjInfo,
|
|
|
|
Writer: pw,
|
|
|
|
Range: p.Range,
|
2022-06-02 16:56:04 +00:00
|
|
|
BucketInfo: p.SrcBktInfo,
|
2022-04-18 12:16:01 +00:00
|
|
|
})
|
2021-11-25 15:05:58 +00:00
|
|
|
|
2022-04-18 12:16:01 +00:00
|
|
|
if err = pw.CloseWithError(err); err != nil {
|
|
|
|
n.log.Error("could not get object", zap.Error(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-05-24 11:30:37 +00:00
|
|
|
params := &UploadPartParams{
|
2022-08-17 11:18:36 +00:00
|
|
|
Info: p.Info,
|
|
|
|
PartNumber: p.PartNumber,
|
|
|
|
Size: size,
|
|
|
|
Reader: pr,
|
2022-05-24 11:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n.uploadPart(ctx, multipartInfo, params)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 08:38:58 +00:00
|
|
|
// implements io.Reader of payloads of the object list stored in the FrostFS network.
|
2022-03-02 16:09:02 +00:00
|
|
|
type multiObjectReader struct {
|
|
|
|
ctx context.Context
|
|
|
|
|
|
|
|
layer *layer
|
|
|
|
|
|
|
|
prm getParams
|
|
|
|
|
|
|
|
curReader io.Reader
|
|
|
|
|
2022-05-31 12:38:06 +00:00
|
|
|
parts []*data.PartInfo
|
2022-03-02 16:09:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *multiObjectReader) Read(p []byte) (n int, err error) {
|
|
|
|
if x.curReader != nil {
|
|
|
|
n, err = x.curReader.Read(p)
|
|
|
|
if !stderrors.Is(err, io.EOF) {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(x.parts) == 0 {
|
|
|
|
return n, io.EOF
|
|
|
|
}
|
|
|
|
|
2022-05-31 12:38:06 +00:00
|
|
|
x.prm.oid = x.parts[0].OID
|
2022-03-02 16:09:02 +00:00
|
|
|
|
|
|
|
x.curReader, err = x.layer.initObjectPayloadReader(x.ctx, x.prm)
|
|
|
|
if err != nil {
|
|
|
|
return n, fmt.Errorf("init payload reader for the next part: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
x.parts = x.parts[1:]
|
|
|
|
|
|
|
|
next, err := x.Read(p[n:])
|
|
|
|
|
|
|
|
return n + next, err
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:36:43 +00:00
|
|
|
func (n *layer) CompleteMultipartUpload(ctx context.Context, p *CompleteMultipartParams) (*UploadData, *data.ExtendedObjectInfo, error) {
|
2021-11-25 15:05:58 +00:00
|
|
|
for i := 1; i < len(p.Parts); i++ {
|
|
|
|
if p.Parts[i].PartNumber <= p.Parts[i-1].PartNumber {
|
2022-05-24 14:55:56 +00:00
|
|
|
return nil, nil, errors.GetAPIError(errors.ErrInvalidPartOrder)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 12:38:06 +00:00
|
|
|
multipartInfo, partsInfo, err := n.getUploadParts(ctx, p.Info)
|
2021-11-25 15:05:58 +00:00
|
|
|
if err != nil {
|
2022-05-24 14:55:56 +00:00
|
|
|
return nil, nil, err
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
2022-08-11 08:48:58 +00:00
|
|
|
encInfo := FormEncryptionInfo(multipartInfo.Meta)
|
2021-11-25 15:05:58 +00:00
|
|
|
|
2022-05-31 12:38:06 +00:00
|
|
|
if len(partsInfo) < len(p.Parts) {
|
2022-05-24 14:55:56 +00:00
|
|
|
return nil, nil, errors.GetAPIError(errors.ErrInvalidPart)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 09:01:39 +00:00
|
|
|
var multipartObjetSize int64
|
2022-08-01 16:52:09 +00:00
|
|
|
var encMultipartObjectSize uint64
|
2022-05-31 12:38:06 +00:00
|
|
|
parts := make([]*data.PartInfo, 0, len(p.Parts))
|
2021-11-25 15:05:58 +00:00
|
|
|
|
2022-06-02 15:26:26 +00:00
|
|
|
var completedPartsHeader strings.Builder
|
2021-11-25 15:05:58 +00:00
|
|
|
for i, part := range p.Parts {
|
2022-05-31 12:38:06 +00:00
|
|
|
partInfo := partsInfo[part.PartNumber]
|
2022-06-07 12:33:23 +00:00
|
|
|
if partInfo == nil || part.ETag != partInfo.ETag {
|
2022-05-24 14:55:56 +00:00
|
|
|
return nil, nil, errors.GetAPIError(errors.ErrInvalidPart)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
// for the last part we have no minimum size limit
|
2022-05-31 12:38:06 +00:00
|
|
|
if i != len(p.Parts)-1 && partInfo.Size < uploadMinSize {
|
2022-05-24 14:55:56 +00:00
|
|
|
return nil, nil, errors.GetAPIError(errors.ErrEntityTooSmall)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
2022-05-31 12:38:06 +00:00
|
|
|
parts = append(parts, partInfo)
|
2022-08-01 16:52:09 +00:00
|
|
|
multipartObjetSize += partInfo.Size // even if encryption is enabled size is actual (decrypted)
|
|
|
|
|
|
|
|
if encInfo.Enabled {
|
|
|
|
encPartSize, err := sio.EncryptedSize(uint64(partInfo.Size))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("compute encrypted size: %w", err)
|
|
|
|
}
|
|
|
|
encMultipartObjectSize += encPartSize
|
|
|
|
}
|
2022-06-02 15:26:26 +00:00
|
|
|
|
|
|
|
partInfoStr := partInfo.ToHeaderString()
|
|
|
|
if i != len(p.Parts)-1 {
|
|
|
|
partInfoStr += ","
|
|
|
|
}
|
|
|
|
if _, err = completedPartsHeader.WriteString(partInfoStr); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 09:54:27 +00:00
|
|
|
initMetadata := make(map[string]string, len(multipartInfo.Meta)+1)
|
2022-06-02 15:26:26 +00:00
|
|
|
initMetadata[UploadCompletedParts] = completedPartsHeader.String()
|
|
|
|
|
2022-05-24 14:55:56 +00:00
|
|
|
uploadData := &UploadData{
|
|
|
|
TagSet: make(map[string]string),
|
|
|
|
ACLHeaders: make(map[string]string),
|
|
|
|
}
|
|
|
|
for key, val := range multipartInfo.Meta {
|
|
|
|
if strings.HasPrefix(key, metaPrefix) {
|
|
|
|
initMetadata[strings.TrimPrefix(key, metaPrefix)] = val
|
|
|
|
} else if strings.HasPrefix(key, tagPrefix) {
|
|
|
|
uploadData.TagSet[strings.TrimPrefix(key, tagPrefix)] = val
|
|
|
|
} else if strings.HasPrefix(key, aclPrefix) {
|
|
|
|
uploadData.ACLHeaders[strings.TrimPrefix(key, aclPrefix)] = val
|
|
|
|
}
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
2022-05-12 21:15:08 +00:00
|
|
|
|
2022-08-01 16:52:09 +00:00
|
|
|
if encInfo.Enabled {
|
|
|
|
initMetadata[AttributeEncryptionAlgorithm] = encInfo.Algorithm
|
|
|
|
initMetadata[AttributeHMACKey] = encInfo.HMACKey
|
|
|
|
initMetadata[AttributeHMACSalt] = encInfo.HMACSalt
|
|
|
|
initMetadata[AttributeDecryptedSize] = strconv.FormatInt(multipartObjetSize, 10)
|
|
|
|
multipartObjetSize = int64(encMultipartObjectSize)
|
|
|
|
}
|
|
|
|
|
2022-03-02 16:09:02 +00:00
|
|
|
r := &multiObjectReader{
|
|
|
|
ctx: ctx,
|
|
|
|
layer: n,
|
|
|
|
parts: parts,
|
|
|
|
}
|
2022-02-08 16:54:04 +00:00
|
|
|
|
2022-06-02 16:56:04 +00:00
|
|
|
r.prm.bktInfo = p.Info.Bkt
|
|
|
|
|
2022-10-14 14:36:43 +00:00
|
|
|
extObjInfo, err := n.PutObject(ctx, &PutObjectParams{
|
2022-08-17 11:18:36 +00:00
|
|
|
BktInfo: p.Info.Bkt,
|
|
|
|
Object: p.Info.Key,
|
|
|
|
Reader: r,
|
|
|
|
Header: initMetadata,
|
|
|
|
Size: multipartObjetSize,
|
|
|
|
Encryption: p.Info.Encryption,
|
|
|
|
CopiesNumber: multipartInfo.CopiesNumber,
|
2022-03-02 16:09:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
n.log.Error("could not put a completed object (multipart upload)",
|
2022-07-27 15:02:52 +00:00
|
|
|
zap.String("uploadID", p.Info.UploadID),
|
2022-03-02 16:09:02 +00:00
|
|
|
zap.String("uploadKey", p.Info.Key),
|
|
|
|
zap.Error(err))
|
2021-11-25 15:05:58 +00:00
|
|
|
|
2022-05-24 14:55:56 +00:00
|
|
|
return nil, nil, errors.GetAPIError(errors.ErrInternalError)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 14:55:56 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(p.Info.Bkt.CID)
|
2022-05-31 12:38:06 +00:00
|
|
|
for _, partInfo := range partsInfo {
|
|
|
|
if err = n.objectDelete(ctx, p.Info.Bkt, partInfo.OID); err != nil {
|
2021-11-25 15:05:58 +00:00
|
|
|
n.log.Warn("could not delete upload part",
|
2022-05-31 12:38:06 +00:00
|
|
|
zap.Stringer("object id", &partInfo.OID),
|
2022-06-27 09:08:26 +00:00
|
|
|
zap.Stringer("bucket id", p.Info.Bkt.CID),
|
2021-11-25 15:05:58 +00:00
|
|
|
zap.Error(err))
|
|
|
|
}
|
2022-05-31 12:38:06 +00:00
|
|
|
addr.SetObject(partInfo.OID)
|
2022-10-03 14:33:49 +00:00
|
|
|
n.cache.DeleteObject(addr)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:36:43 +00:00
|
|
|
return uploadData, extObjInfo, n.treeService.DeleteMultipartUpload(ctx, p.Info.Bkt, multipartInfo.ID)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) ListMultipartUploads(ctx context.Context, p *ListMultipartUploadsParams) (*ListMultipartUploadsInfo, error) {
|
|
|
|
var result ListMultipartUploadsInfo
|
|
|
|
if p.MaxUploads == 0 {
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
multipartInfos, err := n.treeService.GetMultipartUploadsByPrefix(ctx, p.Bkt, p.Prefix)
|
2021-11-25 15:05:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-24 08:41:10 +00:00
|
|
|
uploads := make([]*UploadInfo, 0, len(multipartInfos))
|
2021-11-25 15:05:58 +00:00
|
|
|
uniqDirs := make(map[string]struct{})
|
|
|
|
|
2022-05-24 08:41:10 +00:00
|
|
|
for _, multipartInfo := range multipartInfos {
|
|
|
|
info := uploadInfoFromMultipartInfo(multipartInfo, p.Prefix, p.Delimiter)
|
2021-11-25 15:05:58 +00:00
|
|
|
if info != nil {
|
|
|
|
if info.IsDir {
|
|
|
|
if _, ok := uniqDirs[info.Key]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
uniqDirs[info.Key] = struct{}{}
|
|
|
|
}
|
|
|
|
uploads = append(uploads, info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(uploads, func(i, j int) bool {
|
|
|
|
if uploads[i].Key == uploads[j].Key {
|
|
|
|
return uploads[i].UploadID < uploads[j].UploadID
|
|
|
|
}
|
|
|
|
return uploads[i].Key < uploads[j].Key
|
|
|
|
})
|
|
|
|
|
|
|
|
if p.KeyMarker != "" {
|
|
|
|
if p.UploadIDMarker != "" {
|
|
|
|
uploads = trimAfterUploadIDAndKey(p.KeyMarker, p.UploadIDMarker, uploads)
|
|
|
|
} else {
|
|
|
|
uploads = trimAfterUploadKey(p.KeyMarker, uploads)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(uploads) > p.MaxUploads {
|
|
|
|
result.IsTruncated = true
|
|
|
|
uploads = uploads[:p.MaxUploads]
|
|
|
|
result.NextUploadIDMarker = uploads[len(uploads)-1].UploadID
|
|
|
|
result.NextKeyMarker = uploads[len(uploads)-1].Key
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ov := range uploads {
|
|
|
|
if ov.IsDir {
|
|
|
|
result.Prefixes = append(result.Prefixes, ov.Key)
|
|
|
|
} else {
|
|
|
|
result.Uploads = append(result.Uploads, ov)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) AbortMultipartUpload(ctx context.Context, p *UploadInfoParams) error {
|
2022-05-31 12:38:06 +00:00
|
|
|
multipartInfo, parts, err := n.getUploadParts(ctx, p)
|
2021-11-25 15:05:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-31 12:38:06 +00:00
|
|
|
for _, info := range parts {
|
2022-12-09 13:30:51 +00:00
|
|
|
reqInfo := api.GetReqInfo(ctx)
|
|
|
|
n.log.Debug("part details",
|
|
|
|
zap.String("reqId", reqInfo.RequestID),
|
|
|
|
zap.String("bucket", p.Bkt.Name), zap.Stringer("cid", p.Bkt.CID),
|
|
|
|
zap.String("object", info.Key), zap.Stringer("oid", info.OID))
|
2022-05-31 12:38:06 +00:00
|
|
|
if err = n.objectDelete(ctx, p.Bkt, info.OID); err != nil {
|
|
|
|
n.log.Warn("couldn't delete part", zap.String("cid", p.Bkt.CID.EncodeToString()),
|
2022-10-19 12:14:29 +00:00
|
|
|
zap.String("oid", info.OID.EncodeToString()), zap.Int("part number", info.Number), zap.Error(err))
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
return n.treeService.DeleteMultipartUpload(ctx, p.Bkt, multipartInfo.ID)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *layer) ListParts(ctx context.Context, p *ListPartsParams) (*ListPartsInfo, error) {
|
|
|
|
var res ListPartsInfo
|
2022-05-31 12:38:06 +00:00
|
|
|
multipartInfo, partsInfo, err := n.getUploadParts(ctx, p.Info)
|
2021-11-25 15:05:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-11 08:48:58 +00:00
|
|
|
encInfo := FormEncryptionInfo(multipartInfo.Meta)
|
2022-08-01 16:52:09 +00:00
|
|
|
if err = p.Info.Encryption.MatchObjectEncryption(encInfo); err != nil {
|
|
|
|
n.log.Warn("mismatched obj encryptionInfo", zap.Error(err))
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidEncryptionParameters)
|
|
|
|
}
|
|
|
|
|
2022-05-24 13:07:47 +00:00
|
|
|
res.Owner = multipartInfo.Owner
|
2021-11-25 15:05:58 +00:00
|
|
|
|
2022-05-31 12:38:06 +00:00
|
|
|
parts := make([]*Part, 0, len(partsInfo))
|
2021-11-25 15:05:58 +00:00
|
|
|
|
2022-05-31 12:38:06 +00:00
|
|
|
for _, partInfo := range partsInfo {
|
2022-12-09 13:30:51 +00:00
|
|
|
reqInfo := api.GetReqInfo(ctx)
|
|
|
|
n.log.Debug("part details",
|
|
|
|
zap.String("reqId", reqInfo.RequestID),
|
|
|
|
zap.String("container", p.Info.Bkt.Name), zap.Stringer("cid", p.Info.Bkt.CID),
|
|
|
|
zap.String("object", partInfo.Key), zap.Stringer("oid", partInfo.OID))
|
2021-11-25 15:05:58 +00:00
|
|
|
parts = append(parts, &Part{
|
2022-05-31 12:38:06 +00:00
|
|
|
ETag: partInfo.ETag,
|
|
|
|
LastModified: partInfo.Created.UTC().Format(time.RFC3339),
|
|
|
|
PartNumber: partInfo.Number,
|
|
|
|
Size: partInfo.Size,
|
2021-11-25 15:05:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(parts, func(i, j int) bool {
|
|
|
|
return parts[i].PartNumber < parts[j].PartNumber
|
|
|
|
})
|
|
|
|
|
|
|
|
if p.PartNumberMarker != 0 {
|
|
|
|
for i, part := range parts {
|
|
|
|
if part.PartNumber > p.PartNumberMarker {
|
|
|
|
parts = parts[i:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parts) > p.MaxParts {
|
|
|
|
res.IsTruncated = true
|
|
|
|
res.NextPartNumberMarker = parts[p.MaxParts-1].PartNumber
|
|
|
|
parts = parts[:p.MaxParts]
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Parts = parts
|
|
|
|
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 12:38:06 +00:00
|
|
|
func (n *layer) getUploadParts(ctx context.Context, p *UploadInfoParams) (*data.MultipartInfo, map[int]*data.PartInfo, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
multipartInfo, err := n.treeService.GetMultipartUpload(ctx, p.Bkt, p.Key, p.UploadID)
|
2022-05-24 13:07:47 +00:00
|
|
|
if err != nil {
|
|
|
|
if stderrors.Is(err, ErrNodeNotFound) {
|
|
|
|
return nil, nil, errors.GetAPIError(errors.ErrNoSuchUpload)
|
|
|
|
}
|
|
|
|
return nil, nil, err
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
parts, err := n.treeService.GetParts(ctx, p.Bkt, multipartInfo.ID)
|
2021-11-25 15:05:58 +00:00
|
|
|
if err != nil {
|
2022-05-24 13:07:47 +00:00
|
|
|
return nil, nil, err
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 12:38:06 +00:00
|
|
|
res := make(map[int]*data.PartInfo, len(parts))
|
2022-12-09 13:30:51 +00:00
|
|
|
partsNumbers := make([]int, len(parts))
|
|
|
|
oids := make([]string, len(parts))
|
|
|
|
for i, part := range parts {
|
2022-05-31 12:38:06 +00:00
|
|
|
res[part.Number] = part
|
2022-12-09 13:30:51 +00:00
|
|
|
partsNumbers[i] = part.Number
|
|
|
|
oids[i] = part.OID.EncodeToString()
|
|
|
|
}
|
|
|
|
|
|
|
|
reqInfo := api.GetReqInfo(ctx)
|
|
|
|
n.log.Debug("part details",
|
|
|
|
zap.String("reqId", reqInfo.RequestID),
|
|
|
|
zap.String("bucket", p.Bkt.Name),
|
|
|
|
zap.Stringer("cid", p.Bkt.CID),
|
|
|
|
zap.String("object", p.Key),
|
|
|
|
zap.String("upload id", p.UploadID),
|
|
|
|
zap.Ints("part numbers", partsNumbers),
|
|
|
|
zap.Strings("oids", oids))
|
2022-04-18 12:16:01 +00:00
|
|
|
|
2022-05-24 13:07:47 +00:00
|
|
|
return multipartInfo, res, nil
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func trimAfterUploadIDAndKey(key, id string, uploads []*UploadInfo) []*UploadInfo {
|
|
|
|
var res []*UploadInfo
|
|
|
|
if len(uploads) != 0 && uploads[len(uploads)-1].Key < key {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, obj := range uploads {
|
|
|
|
if obj.Key >= key && obj.UploadID > id {
|
|
|
|
res = append(res, obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func trimAfterUploadKey(key string, objects []*UploadInfo) []*UploadInfo {
|
|
|
|
var result []*UploadInfo
|
|
|
|
if len(objects) != 0 && objects[len(objects)-1].Key <= key {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
for i, obj := range objects {
|
|
|
|
if obj.Key > key {
|
|
|
|
result = objects[i:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-05-24 08:41:10 +00:00
|
|
|
func uploadInfoFromMultipartInfo(uploadInfo *data.MultipartInfo, prefix, delimiter string) *UploadInfo {
|
|
|
|
var isDir bool
|
|
|
|
key := uploadInfo.Key
|
2021-11-25 15:05:58 +00:00
|
|
|
|
|
|
|
if !strings.HasPrefix(key, prefix) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(delimiter) > 0 {
|
|
|
|
tail := strings.TrimPrefix(key, prefix)
|
|
|
|
index := strings.Index(tail, delimiter)
|
|
|
|
if index >= 0 {
|
|
|
|
isDir = true
|
|
|
|
key = prefix + tail[:index+1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &UploadInfo{
|
|
|
|
IsDir: isDir,
|
|
|
|
Key: key,
|
2022-05-24 08:41:10 +00:00
|
|
|
UploadID: uploadInfo.UploadID,
|
|
|
|
Owner: uploadInfo.Owner,
|
|
|
|
Created: uploadInfo.Created,
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
}
|