2018-07-18 09:37:09 +00:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-07-22 00:39:10 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-07-19 08:21:44 +00:00
|
|
|
"github.com/gorilla/mux"
|
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-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
|
|
|
)
|
|
|
|
|
|
|
|
func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
var response ListAllMyBucketsResponse
|
|
|
|
err := s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
|
|
|
|
request := &filer_pb.ListEntriesRequest{
|
2018-07-19 08:21:44 +00:00
|
|
|
Directory: s3a.option.BucketsPath,
|
2018-07-18 09:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(4).Infof("read directory: %v", request)
|
|
|
|
resp, err := client.ListEntries(context.Background(), request)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("list buckets: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var buckets []ListAllMyBucketsEntry
|
|
|
|
for _, entry := range resp.Entries {
|
|
|
|
if entry.IsDirectory {
|
|
|
|
buckets = append(buckets, ListAllMyBucketsEntry{
|
|
|
|
Name: entry.Name,
|
|
|
|
CreationDate: time.Unix(entry.Attributes.Crtime, 0),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response = ListAllMyBucketsResponse{
|
|
|
|
ListAllMyBucketsResponse: ListAllMyBucketsResult{
|
|
|
|
Owner: CanonicalUser{
|
|
|
|
ID: "",
|
|
|
|
DisplayName: "",
|
|
|
|
},
|
|
|
|
Buckets: ListAllMyBucketsList{
|
|
|
|
Bucket: buckets,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponse(w, ErrInternalError, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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"]
|
|
|
|
|
|
|
|
err := s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
|
|
|
|
request := &filer_pb.CreateEntryRequest{
|
|
|
|
Directory: s3a.option.BucketsPath,
|
|
|
|
Entry: &filer_pb.Entry{
|
|
|
|
Name: bucket,
|
|
|
|
IsDirectory: true,
|
|
|
|
Attributes: &filer_pb.FuseAttributes{
|
|
|
|
Mtime: time.Now().Unix(),
|
|
|
|
Crtime: time.Now().Unix(),
|
|
|
|
FileMode: uint32(0777 | os.ModeDir),
|
|
|
|
Uid: OS_UID,
|
|
|
|
Gid: OS_GID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("create bucket: %v", request)
|
|
|
|
if _, err := client.CreateEntry(context.Background(), request); err != nil {
|
|
|
|
return fmt.Errorf("mkdir %s/%s: %v", s3a.option.BucketsPath, bucket, err)
|
|
|
|
}
|
|
|
|
|
2018-07-20 07:10:01 +00:00
|
|
|
// lazily create collection
|
2018-07-19 08:21:44 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if 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"]
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// delete bucket metadata
|
2018-07-19 08:21:44 +00:00
|
|
|
request := &filer_pb.DeleteEntryRequest{
|
|
|
|
Directory: s3a.option.BucketsPath,
|
|
|
|
Name: bucket,
|
|
|
|
IsDirectory: true,
|
|
|
|
IsDeleteData: false,
|
|
|
|
IsRecursive: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(1).Infof("delete bucket: %v", request)
|
2018-07-20 07:10:01 +00:00
|
|
|
if _, err := client.DeleteEntry(ctx, request); err != nil {
|
2018-07-19 08:21:44 +00:00
|
|
|
return fmt.Errorf("delete bucket %s/%s: %v", s3a.option.BucketsPath, bucket, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|