2018-07-18 09:37:09 +00:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-27 07:23:04 +00:00
|
|
|
"encoding/xml"
|
2018-07-18 09:37:09 +00:00
|
|
|
"fmt"
|
2020-09-19 21:09:58 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/s3api/s3err"
|
2019-01-02 19:36:29 +00:00
|
|
|
"math"
|
2018-07-22 00:39:10 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2018-07-24 08:38:08 +00:00
|
|
|
|
2019-01-02 19:36:29 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
2020-02-25 19:13:06 +00:00
|
|
|
|
2018-07-24 08:38:08 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-07-19 08:21:44 +00:00
|
|
|
)
|
|
|
|
|
2019-01-02 19:36:29 +00:00
|
|
|
type ListAllMyBucketsResult struct {
|
2019-03-15 22:55:34 +00:00
|
|
|
XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListAllMyBucketsResult"`
|
2019-01-02 19:36:29 +00:00
|
|
|
Owner *s3.Owner
|
2019-02-27 07:23:04 +00:00
|
|
|
Buckets []*s3.Bucket `xml:"Buckets>Bucket"`
|
2019-01-02 19:36:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 09:37:09 +00:00
|
|
|
func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2019-01-02 19:36:29 +00:00
|
|
|
var response ListAllMyBucketsResult
|
2018-07-18 09:37:09 +00:00
|
|
|
|
2020-09-11 21:53:50 +00:00
|
|
|
entries, _, err := s3a.list(s3a.option.BucketsPath, "", "", false, math.MaxInt32)
|
2018-07-18 09:37:09 +00:00
|
|
|
|
2018-09-03 20:03:16 +00:00
|
|
|
if err != nil {
|
2020-09-19 21:09:58 +00:00
|
|
|
writeErrorResponse(w, s3err.ErrInternalError, r.URL)
|
2018-09-03 20:03:16 +00:00
|
|
|
return
|
|
|
|
}
|
2018-07-18 09:37:09 +00:00
|
|
|
|
2019-01-02 19:36:29 +00:00
|
|
|
var buckets []*s3.Bucket
|
2018-09-03 20:03:16 +00:00
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.IsDirectory {
|
2019-01-02 19:36:29 +00:00
|
|
|
buckets = append(buckets, &s3.Bucket{
|
|
|
|
Name: aws.String(entry.Name),
|
2020-04-30 04:35:24 +00:00
|
|
|
CreationDate: aws.Time(time.Unix(entry.Attributes.Crtime, 0).UTC()),
|
2018-09-03 20:03:16 +00:00
|
|
|
})
|
2018-07-18 09:37:09 +00:00
|
|
|
}
|
2018-09-03 20:03:16 +00:00
|
|
|
}
|
2018-07-18 09:37:09 +00:00
|
|
|
|
2019-01-02 19:36:29 +00:00
|
|
|
response = ListAllMyBucketsResult{
|
|
|
|
Owner: &s3.Owner{
|
|
|
|
ID: aws.String(""),
|
|
|
|
DisplayName: aws.String(""),
|
2018-09-03 20:03:16 +00:00
|
|
|
},
|
2019-01-02 19:36:29 +00:00
|
|
|
Buckets: buckets,
|
2018-07-18 09:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
|
|
|
}
|
2018-07-19 08:21:44 +00:00
|
|
|
|
|
|
|
func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2020-07-26 19:58:58 +00:00
|
|
|
bucket, _ := getBucketAndObject(r)
|
2018-07-19 08:21:44 +00:00
|
|
|
|
2020-10-15 17:52:17 +00: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 03:12:04 +00:00
|
|
|
} else {
|
2020-10-15 17:52:17 +00:00
|
|
|
for _, c := range resp.Collections {
|
|
|
|
if bucket == c.Name {
|
|
|
|
errCode = s3err.ErrBucketAlreadyExists
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
writeErrorResponse(w, s3err.ErrInternalError, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if errCode != s3err.ErrNone {
|
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-03 20:03:16 +00:00
|
|
|
// create the folder for bucket, but lazily create actual collection
|
2020-02-26 06:23:59 +00:00
|
|
|
if err := s3a.mkdir(s3a.option.BucketsPath, bucket, nil); err != nil {
|
2020-10-15 17:52:17 +00:00
|
|
|
glog.Errorf("PutBucketHandler mkdir: %v", err)
|
2020-09-19 21:09:58 +00:00
|
|
|
writeErrorResponse(w, s3err.ErrInternalError, r.URL)
|
2018-07-19 08:21:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseEmpty(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2020-07-26 19:58:58 +00:00
|
|
|
bucket, _ := getBucketAndObject(r)
|
2018-07-19 08:21:44 +00:00
|
|
|
|
2020-03-23 06:52:55 +00:00
|
|
|
err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2018-07-20 07:10:01 +00:00
|
|
|
|
|
|
|
// delete collection
|
|
|
|
deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
|
|
|
|
Collection: bucket,
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
|
2020-02-26 06:23:59 +00:00
|
|
|
if _, err := client.DeleteCollection(context.Background(), deleteCollectionRequest); err != nil {
|
2018-07-20 07:58:10 +00:00
|
|
|
return fmt.Errorf("delete collection %s: %v", bucket, err)
|
2018-07-20 07:10:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 08:21:44 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-03-20 21:17:31 +00:00
|
|
|
err = s3a.rm(s3a.option.BucketsPath, bucket, false, true)
|
2018-09-03 20:16:26 +00:00
|
|
|
|
2018-07-19 08:21:44 +00:00
|
|
|
if err != nil {
|
2020-09-19 21:09:58 +00:00
|
|
|
writeErrorResponse(w, s3err.ErrInternalError, r.URL)
|
2018-07-19 08:21:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeResponse(w, http.StatusNoContent, nil, mimeNone)
|
|
|
|
}
|
2018-07-19 08:43:27 +00:00
|
|
|
|
|
|
|
func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2020-07-26 19:58:58 +00:00
|
|
|
bucket, _ := getBucketAndObject(r)
|
2018-07-19 08:43:27 +00:00
|
|
|
|
2020-03-23 06:52:55 +00:00
|
|
|
err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2018-07-19 08:43:27 +00:00
|
|
|
|
|
|
|
request := &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Directory: s3a.option.BucketsPath,
|
|
|
|
Name: bucket,
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("lookup bucket: %v", request)
|
2020-03-08 00:51:46 +00:00
|
|
|
if _, err := filer_pb.LookupEntry(client, request); err != nil {
|
2020-03-08 01:01:39 +00:00
|
|
|
if err == filer_pb.ErrNotFound {
|
|
|
|
return filer_pb.ErrNotFound
|
2020-03-08 00:51:46 +00:00
|
|
|
}
|
2018-07-19 08:43:27 +00:00
|
|
|
return fmt.Errorf("lookup bucket %s/%s: %v", s3a.option.BucketsPath, bucket, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2020-09-19 21:09:58 +00:00
|
|
|
writeErrorResponse(w, s3err.ErrNoSuchBucket, r.URL)
|
2018-07-19 08:43:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseEmpty(w)
|
|
|
|
}
|