forked from TrueCloudLab/frostfs-s3-gw
[#122] Add versioning put object
Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
parent
feb45d0633
commit
f463522f34
3 changed files with 90 additions and 31 deletions
|
@ -3,7 +3,6 @@ package handler
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/nspcc-dev/neofs-s3-gw/api"
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
||||||
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
||||||
|
@ -21,7 +20,7 @@ func (h *handler) PutBucketVersioningHandler(w http.ResponseWriter, r *http.Requ
|
||||||
|
|
||||||
p := &layer.PutVersioningParams{
|
p := &layer.PutVersioningParams{
|
||||||
Bucket: reqInfo.BucketName,
|
Bucket: reqInfo.BucketName,
|
||||||
VersioningEnabled: configuration.Status == "Enabled",
|
Settings: &layer.BucketSettings{VersioningEnabled: configuration.Status == "Enabled"},
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := h.obj.PutBucketVersioning(r.Context(), p); err != nil {
|
if _, err := h.obj.PutBucketVersioning(r.Context(), p); err != nil {
|
||||||
|
@ -33,7 +32,7 @@ func (h *handler) PutBucketVersioningHandler(w http.ResponseWriter, r *http.Requ
|
||||||
func (h *handler) GetBucketVersioningHandler(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) GetBucketVersioningHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
reqInfo := api.GetReqInfo(r.Context())
|
reqInfo := api.GetReqInfo(r.Context())
|
||||||
|
|
||||||
objInfo, err := h.obj.GetBucketVersioning(r.Context(), reqInfo.BucketName)
|
settings, err := h.obj.GetBucketVersioning(r.Context(), reqInfo.BucketName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.log.Warn("couldn't get version settings object: default version settings will be used",
|
h.log.Warn("couldn't get version settings object: default version settings will be used",
|
||||||
zap.String("request_id", reqInfo.RequestID),
|
zap.String("request_id", reqInfo.RequestID),
|
||||||
|
@ -42,23 +41,18 @@ func (h *handler) GetBucketVersioningHandler(w http.ResponseWriter, r *http.Requ
|
||||||
zap.Error(err))
|
zap.Error(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = api.EncodeToResponse(w, formVersioningConfiguration(objInfo)); err != nil {
|
if err = api.EncodeToResponse(w, formVersioningConfiguration(settings)); err != nil {
|
||||||
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func formVersioningConfiguration(inf *layer.ObjectInfo) *VersioningConfiguration {
|
func formVersioningConfiguration(settings *layer.BucketSettings) *VersioningConfiguration {
|
||||||
res := &VersioningConfiguration{Status: "Suspended"}
|
res := &VersioningConfiguration{Status: "Suspended"}
|
||||||
|
if settings == nil {
|
||||||
if inf == nil {
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
if settings.VersioningEnabled {
|
||||||
enabled, ok := inf.Headers["S3-Settings-Versioning-enabled"]
|
|
||||||
if ok {
|
|
||||||
if parsed, err := strconv.ParseBool(enabled); err == nil && parsed {
|
|
||||||
res.Status = "Enabled"
|
res.Status = "Enabled"
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,6 +75,11 @@ type (
|
||||||
// PutVersioningParams stores object copy request parameters.
|
// PutVersioningParams stores object copy request parameters.
|
||||||
PutVersioningParams struct {
|
PutVersioningParams struct {
|
||||||
Bucket string
|
Bucket string
|
||||||
|
Settings *BucketSettings
|
||||||
|
}
|
||||||
|
|
||||||
|
// BucketSettings stores settings such as versioning.
|
||||||
|
BucketSettings struct {
|
||||||
VersioningEnabled bool
|
VersioningEnabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,7 +130,7 @@ type (
|
||||||
NeoFS
|
NeoFS
|
||||||
|
|
||||||
PutBucketVersioning(ctx context.Context, p *PutVersioningParams) (*ObjectInfo, error)
|
PutBucketVersioning(ctx context.Context, p *PutVersioningParams) (*ObjectInfo, error)
|
||||||
GetBucketVersioning(ctx context.Context, name string) (*ObjectInfo, error)
|
GetBucketVersioning(ctx context.Context, name string) (*BucketSettings, error)
|
||||||
|
|
||||||
ListBuckets(ctx context.Context) ([]*BucketInfo, error)
|
ListBuckets(ctx context.Context) ([]*BucketInfo, error)
|
||||||
GetBucketInfo(ctx context.Context, name string) (*BucketInfo, error)
|
GetBucketInfo(ctx context.Context, name string) (*BucketInfo, error)
|
||||||
|
@ -551,7 +556,7 @@ func (n *layer) PutBucketVersioning(ctx context.Context, p *PutVersioningParams)
|
||||||
|
|
||||||
settingsVersioningEnabled := object.NewAttribute()
|
settingsVersioningEnabled := object.NewAttribute()
|
||||||
settingsVersioningEnabled.SetKey("S3-Settings-Versioning-enabled")
|
settingsVersioningEnabled.SetKey("S3-Settings-Versioning-enabled")
|
||||||
settingsVersioningEnabled.SetValue(strconv.FormatBool(p.VersioningEnabled))
|
settingsVersioningEnabled.SetValue(strconv.FormatBool(p.Settings.VersioningEnabled))
|
||||||
|
|
||||||
attributes = append(attributes, filename, createdAt, versioningIgnore, settingsVersioningEnabled)
|
attributes = append(attributes, filename, createdAt, versioningIgnore, settingsVersioningEnabled)
|
||||||
|
|
||||||
|
@ -586,10 +591,36 @@ func (n *layer) PutBucketVersioning(ctx context.Context, p *PutVersioningParams)
|
||||||
return objectInfoFromMeta(bucketInfo, meta, "", ""), nil
|
return objectInfoFromMeta(bucketInfo, meta, "", ""), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *layer) GetBucketVersioning(ctx context.Context, bucketName string) (*ObjectInfo, error) {
|
func (n *layer) GetBucketVersioning(ctx context.Context, bucketName string) (*BucketSettings, error) {
|
||||||
bktInfo, err := n.GetBucketInfo(ctx, bucketName)
|
bktInfo, err := n.GetBucketInfo(ctx, bucketName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return n.getSettingsObjectInfo(ctx, bktInfo)
|
|
||||||
|
return n.getBucketSettings(ctx, bktInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *layer) getBucketSettings(ctx context.Context, bktInfo *BucketInfo) (*BucketSettings, error) {
|
||||||
|
objInfo, err := n.getSettingsObjectInfo(ctx, bktInfo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return objectInfoToBucketSettings(objInfo), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func objectInfoToBucketSettings(info *ObjectInfo) *BucketSettings {
|
||||||
|
res := &BucketSettings{}
|
||||||
|
|
||||||
|
if info == nil {
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
enabled, ok := info.Headers["S3-Settings-Versioning-enabled"]
|
||||||
|
if ok {
|
||||||
|
if parsed, err := strconv.ParseBool(enabled); err == nil {
|
||||||
|
res.VersioningEnabled = parsed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,6 +62,11 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
versionsDelAttr = "S3-Versions-del"
|
||||||
|
versionsAddAttr = "S3-Versions-add"
|
||||||
|
)
|
||||||
|
|
||||||
// objectSearch returns all available objects by search params.
|
// objectSearch returns all available objects by search params.
|
||||||
func (n *layer) objectSearch(ctx context.Context, p *findParams) ([]*object.ID, error) {
|
func (n *layer) objectSearch(ctx context.Context, p *findParams) ([]*object.ID, error) {
|
||||||
var opts object.SearchFilters
|
var opts object.SearchFilters
|
||||||
|
@ -131,6 +136,7 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
versioningEnabled := n.isVersioningEnabled(ctx, bkt)
|
||||||
lastVersionInfo, err := n.headLastVersion(ctx, bkt, p.Object)
|
lastVersionInfo, err := n.headLastVersion(ctx, bkt, p.Object)
|
||||||
if err != nil && !apiErrors.IsS3Error(err, apiErrors.ErrNoSuchKey) {
|
if err != nil && !apiErrors.IsS3Error(err, apiErrors.ErrNoSuchKey) {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -139,18 +145,36 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
|
||||||
attributes := make([]*object.Attribute, 0, len(p.Header)+1)
|
attributes := make([]*object.Attribute, 0, len(p.Header)+1)
|
||||||
var idsToDeleteArr []*object.ID
|
var idsToDeleteArr []*object.ID
|
||||||
if lastVersionInfo != nil {
|
if lastVersionInfo != nil {
|
||||||
versionsDeletedStr := lastVersionInfo.Headers["S3-Versions-del"]
|
if versioningEnabled {
|
||||||
|
versionsAddedStr := lastVersionInfo.Headers[versionsAddAttr]
|
||||||
|
if len(versionsAddedStr) != 0 {
|
||||||
|
versionsAddedStr += ","
|
||||||
|
}
|
||||||
|
versionsAddedStr += lastVersionInfo.ID().String()
|
||||||
|
addedVersions := object.NewAttribute()
|
||||||
|
addedVersions.SetKey(versionsAddAttr)
|
||||||
|
addedVersions.SetValue(versionsAddedStr)
|
||||||
|
attributes = append(attributes, addedVersions)
|
||||||
|
if delVersions := lastVersionInfo.Headers[versionsDelAttr]; len(delVersions) > 0 {
|
||||||
|
deletedVersions := object.NewAttribute()
|
||||||
|
deletedVersions.SetKey(versionsDelAttr)
|
||||||
|
deletedVersions.SetValue(delVersions)
|
||||||
|
attributes = append(attributes, deletedVersions)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
versionsDeletedStr := lastVersionInfo.Headers[versionsDelAttr]
|
||||||
if len(versionsDeletedStr) != 0 {
|
if len(versionsDeletedStr) != 0 {
|
||||||
versionsDeletedStr += ","
|
versionsDeletedStr += ","
|
||||||
}
|
}
|
||||||
versionsDeletedStr += lastVersionInfo.ID().String()
|
versionsDeletedStr += lastVersionInfo.ID().String()
|
||||||
deletedVersions := object.NewAttribute()
|
deletedVersions := object.NewAttribute()
|
||||||
deletedVersions.SetKey("S3-Versions-del")
|
deletedVersions.SetKey(versionsDelAttr)
|
||||||
deletedVersions.SetValue(versionsDeletedStr)
|
deletedVersions.SetValue(versionsDeletedStr)
|
||||||
|
|
||||||
attributes = append(attributes, deletedVersions)
|
attributes = append(attributes, deletedVersions)
|
||||||
idsToDeleteArr = append(idsToDeleteArr, lastVersionInfo.ID())
|
idsToDeleteArr = append(idsToDeleteArr, lastVersionInfo.ID())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
unix := strconv.FormatInt(time.Now().UTC().Unix(), 10)
|
unix := strconv.FormatInt(time.Now().UTC().Unix(), 10)
|
||||||
|
|
||||||
|
@ -260,8 +284,8 @@ func (n *layer) headLastVersion(ctx context.Context, bkt *BucketInfo, objectName
|
||||||
})
|
})
|
||||||
|
|
||||||
return objectInfoFromMeta(bkt, infos[len(infos)-1], "", ""), nil
|
return objectInfoFromMeta(bkt, infos[len(infos)-1], "", ""), nil
|
||||||
//versionsToDeleteStr, ok := lastVersionInfo.Headers["S3-Versions-add"]
|
//versionsToDeleteStr, ok := lastVersionInfo.Headers[versionsAddAttr]
|
||||||
//versionsDeletedStr := lastVersionInfo.Headers["S3-Versions-del"]
|
//versionsDeletedStr := lastVersionInfo.Headers[versionsDelAttr]
|
||||||
//idsToDeleteArr := []*object.ID{lastVersionInfo.ID()}
|
//idsToDeleteArr := []*object.ID{lastVersionInfo.ID()}
|
||||||
//if ok {
|
//if ok {
|
||||||
// // for versioning mode only
|
// // for versioning mode only
|
||||||
|
@ -475,3 +499,13 @@ func (n *layer) listAllObjects(ctx context.Context, p ListObjectsParamsCommon) (
|
||||||
|
|
||||||
return allObjects, nil
|
return allObjects, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *layer) isVersioningEnabled(ctx context.Context, bktInfo *BucketInfo) bool {
|
||||||
|
settings, err := n.getBucketSettings(ctx, bktInfo)
|
||||||
|
if err != nil {
|
||||||
|
n.log.Warn("couldn't get versioning settings object", zap.Error(err))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return settings.VersioningEnabled
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue