Files
seaweedfs/weed/s3api/s3api_bucket_default_storage_class.go
T
Chris LuandClaude Opus 5.5 b1f86ddfa2 s3: support PutBucketDefaultStorageClass and GetBucketDefaultStorageClass (#13282)
* s3: add PutBucketDefaultStorageClass and GetBucketDefaultStorageClass

The setting is kept in the bucket entry's extended attributes next to
the other bucket configuration, so every gateway sees it through the
bucket config cache. Only STANDARD and INTELLIGENT_TIERING are accepted,
matching the AWS model; STANDARD clears the stored value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: give new objects the bucket default storage class

PutObject, POST uploads, CreateMultipartUpload and CopyObject store the
bucket default when the request names no storage class. The request
headers are left alone, so bucket-policy storage-class conditions and
the self-copy change check still see only what the client sent. Upload
parts are not tagged; a completed upload takes the class recorded when
it was initiated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: test the bucket default storage class APIs and write default

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: fail a write whose bucket default storage class cannot be read

A failed bucket config lookup used to fall back to STANDARD, so a write
could store a different class than the bucket default asks for. The
write now returns the lookup error instead. A request that names its
own class still needs no lookup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: give extracted ZIP directory members the bucket default storage class

Directory members are created through mkdir rather than putToFiler, so
they stored no class while the files next to them did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: reject a default storage class body with trailing data

The decoder stopped after the first element, so a valid configuration
followed by another element or stray bytes was accepted. The body is
now read with a size bound and only whitespace, comments or processing
instructions may follow the configuration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* go.mod: bump aws-sdk-go-v2/service/s3 to v1.117.1

The release adds the PutBucketDefaultStorageClass and
GetBucketDefaultStorageClass client operations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: test the bucket default storage class through the AWS SDK

Covers get/set, an unsupported class, the setting stored in the bucket
entry and read back by a fresh bucket config, PUT, explicit classes,
multipart initiation, copy, existing objects, and per-API grants.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: let each default storage class subtest set up its own state

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: assert the explicit STANDARD class exactly in the default class test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* s3: note that the bucket default storage class is a label only

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 04:05:48 -08:00

123 lines
3.9 KiB
Go

package s3api
import (
"bytes"
"encoding/xml"
"io"
"net/http"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
)
const maxDefaultStorageClassBodySize = 64 * 1024
type DefaultStorageClassConfiguration struct {
XMLName xml.Name `xml:"DefaultStorageClassConfiguration"`
Xmlns string `xml:"xmlns,attr,omitempty"`
DefaultStorageClass string `xml:"DefaultStorageClass"`
}
// validBucketDefaultStorageClass lists the classes AWS accepts as a bucket default.
// Like the per-object header, the class is stored as a label and does not change placement.
func validBucketDefaultStorageClass(sc string) bool {
return sc == "STANDARD" || sc == "INTELLIGENT_TIERING"
}
// PutBucketDefaultStorageClassHandler https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketDefaultStorageClass.html
func (s3a *S3ApiServer) PutBucketDefaultStorageClassHandler(w http.ResponseWriter, r *http.Request) {
bucket, _ := s3_constants.GetBucketAndObject(r)
glog.V(3).Infof("PutBucketDefaultStorageClass %s", bucket)
defer util_http.CloseRequest(r)
body, err := readRequestBody(r, maxDefaultStorageClassBodySize)
if err != nil || len(body) == 0 {
s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
return
}
var config DefaultStorageClassConfiguration
dec := xml.NewDecoder(bytes.NewReader(body))
if err := dec.Decode(&config); err != nil || !xmlEndsCleanly(dec) {
s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
return
}
if !validBucketDefaultStorageClass(config.DefaultStorageClass) {
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidStorageClass)
return
}
stored := config.DefaultStorageClass
if stored == "STANDARD" {
stored = ""
}
if errCode := s3a.updateBucketConfig(bucket, func(c *BucketConfig) error {
c.DefaultStorageClass = stored
return nil
}); errCode != s3err.ErrNone {
s3err.WriteErrorResponse(w, r, errCode)
return
}
writeSuccessResponseEmpty(w, r)
}
// GetBucketDefaultStorageClassHandler https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketDefaultStorageClass.html
func (s3a *S3ApiServer) GetBucketDefaultStorageClassHandler(w http.ResponseWriter, r *http.Request) {
bucket, _ := s3_constants.GetBucketAndObject(r)
glog.V(3).Infof("GetBucketDefaultStorageClass %s", bucket)
config, errCode := s3a.getBucketConfig(bucket)
if errCode != s3err.ErrNone {
s3err.WriteErrorResponse(w, r, errCode)
return
}
sc := config.DefaultStorageClass
if sc == "" {
sc = "STANDARD"
}
writeSuccessResponseXML(w, r, DefaultStorageClassConfiguration{Xmlns: s3_constants.S3Namespace, DefaultStorageClass: sc})
}
// objectStorageClass is the class a new object stores: the request's, else the bucket default ("" means STANDARD).
func (s3a *S3ApiServer) objectStorageClass(h http.Header, bucket string) (string, s3err.ErrorCode) {
if sc := h.Get(s3_constants.AmzStorageClass); sc != "" {
return sc, s3err.ErrNone
}
config, errCode := s3a.getBucketConfig(bucket)
if errCode != s3err.ErrNone {
return "", errCode
}
return config.DefaultStorageClass, s3err.ErrNone
}
func (s3a *S3ApiServer) applyBucketDefaultStorageClass(h http.Header, bucket string, metadata map[string][]byte) s3err.ErrorCode {
sc, errCode := s3a.objectStorageClass(h, bucket)
if errCode == s3err.ErrNone && sc != "" {
metadata[s3_constants.AmzStorageClass] = []byte(sc)
}
return errCode
}
// xmlEndsCleanly reports whether only whitespace, comments and processing instructions follow the decoded element.
func xmlEndsCleanly(dec *xml.Decoder) bool {
for {
tok, err := dec.Token()
if err == io.EOF {
return true
}
if err != nil {
return false
}
switch t := tok.(type) {
case xml.Comment, xml.ProcInst:
case xml.CharData:
if len(bytes.TrimSpace(t)) != 0 {
return false
}
default:
return false
}
}
}