2018-09-04 02:38:10 +08:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
2018-09-04 15:42:44 +08:00
|
|
|
"fmt"
|
2020-09-20 05:09:58 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/s3api/s3err"
|
2018-09-10 07:26:11 +08:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2018-09-04 15:42:44 +08:00
|
|
|
"strings"
|
2020-02-10 09:42:17 +08:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
2018-09-04 15:42:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-02-26 03:45:40 +08:00
|
|
|
maxObjectListSizeLimit = 10000 // Limit number of objects in a listObjectsResponse.
|
|
|
|
maxUploadsList = 10000 // Limit number of uploads in a listUploadsResponse.
|
|
|
|
maxPartsList = 10000 // Limit number of parts in a listPartsResponse.
|
|
|
|
globalMaxPartID = 100000
|
2018-09-04 02:38:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewMultipartUploadHandler - New multipart upload.
|
2018-09-04 15:42:44 +08:00
|
|
|
func (s3a *S3ApiServer) NewMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
|
2020-07-27 03:58:58 +08:00
|
|
|
bucket, object := getBucketAndObject(r)
|
2018-09-04 15:42:44 +08:00
|
|
|
|
2020-02-26 14:23:59 +08:00
|
|
|
response, errCode := s3a.createMultipartUpload(&s3.CreateMultipartUploadInput{
|
2018-09-04 15:42:44 +08:00
|
|
|
Bucket: aws.String(bucket),
|
2019-07-09 03:37:20 +08:00
|
|
|
Key: objectKey(aws.String(object)),
|
2018-09-04 15:42:44 +08:00
|
|
|
})
|
|
|
|
|
2020-09-20 05:09:58 +08:00
|
|
|
if errCode != s3err.ErrNone {
|
2018-09-04 15:42:44 +08:00
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-12 15:46:12 +08:00
|
|
|
// println("NewMultipartUploadHandler", string(encodeResponse(response)))
|
|
|
|
|
2018-09-04 15:42:44 +08:00
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
2018-09-04 02:38:10 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteMultipartUploadHandler - Completes multipart upload.
|
2018-09-04 15:42:44 +08:00
|
|
|
func (s3a *S3ApiServer) CompleteMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
|
2020-07-27 03:58:58 +08:00
|
|
|
bucket, object := getBucketAndObject(r)
|
2018-09-04 15:42:44 +08:00
|
|
|
|
|
|
|
// Get upload id.
|
|
|
|
uploadID, _, _, _ := getObjectResources(r.URL.Query())
|
|
|
|
|
2020-02-26 14:23:59 +08:00
|
|
|
response, errCode := s3a.completeMultipartUpload(&s3.CompleteMultipartUploadInput{
|
2018-09-12 15:46:12 +08:00
|
|
|
Bucket: aws.String(bucket),
|
2019-07-09 03:37:20 +08:00
|
|
|
Key: objectKey(aws.String(object)),
|
2018-09-12 15:46:12 +08:00
|
|
|
UploadId: aws.String(uploadID),
|
2018-09-04 15:42:44 +08:00
|
|
|
})
|
|
|
|
|
2018-09-12 15:46:12 +08:00
|
|
|
// println("CompleteMultipartUploadHandler", string(encodeResponse(response)), errCode)
|
|
|
|
|
2020-09-20 05:09:58 +08:00
|
|
|
if errCode != s3err.ErrNone {
|
2018-09-04 15:42:44 +08:00
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
2018-09-04 02:38:10 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// AbortMultipartUploadHandler - Aborts multipart upload.
|
2018-09-04 15:42:44 +08:00
|
|
|
func (s3a *S3ApiServer) AbortMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
|
2020-07-27 03:58:58 +08:00
|
|
|
bucket, object := getBucketAndObject(r)
|
2018-09-04 15:42:44 +08:00
|
|
|
|
|
|
|
// Get upload id.
|
|
|
|
uploadID, _, _, _ := getObjectResources(r.URL.Query())
|
|
|
|
|
2020-02-26 14:23:59 +08:00
|
|
|
response, errCode := s3a.abortMultipartUpload(&s3.AbortMultipartUploadInput{
|
2018-09-04 15:42:44 +08:00
|
|
|
Bucket: aws.String(bucket),
|
2019-07-09 03:37:20 +08:00
|
|
|
Key: objectKey(aws.String(object)),
|
2018-09-04 15:42:44 +08:00
|
|
|
UploadId: aws.String(uploadID),
|
|
|
|
})
|
|
|
|
|
2020-09-20 05:09:58 +08:00
|
|
|
if errCode != s3err.ErrNone {
|
2018-09-04 15:42:44 +08:00
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-12 15:46:12 +08:00
|
|
|
// println("AbortMultipartUploadHandler", string(encodeResponse(response)))
|
|
|
|
|
2018-09-04 15:42:44 +08:00
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
2018-09-04 02:38:10 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListMultipartUploadsHandler - Lists multipart uploads.
|
2018-09-04 15:42:44 +08:00
|
|
|
func (s3a *S3ApiServer) ListMultipartUploadsHandler(w http.ResponseWriter, r *http.Request) {
|
2020-07-27 03:58:58 +08:00
|
|
|
bucket, _ := getBucketAndObject(r)
|
2018-09-04 02:38:10 +08:00
|
|
|
|
2018-09-04 15:42:44 +08:00
|
|
|
prefix, keyMarker, uploadIDMarker, delimiter, maxUploads, encodingType := getBucketMultipartResources(r.URL.Query())
|
|
|
|
if maxUploads < 0 {
|
2020-09-20 05:09:58 +08:00
|
|
|
writeErrorResponse(w, s3err.ErrInvalidMaxUploads, r.URL)
|
2018-09-04 15:42:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if keyMarker != "" {
|
|
|
|
// Marker not common with prefix is not implemented.
|
|
|
|
if !strings.HasPrefix(keyMarker, prefix) {
|
2020-09-20 05:09:58 +08:00
|
|
|
writeErrorResponse(w, s3err.ErrNotImplemented, r.URL)
|
2018-09-04 15:42:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 14:23:59 +08:00
|
|
|
response, errCode := s3a.listMultipartUploads(&s3.ListMultipartUploadsInput{
|
2018-09-04 15:42:44 +08:00
|
|
|
Bucket: aws.String(bucket),
|
|
|
|
Delimiter: aws.String(delimiter),
|
|
|
|
EncodingType: aws.String(encodingType),
|
|
|
|
KeyMarker: aws.String(keyMarker),
|
|
|
|
MaxUploads: aws.Int64(int64(maxUploads)),
|
|
|
|
Prefix: aws.String(prefix),
|
|
|
|
UploadIdMarker: aws.String(uploadIDMarker),
|
|
|
|
})
|
|
|
|
|
2020-09-20 05:09:58 +08:00
|
|
|
if errCode != s3err.ErrNone {
|
2018-09-04 15:42:44 +08:00
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-10 07:25:43 +08:00
|
|
|
// TODO handle encodingType
|
2018-09-12 15:46:12 +08:00
|
|
|
// println("ListMultipartUploadsHandler", string(encodeResponse(response)))
|
2018-09-10 07:25:43 +08:00
|
|
|
|
2018-09-04 15:42:44 +08:00
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
2018-09-04 02:38:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjectPartsHandler - Lists object parts in a multipart upload.
|
2018-09-04 15:42:44 +08:00
|
|
|
func (s3a *S3ApiServer) ListObjectPartsHandler(w http.ResponseWriter, r *http.Request) {
|
2020-07-27 03:58:58 +08:00
|
|
|
bucket, object := getBucketAndObject(r)
|
2018-09-04 15:42:44 +08:00
|
|
|
|
|
|
|
uploadID, partNumberMarker, maxParts, _ := getObjectResources(r.URL.Query())
|
|
|
|
if partNumberMarker < 0 {
|
2020-09-20 05:09:58 +08:00
|
|
|
writeErrorResponse(w, s3err.ErrInvalidPartNumberMarker, r.URL)
|
2018-09-04 15:42:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if maxParts < 0 {
|
2020-09-20 05:09:58 +08:00
|
|
|
writeErrorResponse(w, s3err.ErrInvalidMaxParts, r.URL)
|
2018-09-04 15:42:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-26 14:23:59 +08:00
|
|
|
response, errCode := s3a.listObjectParts(&s3.ListPartsInput{
|
2018-09-04 15:42:44 +08:00
|
|
|
Bucket: aws.String(bucket),
|
2019-07-09 03:37:20 +08:00
|
|
|
Key: objectKey(aws.String(object)),
|
2018-09-04 15:42:44 +08:00
|
|
|
MaxParts: aws.Int64(int64(maxParts)),
|
|
|
|
PartNumberMarker: aws.Int64(int64(partNumberMarker)),
|
|
|
|
UploadId: aws.String(uploadID),
|
|
|
|
})
|
|
|
|
|
2020-09-20 05:09:58 +08:00
|
|
|
if errCode != s3err.ErrNone {
|
2018-09-04 15:42:44 +08:00
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-12 15:46:12 +08:00
|
|
|
// println("ListObjectPartsHandler", string(encodeResponse(response)))
|
|
|
|
|
2018-09-04 15:42:44 +08:00
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
2018-09-04 02:38:10 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectPartHandler - Put an object part in a multipart upload.
|
2018-09-04 15:42:44 +08:00
|
|
|
func (s3a *S3ApiServer) PutObjectPartHandler(w http.ResponseWriter, r *http.Request) {
|
2020-07-27 03:58:58 +08:00
|
|
|
bucket, _ := getBucketAndObject(r)
|
2018-09-04 15:42:44 +08:00
|
|
|
|
|
|
|
uploadID := r.URL.Query().Get("uploadId")
|
2020-02-26 14:23:59 +08:00
|
|
|
exists, err := s3a.exists(s3a.genUploadsFolder(bucket), uploadID, true)
|
2018-09-04 15:42:44 +08:00
|
|
|
if !exists {
|
2020-09-20 05:09:58 +08:00
|
|
|
writeErrorResponse(w, s3err.ErrNoSuchUpload, r.URL)
|
2018-09-04 15:42:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
partIDString := r.URL.Query().Get("partNumber")
|
|
|
|
partID, err := strconv.Atoi(partIDString)
|
|
|
|
if err != nil {
|
2020-09-20 05:09:58 +08:00
|
|
|
writeErrorResponse(w, s3err.ErrInvalidPart, r.URL)
|
2018-09-04 15:42:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if partID > globalMaxPartID {
|
2020-09-20 05:09:58 +08:00
|
|
|
writeErrorResponse(w, s3err.ErrInvalidMaxParts, r.URL)
|
2018-09-04 15:42:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dataReader := r.Body
|
2020-08-09 00:11:40 +08:00
|
|
|
if s3a.iam.isEnabled() {
|
|
|
|
rAuthType := getRequestAuthType(r)
|
2020-09-20 05:09:58 +08:00
|
|
|
var s3ErrCode s3err.ErrorCode
|
2020-08-09 00:11:40 +08:00
|
|
|
switch rAuthType {
|
|
|
|
case authTypeStreamingSigned:
|
|
|
|
dataReader, s3ErrCode = s3a.iam.newSignV4ChunkedReader(r)
|
|
|
|
case authTypeSignedV2, authTypePresignedV2:
|
|
|
|
_, s3ErrCode = s3a.iam.isReqAuthenticatedV2(r)
|
|
|
|
case authTypePresigned, authTypeSigned:
|
|
|
|
_, s3ErrCode = s3a.iam.reqSignatureV4Verify(r)
|
|
|
|
}
|
2020-09-20 05:09:58 +08:00
|
|
|
if s3ErrCode != s3err.ErrNone {
|
2020-08-09 00:11:40 +08:00
|
|
|
writeErrorResponse(w, s3ErrCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
2018-09-04 15:42:44 +08:00
|
|
|
}
|
2020-02-15 01:09:15 +08:00
|
|
|
defer dataReader.Close()
|
2018-09-04 15:42:44 +08:00
|
|
|
|
2019-04-01 02:10:19 +08:00
|
|
|
uploadUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s",
|
2020-09-22 01:51:24 +08:00
|
|
|
s3a.option.Filer, s3a.genUploadsFolder(bucket), uploadID, partID, bucket)
|
2018-09-04 15:42:44 +08:00
|
|
|
|
|
|
|
etag, errCode := s3a.putToFiler(r, uploadUrl, dataReader)
|
2018-09-04 02:38:10 +08:00
|
|
|
|
2020-09-20 05:09:58 +08:00
|
|
|
if errCode != s3err.ErrNone {
|
2018-09-04 15:42:44 +08:00
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setEtag(w, etag)
|
|
|
|
|
|
|
|
writeSuccessResponseEmpty(w)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s3a *S3ApiServer) genUploadsFolder(bucket string) string {
|
2018-09-20 13:03:16 +08:00
|
|
|
return fmt.Sprintf("%s/%s/.uploads", s3a.option.BucketsPath, bucket)
|
2018-09-04 02:38:10 +08:00
|
|
|
}
|
|
|
|
|
2018-09-04 15:42:44 +08:00
|
|
|
// Parse bucket url queries for ?uploads
|
|
|
|
func getBucketMultipartResources(values url.Values) (prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int, encodingType string) {
|
|
|
|
prefix = values.Get("prefix")
|
|
|
|
keyMarker = values.Get("key-marker")
|
|
|
|
uploadIDMarker = values.Get("upload-id-marker")
|
|
|
|
delimiter = values.Get("delimiter")
|
|
|
|
if values.Get("max-uploads") != "" {
|
|
|
|
maxUploads, _ = strconv.Atoi(values.Get("max-uploads"))
|
|
|
|
} else {
|
|
|
|
maxUploads = maxUploadsList
|
|
|
|
}
|
|
|
|
encodingType = values.Get("encoding-type")
|
|
|
|
return
|
|
|
|
}
|
2018-09-04 02:38:10 +08:00
|
|
|
|
2018-09-04 15:42:44 +08:00
|
|
|
// Parse object url queries
|
|
|
|
func getObjectResources(values url.Values) (uploadID string, partNumberMarker, maxParts int, encodingType string) {
|
|
|
|
uploadID = values.Get("uploadId")
|
|
|
|
partNumberMarker, _ = strconv.Atoi(values.Get("part-number-marker"))
|
|
|
|
if values.Get("max-parts") != "" {
|
|
|
|
maxParts, _ = strconv.Atoi(values.Get("max-parts"))
|
|
|
|
} else {
|
|
|
|
maxParts = maxPartsList
|
|
|
|
}
|
|
|
|
encodingType = values.Get("encoding-type")
|
|
|
|
return
|
2018-09-04 02:38:10 +08:00
|
|
|
}
|
2018-09-04 15:42:44 +08:00
|
|
|
|
|
|
|
type byCompletedPartNumber []*s3.CompletedPart
|
|
|
|
|
|
|
|
func (a byCompletedPartNumber) Len() int { return len(a) }
|
|
|
|
func (a byCompletedPartNumber) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a byCompletedPartNumber) Less(i, j int) bool { return *a[i].PartNumber < *a[j].PartNumber }
|