seaweedfs/weed/s3api/s3api_bucket_handlers.go

137 lines
3.1 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"
"math"
2018-07-22 08:39:10 +08:00
"net/http"
"os"
2018-07-22 08:39:10 +08:00
"time"
2018-07-24 16:38:08 +08:00
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
2020-02-26 03:13:06 +08:00
"github.com/gorilla/mux"
2018-07-24 16:38:08 +08:00
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
)
var (
OS_UID = uint32(os.Getuid())
OS_GID = uint32(os.Getgid())
2018-07-18 17:37:09 +08:00
)
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) {
var response ListAllMyBucketsResult
2018-07-18 17:37:09 +08:00
2020-02-26 14:23:59 +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 {
writeErrorResponse(w, ErrInternalError, r.URL)
return
}
2018-07-18 17:37:09 +08:00
var buckets []*s3.Bucket
2018-09-04 04:03:16 +08:00
for _, entry := range entries {
if entry.IsDirectory {
buckets = append(buckets, &s3.Bucket{
Name: aws.String(entry.Name),
CreationDate: aws.Time(time.Unix(entry.Attributes.Crtime, 0)),
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{
ID: aws.String(""),
DisplayName: aws.String(""),
2018-09-04 04:03:16 +08:00
},
Buckets: buckets,
2018-07-18 17:37:09 +08:00
}
writeSuccessResponseXML(w, encodeResponse(response))
}
func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bucket := vars["bucket"]
2018-09-04 04:03:16 +08:00
// create the folder for bucket, but lazily create actual collection
2020-02-26 14:23:59 +08:00
if err := s3a.mkdir(s3a.option.BucketsPath, bucket, nil); err != nil {
writeErrorResponse(w, ErrInternalError, r.URL)
return
}
writeSuccessResponseEmpty(w)
}
func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bucket := vars["bucket"]
2020-03-23 14:52:55 +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 {
writeErrorResponse(w, ErrInternalError, r.URL)
return
}
writeResponse(w, http.StatusNoContent, nil, mimeNone)
}
2018-07-19 16:43:27 +08:00
func (s3a *S3ApiServer) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bucket := vars["bucket"]
2020-03-23 14:52:55 +08:00
err := s3a.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
2018-07-19 16:43:27 +08:00
request := &filer_pb.LookupDirectoryEntryRequest{
Directory: s3a.option.BucketsPath,
Name: bucket,
}
glog.V(1).Infof("lookup bucket: %v", request)
if _, err := filer_pb.LookupEntry(client, request); err != nil {
2020-03-08 09:01:39 +08:00
if err == filer_pb.ErrNotFound {
return filer_pb.ErrNotFound
}
2018-07-19 16:43:27 +08:00
return fmt.Errorf("lookup bucket %s/%s: %v", s3a.option.BucketsPath, bucket, err)
}
return nil
})
if err != nil {
writeErrorResponse(w, ErrNoSuchBucket, r.URL)
return
}
writeSuccessResponseEmpty(w)
}