mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-09-23 22:03:38 +08:00
refactoring
This commit is contained in:
60
weed/s3api/filer_util.go
Normal file
60
weed/s3api/filer_util.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package s3api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
||||||
|
"time"
|
||||||
|
"os"
|
||||||
|
"fmt"
|
||||||
|
"github.com/chrislusf/glog"
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s3a *S3ApiServer) mkdir(parentDirectoryPath string, dirName string) error {
|
||||||
|
return s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
||||||
|
|
||||||
|
request := &filer_pb.CreateEntryRequest{
|
||||||
|
Directory: parentDirectoryPath,
|
||||||
|
Entry: &filer_pb.Entry{
|
||||||
|
Name: dirName,
|
||||||
|
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", parentDirectoryPath, dirName, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s3a *S3ApiServer) list(parentDirectoryPath string) (entries []*filer_pb.Entry, err error) {
|
||||||
|
|
||||||
|
s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
||||||
|
|
||||||
|
request := &filer_pb.ListEntriesRequest{
|
||||||
|
Directory: s3a.option.BucketsPath,
|
||||||
|
}
|
||||||
|
|
||||||
|
glog.V(4).Infof("read directory: %v", request)
|
||||||
|
resp, err := client.ListEntries(context.Background(), request)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("list dir %v: %v", parentDirectoryPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
entries = resp.Entries
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
}
|
@@ -20,48 +20,36 @@ var (
|
|||||||
func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
|
func (s3a *S3ApiServer) ListBucketsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
var response ListAllMyBucketsResponse
|
var response ListAllMyBucketsResponse
|
||||||
err := s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
||||||
|
|
||||||
request := &filer_pb.ListEntriesRequest{
|
entries, err := s3a.list(s3a.option.BucketsPath)
|
||||||
Directory: s3a.option.BucketsPath,
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
writeErrorResponse(w, ErrInternalError, r.URL)
|
writeErrorResponse(w, ErrInternalError, r.URL)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var buckets []ListAllMyBucketsEntry
|
||||||
|
for _, entry := range 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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
writeSuccessResponseXML(w, encodeResponse(response))
|
writeSuccessResponseXML(w, encodeResponse(response))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,34 +58,8 @@ func (s3a *S3ApiServer) PutBucketHandler(w http.ResponseWriter, r *http.Request)
|
|||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
bucket := vars["bucket"]
|
bucket := vars["bucket"]
|
||||||
|
|
||||||
err := s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
// create the folder for bucket, but lazily create actual collection
|
||||||
|
if err := s3a.mkdir(s3a.option.BucketsPath, bucket); err != nil {
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// lazily create collection
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
writeErrorResponse(w, ErrInternalError, r.URL)
|
writeErrorResponse(w, ErrInternalError, r.URL)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user