2018-07-22 16:14:36 +08:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
2018-07-22 16:15:11 +08:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-07-22 16:14:36 +08:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path/filepath"
|
2018-07-22 16:15:11 +08:00
|
|
|
"strconv"
|
2018-07-22 16:14:36 +08:00
|
|
|
"time"
|
2018-07-23 16:55:26 +08:00
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2018-07-22 16:14:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxObjectListSizeLimit = 1000 // Limit number of objects in a listObjectsResponse.
|
|
|
|
)
|
|
|
|
|
2018-07-22 17:04:07 +08:00
|
|
|
func (s3a *S3ApiServer) ListObjectsV2Handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/v2-RESTBucketGET.html
|
|
|
|
|
|
|
|
// collect parameters
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
2018-07-23 16:55:26 +08:00
|
|
|
glog.V(4).Infof("read v2: %v", vars)
|
|
|
|
|
2018-07-22 17:04:07 +08:00
|
|
|
originalPrefix, marker, startAfter, delimiter, _, maxKeys := getListObjectsV2Args(r.URL.Query())
|
|
|
|
|
|
|
|
if maxKeys < 0 {
|
|
|
|
writeErrorResponse(w, ErrInvalidMaxKeys, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if delimiter != "" && delimiter != "/" {
|
|
|
|
writeErrorResponse(w, ErrNotImplemented, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if marker == "" {
|
|
|
|
marker = startAfter
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := s3a.listFilerEntries(bucket, originalPrefix, maxKeys, marker)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponse(w, ErrInternalError, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
|
|
|
}
|
|
|
|
|
2018-07-22 16:14:36 +08:00
|
|
|
func (s3a *S3ApiServer) ListObjectsV1Handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html
|
|
|
|
|
|
|
|
// collect parameters
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
|
|
|
originalPrefix, marker, delimiter, maxKeys := getListObjectsV1Args(r.URL.Query())
|
|
|
|
|
|
|
|
if maxKeys < 0 {
|
|
|
|
writeErrorResponse(w, ErrInvalidMaxKeys, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if delimiter != "" && delimiter != "/" {
|
|
|
|
writeErrorResponse(w, ErrNotImplemented, r.URL)
|
2018-07-22 17:04:07 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := s3a.listFilerEntries(bucket, originalPrefix, maxKeys, marker)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponse(w, ErrInternalError, r.URL)
|
|
|
|
return
|
2018-07-22 16:14:36 +08:00
|
|
|
}
|
|
|
|
|
2018-07-22 17:04:07 +08:00
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
|
|
|
}
|
|
|
|
|
2018-07-23 16:55:26 +08:00
|
|
|
func (s3a *S3ApiServer) listFilerEntries(bucket, originalPrefix string, maxKeys int, marker string) (response *s3.ListObjectsOutput, err error) {
|
2018-07-22 17:04:07 +08:00
|
|
|
|
2018-07-22 16:14:36 +08:00
|
|
|
// convert full path prefix into directory name and prefix for entry name
|
|
|
|
dir, prefix := filepath.Split(originalPrefix)
|
|
|
|
|
|
|
|
// check filer
|
2018-07-22 17:04:07 +08:00
|
|
|
err = s3a.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2018-07-22 16:14:36 +08:00
|
|
|
|
|
|
|
request := &filer_pb.ListEntriesRequest{
|
|
|
|
Directory: fmt.Sprintf("%s/%s/%s", s3a.option.BucketsPath, bucket, dir),
|
|
|
|
Prefix: prefix,
|
2018-07-22 17:04:07 +08:00
|
|
|
Limit: uint32(maxKeys + 1),
|
2018-07-22 16:14:36 +08:00
|
|
|
StartFromFileName: marker,
|
|
|
|
InclusiveStartFrom: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.ListEntries(context.Background(), request)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("list buckets: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-23 16:55:26 +08:00
|
|
|
var contents []*s3.Object
|
|
|
|
var commonPrefixes []*s3.CommonPrefix
|
2018-07-22 17:04:07 +08:00
|
|
|
var counter int
|
|
|
|
var lastEntryName string
|
|
|
|
var isTruncated bool
|
2018-07-22 16:14:36 +08:00
|
|
|
for _, entry := range resp.Entries {
|
2018-07-22 17:04:07 +08:00
|
|
|
counter++
|
|
|
|
if counter > maxKeys {
|
|
|
|
isTruncated = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
lastEntryName = entry.Name
|
2018-07-22 16:14:36 +08:00
|
|
|
if entry.IsDirectory {
|
2018-07-23 16:55:26 +08:00
|
|
|
commonPrefixes = append(commonPrefixes, &s3.CommonPrefix{
|
|
|
|
Prefix: aws.String(fmt.Sprintf("%s%s/", dir, entry.Name)),
|
2018-07-22 16:14:36 +08:00
|
|
|
})
|
|
|
|
} else {
|
2018-07-23 16:55:26 +08:00
|
|
|
contents = append(contents, &s3.Object{
|
|
|
|
Key: aws.String(fmt.Sprintf("%s%s", dir, entry.Name)),
|
|
|
|
LastModified: aws.Time(time.Unix(entry.Attributes.Mtime, 0)),
|
|
|
|
ETag: aws.String("\"2345sgfwetrewrt\""), // TODO add etag
|
|
|
|
Size: aws.Int64(int64(filer2.TotalSize(entry.Chunks))),
|
|
|
|
Owner: &s3.Owner{
|
|
|
|
ID: aws.String("bcaf161ca5fb16fd081034f"),
|
|
|
|
DisplayName: aws.String("webfile"),
|
2018-07-22 16:14:36 +08:00
|
|
|
},
|
2018-07-23 16:55:26 +08:00
|
|
|
StorageClass: aws.String("STANDARD"),
|
2018-07-22 16:14:36 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 16:55:26 +08:00
|
|
|
response = &s3.ListObjectsOutput{
|
|
|
|
Name: aws.String(bucket),
|
|
|
|
Prefix: aws.String(originalPrefix),
|
|
|
|
Marker: aws.String(marker),
|
|
|
|
NextMarker: aws.String(lastEntryName),
|
|
|
|
MaxKeys: aws.Int64(int64(maxKeys)),
|
|
|
|
Delimiter: aws.String("/"),
|
|
|
|
IsTruncated: aws.Bool(isTruncated),
|
|
|
|
Contents: contents,
|
|
|
|
CommonPrefixes: commonPrefixes,
|
2018-07-22 16:14:36 +08:00
|
|
|
}
|
|
|
|
|
2018-07-23 16:55:26 +08:00
|
|
|
glog.V(4).Infof("read directory: %v, found: %v", request, counter)
|
|
|
|
|
2018-07-22 16:14:36 +08:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2018-07-22 17:04:07 +08:00
|
|
|
return
|
|
|
|
}
|
2018-07-22 16:14:36 +08:00
|
|
|
|
2018-07-22 17:04:07 +08:00
|
|
|
func getListObjectsV2Args(values url.Values) (prefix, token, startAfter, delimiter string, fetchOwner bool, maxkeys int) {
|
|
|
|
prefix = values.Get("prefix")
|
|
|
|
token = values.Get("continuation-token")
|
|
|
|
startAfter = values.Get("start-after")
|
|
|
|
delimiter = values.Get("delimiter")
|
|
|
|
if values.Get("max-keys") != "" {
|
|
|
|
maxkeys, _ = strconv.Atoi(values.Get("max-keys"))
|
|
|
|
} else {
|
|
|
|
maxkeys = maxObjectListSizeLimit
|
|
|
|
}
|
|
|
|
fetchOwner = values.Get("fetch-owner") == "true"
|
|
|
|
return
|
2018-07-22 16:14:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func getListObjectsV1Args(values url.Values) (prefix, marker, delimiter string, maxkeys int) {
|
|
|
|
prefix = values.Get("prefix")
|
|
|
|
marker = values.Get("marker")
|
|
|
|
delimiter = values.Get("delimiter")
|
|
|
|
if values.Get("max-keys") != "" {
|
|
|
|
maxkeys, _ = strconv.Atoi(values.Get("max-keys"))
|
|
|
|
} else {
|
|
|
|
maxkeys = maxObjectListSizeLimit
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|