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"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
2022-06-02 12:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/internal/misc"
|
2022-05-24 13:07:47 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-04-25 09:57:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
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 {
|
|
|
|
UploadID string
|
|
|
|
Bkt *data.BucketInfo
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
2022-05-23 14:34:13 +00:00
|
|
|
CreateMultipartParams struct {
|
2022-05-24 14:55:56 +00:00
|
|
|
Info *UploadInfoParams
|
|
|
|
Header map[string]string
|
|
|
|
Data *UploadData
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
Info *UploadInfoParams
|
|
|
|
PartNumber int
|
|
|
|
Size int64
|
|
|
|
Reader io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
UploadCopyParams struct {
|
|
|
|
Info *UploadInfoParams
|
|
|
|
SrcObjInfo *data.ObjectInfo
|
2022-06-02 16:56:04 +00:00
|
|
|
SrcBktInfo *data.BucketInfo
|
2021-11-25 15:05:58 +00:00
|
|
|
PartNumber int
|
|
|
|
Range *RangeParams
|
|
|
|
}
|
|
|
|
|
|
|
|
CompleteMultipartParams struct {
|
|
|
|
Info *UploadInfoParams
|
|
|
|
Parts []*CompletedPart
|
|
|
|
}
|
|
|
|
|
|
|
|
CompletedPart struct {
|
|
|
|
ETag string
|
|
|
|
PartNumber int
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-24 08:41:10 +00:00
|
|
|
Key: p.Info.Key,
|
2022-05-23 14:34:13 +00:00
|
|
|
UploadID: p.Info.UploadID,
|
|
|
|
Owner: n.Owner(ctx),
|
|
|
|
Created: time.Now(),
|
2022-05-24 14:55:56 +00:00
|
|
|
Meta: make(map[string]string, metaSize),
|
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-05-24 11:30:37 +00:00
|
|
|
return n.treeService.CreateMultipartUpload(ctx, &p.Info.Bkt.CID, 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) {
|
|
|
|
multipartInfo, err := n.treeService.GetMultipartUpload(ctx, &p.Info.Bkt.CID, p.Info.Key, p.Info.UploadID)
|
|
|
|
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) {
|
|
|
|
bktInfo := p.Info.Bkt
|
|
|
|
prm := PrmObjectCreate{
|
|
|
|
Container: bktInfo.CID,
|
|
|
|
Creator: bktInfo.Owner,
|
|
|
|
Attributes: make([][2]string, 2),
|
|
|
|
Payload: p.Reader,
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
partInfo := &data.PartInfo{
|
|
|
|
Key: p.Info.Key,
|
|
|
|
UploadID: p.Info.UploadID,
|
|
|
|
Number: p.PartNumber,
|
|
|
|
OID: *id,
|
2022-05-31 12:38:06 +00:00
|
|
|
Size: p.Size,
|
|
|
|
ETag: hex.EncodeToString(hash),
|
|
|
|
Created: time.Now(),
|
2022-05-24 11:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
oldPartID, err := n.treeService.AddPart(ctx, &bktInfo.CID, multipartInfo.ID, partInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if oldPartID != nil {
|
|
|
|
if err = n.objectDelete(ctx, bktInfo, *oldPartID); err != nil {
|
|
|
|
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{
|
|
|
|
ID: *id,
|
|
|
|
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-05-24 11:30:37 +00:00
|
|
|
multipartInfo, err := n.treeService.GetMultipartUpload(ctx, &p.Info.Bkt.CID, p.Info.Key, p.Info.UploadID)
|
|
|
|
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{
|
|
|
|
Info: p.Info,
|
|
|
|
PartNumber: p.PartNumber,
|
|
|
|
Size: size,
|
|
|
|
Reader: pr,
|
|
|
|
}
|
|
|
|
|
|
|
|
return n.uploadPart(ctx, multipartInfo, params)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 16:09:02 +00:00
|
|
|
// implements io.Reader of payloads of the object list stored in the NeoFS network.
|
|
|
|
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-05-24 14:55:56 +00:00
|
|
|
func (n *layer) CompleteMultipartUpload(ctx context.Context, p *CompleteMultipartParams) (*UploadData, *data.ObjectInfo, 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-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-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-06-24 09:01:39 +00:00
|
|
|
multipartObjetSize += partInfo.Size
|
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-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-05-24 14:55:56 +00:00
|
|
|
obj, err := n.PutObject(ctx, &PutObjectParams{
|
2022-03-18 13:04:09 +00:00
|
|
|
BktInfo: p.Info.Bkt,
|
|
|
|
Object: p.Info.Key,
|
|
|
|
Reader: r,
|
|
|
|
Header: initMetadata,
|
2022-06-24 09:01:39 +00:00
|
|
|
Size: multipartObjetSize,
|
2022-03-02 16:09:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
n.log.Error("could not put a completed object (multipart upload)",
|
2022-06-02 12:09:00 +00:00
|
|
|
zap.String("uploadID", misc.SanitizeString(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),
|
|
|
|
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-05-24 14:55:56 +00:00
|
|
|
n.objCache.Delete(addr)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 14:55:56 +00:00
|
|
|
return uploadData, obj, n.treeService.DeleteMultipartUpload(ctx, &p.Info.Bkt.CID, 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-05-24 08:41:10 +00:00
|
|
|
multipartInfos, err := n.treeService.GetMultipartUploadsByPrefix(ctx, &p.Bkt.CID, 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 {
|
|
|
|
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()),
|
|
|
|
zap.String("oid", info.OID.EncodeToString()), zap.Int("part number", info.Number))
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 13:27:15 +00:00
|
|
|
return n.treeService.DeleteMultipartUpload(ctx, &p.Bkt.CID, 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-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 {
|
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-05-24 13:07:47 +00:00
|
|
|
multipartInfo, err := n.treeService.GetMultipartUpload(ctx, &p.Bkt.CID, p.Key, p.UploadID)
|
|
|
|
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-05-24 13:07:47 +00:00
|
|
|
parts, err := n.treeService.GetParts(ctx, &p.Bkt.CID, 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-05-24 13:07:47 +00:00
|
|
|
for _, part := range parts {
|
2022-05-31 12:38:06 +00:00
|
|
|
res[part.Number] = part
|
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
|
|
|
}
|
|
|
|
}
|