2018-07-18 09:37:09 +00:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-01-02 19:36:29 +00:00
|
|
|
"math"
|
2018-07-22 00:39:10 +00:00
|
|
|
"net/http"
|
2018-07-19 08:21:44 +00:00
|
|
|
"os"
|
2018-07-22 00:39:10 +00:00
|
|
|
"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"
|
2018-07-24 08:38:08 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/gorilla/mux"
|
2018-07-19 08:21:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
OS_UID = uint32(os.Getuid())
|
|
|
|
OS_GID = uint32(os.Getgid())
|
2018-07-18 09:37:09 +00:00
|
|
|
)
|
|
|
|
|
2019-01-02 19:36:29 +00:00
|
|
|
type ListAllMyBucketsResult struct {
|
|
|
|
Buckets []*s3.Bucket `xml:"Buckets>Bucket"`
|
|
|
|
Owner *s3.Owner
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-01-02 19:36:29 +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 {
|
|
|
|
writeErrorResponse(w, ErrInternalError, r.URL)
|
|
|
|
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),
|
|
|
|
CreationDate: aws.Time(time.Unix(entry.Attributes.Crtime, 0)),
|
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) {
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
2018-09-03 20:03:16 +00:00
|
|
|
// create the folder for bucket, but lazily create actual collection
|
2018-09-04 07:43:46 +00:00
|
|
|
if err := s3a.mkdir(s3a.option.BucketsPath, bucket, nil); err != nil {
|
2018-07-19 08:21:44 +00:00
|
|
|
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"]
|
|
|
|
|
|
|
|
err := s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
|
2018-07-20 07:10:01 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// delete collection
|
|
|
|
deleteCollectionRequest := &filer_pb.DeleteCollectionRequest{
|
|
|
|
Collection: bucket,
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("delete collection: %v", deleteCollectionRequest)
|
|
|
|
if _, err := client.DeleteCollection(ctx, 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
|
|
|
|
})
|
|
|
|
|
2018-09-03 20:16:26 +00:00
|
|
|
err = s3a.rm(s3a.option.BucketsPath, bucket, true, false, true)
|
|
|
|
|
2018-07-19 08:21:44 +00:00
|
|
|
if err != nil {
|
|
|
|
writeErrorResponse(w, ErrInternalError, r.URL)
|
|
|
|
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) {
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
|
|
|
err := s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
|
|
|
|
request := &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Directory: s3a.option.BucketsPath,
|
|
|
|
Name: bucket,
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("lookup bucket: %v", request)
|
|
|
|
if _, err := client.LookupDirectoryEntry(context.Background(), request); err != nil {
|
|
|
|
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)
|
|
|
|
}
|