seaweedfs/weed/s3api/s3api_bucket_handlers.go

203 lines
5.0 KiB
Go
Raw Normal View History

2018-07-18 17:37:09 +08:00
package s3api
import (
"context"
"encoding/xml"
2018-07-18 17:37:09 +08:00
"fmt"
2020-12-25 16:38:56 +08:00
"github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
"math"
2018-07-22 08:39:10 +08:00
"net/http"
"time"
2018-07-24 16:38:08 +08:00
2020-11-11 16:20:59 +08:00
xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
"github.com/chrislusf/seaweedfs/weed/s3api/s3err"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
2020-02-26 03:13:06 +08:00
2018-07-24 16:38:08 +08:00
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
)
type ListAllMyBucketsResult struct {
2019-03-16 06:55:34 +08:00
XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListAllMyBucketsResult"`
Owner *s3.Owner
Buckets []*s3.Bucket `xml:"Buckets>Bucket"`
}
2018-07-18 17:37:09 +08:00
func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
2020-12-25 16:38:56 +08:00
var identity *Identity
var s3Err s3err.ErrorCode
if s3a.iam.isEnabled() {
identity, s3Err = s3a.iam.authUser(r)
if s3Err != s3err.ErrNone {
2021-06-11 12:50:21 +08:00
s3err.WriteErrorResponse(w, s3Err, r)
2020-12-25 16:38:56 +08:00
return
}
}
var response ListAllMyBucketsResult
2018-07-18 17:37:09 +08:00
entries, _, err := s3a.list(s3a.option.BucketsPath, "", "", false, math.MaxInt32)
2018-07-18 17:37:09 +08:00
2018-09-04 04:03:16 +08:00
if err != nil {
2021-06-11 12:50:21 +08:00
s3err.WriteErrorResponse(w, s3err.ErrInternalError, r)
2018-09-04 04:03:16 +08:00
return
}
2018-07-18 17:37:09 +08:00
2020-11-11 16:20:59 +08:00
identityId := r.Header.Get(xhttp.AmzIdentityId)
var buckets []*s3.Bucket
2018-09-04 04:03:16 +08:00
for _, entry := range entries {
if entry.IsDirectory {
if identity != nil && !identity.canDo(s3_constants.ACTION_LIST, entry.Name) {
2020-11-13 05:57:54 +08:00
continue
2020-11-11 16:20:59 +08:00
}
buckets = append(buckets, &s3.Bucket{
Name: aws.String(entry.Name),
CreationDate: aws.Time(time.Unix(entry.Attributes.Crtime, 0).UTC()),
2018-09-04 04:03:16 +08:00
})
2018-07-18 17:37:09 +08:00
}
2018-09-04 04:03:16 +08:00
}
2018-07-18 17:37:09 +08:00
response = ListAllMyBucketsResult{
Owner: &s3.Owner{
2020-11-11 16:20:59 +08:00
ID: aws.String(identityId),
DisplayName: aws.String(identityId),
2018-09-04 04:03:16 +08:00
},
Buckets: buckets,
2018-07-18 17:37:09 +08:00
}
2021-06-11 12:50:21 +08:00
writeSuccessResponseXML(w, response)
2018-07-18 17:37:09 +08:00
}
func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) {
2020-07-27 03:58:58 +08:00
bucket, _ := getBucketAndObject(r)
2020-10-16 01:52:17 +08:00
// avoid duplicated buckets
errCode := s3err.ErrNone
if err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
if resp, err := client.CollectionList(context.Background(), &filer_pb.CollectionListRequest{
IncludeEcVolumes: true,
IncludeNormalVolumes: true,
}); err != nil {
glog.Errorf("list collection: %v", err)
return fmt.Errorf("list collections: %v", err)
2020-10-25 11:12:04 +08:00
} else {
2020-10-16 01:52:17 +08:00
for _, c := range resp.Collections {
if bucket == c.Name {
errCode = s3err.ErrBucketAlreadyExists
break
}
}
}
return nil
}); err != nil {
2021-06-11 12:50:21 +08:00
s3err.WriteErrorResponse(w, s3err.ErrInternalError, r)
2020-10-16 01:52:17 +08:00
return
}
2020-11-12 14:11:03 +08:00
if exist, err := s3a.exists(s3a.option.BucketsPath, bucket, true); err == nil && exist {
errCode = s3err.ErrBucketAlreadyExists
}
2020-10-16 01:52:17 +08:00
if errCode != s3err.ErrNone {
2021-06-11 12:50:21 +08:00
s3err.WriteErrorResponse(w, errCode, r)
2020-10-16 01:52:17 +08:00
return
}
2020-11-11 16:20:59 +08:00
fn := func(entry *filer_pb.Entry) {
if identityId := r.Header.Get(xhttp.AmzIdentityId); identityId != "" {
2020-11-12 11:50:19 +08:00
if entry.Extended == nil {
entry.Extended = make(map[string][]byte)
}
2020-11-11 16:20:59 +08:00
entry.Extended[xhttp.AmzIdentityId] = []byte(identityId)
}
}
2018-09-04 04:03:16 +08:00
// create the folder for bucket, but lazily create actual collection
2020-11-11 16:20:59 +08:00
if err := s3a.mkdir(s3a.option.BucketsPath, bucket, fn); err != nil {
2020-10-16 01:52:17 +08:00
glog.Errorf("PutBucketHandler mkdir: %v", err)
2021-06-11 12:50:21 +08:00
s3err.WriteErrorResponse(w, s3err.ErrInternalError, r)
return
}
writeSuccessResponseEmpty(w)
}
func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
2020-07-27 03:58:58 +08:00
bucket, _ := getBucketAndObject(r)
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
2021-06-11 12:50:21 +08:00
s3err.WriteErrorResponse(w, err, r)
return
}
2020-11-12 17:59:31 +08:00
err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
// delete collection
deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
Collection: bucket,
}
glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
2020-02-26 14:23:59 +08:00
if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
2018-07-20 15:58:10 +08:00
return fmt.Errorf("delete collection %s: %v", bucket, err)
}
return nil
})
err = s3a.rm(s3a.option.BucketsPath, bucket, false, true)
2018-09-04 04:16:26 +08:00
if err != nil {
2021-06-11 12:50:21 +08:00
s3err.WriteErrorResponse(w, s3err.ErrInternalError, r)
return
}
2021-06-11 12:50:21 +08:00
s3err.WriteEmptyResponse(w, http.StatusNoContent)
}
2018-07-19 16:43:27 +08:00
func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
2020-07-27 03:58:58 +08:00
bucket, _ := getBucketAndObject(r)
2018-07-19 16:43:27 +08:00
if err := s3a.checkBucket(r, bucket); err != s3err.ErrNone {
2021-06-11 12:50:21 +08:00
s3err.WriteErrorResponse(w, err, r)
return
}
writeSuccessResponseEmpty(w)
}
func (s3a *S3ApiServer) checkBucket(r *http.Request, bucket string) s3err.ErrorCode {
2020-11-13 05:30:08 +08:00
entry, err := s3a.getEntry(s3a.option.BucketsPath, bucket)
2020-11-13 05:30:46 +08:00
if entry == nil || err == filer_pb.ErrNotFound {
return s3err.ErrNoSuchBucket
2018-07-19 16:43:27 +08:00
}
2020-11-12 17:59:31 +08:00
2020-11-13 05:57:54 +08:00
if !s3a.hasAccess(r, entry) {
return s3err.ErrAccessDenied
}
return s3err.ErrNone
2018-07-19 16:43:27 +08:00
}
2020-11-13 05:57:54 +08:00
func (s3a *S3ApiServer) hasAccess(r *http.Request, entry *filer_pb.Entry) bool {
isAdmin := r.Header.Get(xhttp.AmzIsAdmin) != ""
if isAdmin {
return true
}
if entry.Extended == nil {
return true
}
identityId := r.Header.Get(xhttp.AmzIdentityId)
if id, ok := entry.Extended[xhttp.AmzIdentityId]; ok {
if identityId != string(id) {
return false
}
}
return true
}