2018-09-03 18:38:10 +00:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
2019-03-16 00:20:24 +00:00
|
|
|
"context"
|
2018-09-04 07:42:44 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2018-09-09 23:26:11 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2018-09-04 07:42:44 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxObjectList = 1000 // Limit number of objects in a listObjectsResponse.
|
|
|
|
maxUploadsList = 1000 // Limit number of uploads in a listUploadsResponse.
|
|
|
|
maxPartsList = 1000 // Limit number of parts in a listPartsResponse.
|
|
|
|
globalMaxPartID = 10000
|
2018-09-03 18:38:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewMultipartUploadHandler - New multipart upload.
|
2018-09-04 07:42:44 +00:00
|
|
|
func (s3a *S3ApiServer) NewMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var object, bucket string
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket = vars["bucket"]
|
|
|
|
object = vars["object"]
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
response, errCode := s3a.createMultipartUpload(context.Background(), &s3.CreateMultipartUploadInput{
|
2018-09-04 07:42:44 +00:00
|
|
|
Bucket: aws.String(bucket),
|
|
|
|
Key: aws.String(object),
|
|
|
|
})
|
|
|
|
|
|
|
|
if errCode != ErrNone {
|
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-12 07:46:12 +00:00
|
|
|
// println("NewMultipartUploadHandler", string(encodeResponse(response)))
|
|
|
|
|
2018-09-04 07:42:44 +00:00
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
2018-09-03 18:38:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteMultipartUploadHandler - Completes multipart upload.
|
2018-09-04 07:42:44 +00:00
|
|
|
func (s3a *S3ApiServer) CompleteMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
2018-09-12 07:46:12 +00:00
|
|
|
object := getObject(vars)
|
2018-09-04 07:42:44 +00:00
|
|
|
|
|
|
|
// Get upload id.
|
|
|
|
uploadID, _, _, _ := getObjectResources(r.URL.Query())
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
response, errCode := s3a.completeMultipartUpload(context.Background(), &s3.CompleteMultipartUploadInput{
|
2018-09-12 07:46:12 +00:00
|
|
|
Bucket: aws.String(bucket),
|
|
|
|
Key: aws.String(object),
|
|
|
|
UploadId: aws.String(uploadID),
|
2018-09-04 07:42:44 +00:00
|
|
|
})
|
|
|
|
|
2018-09-12 07:46:12 +00:00
|
|
|
// println("CompleteMultipartUploadHandler", string(encodeResponse(response)), errCode)
|
|
|
|
|
2018-09-04 07:42:44 +00:00
|
|
|
if errCode != ErrNone {
|
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
2018-09-03 18:38:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// AbortMultipartUploadHandler - Aborts multipart upload.
|
2018-09-04 07:42:44 +00:00
|
|
|
func (s3a *S3ApiServer) AbortMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
2018-09-12 07:46:12 +00:00
|
|
|
object := getObject(vars)
|
2018-09-04 07:42:44 +00:00
|
|
|
|
|
|
|
// Get upload id.
|
|
|
|
uploadID, _, _, _ := getObjectResources(r.URL.Query())
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
response, errCode := s3a.abortMultipartUpload(context.Background(), &s3.AbortMultipartUploadInput{
|
2018-09-04 07:42:44 +00:00
|
|
|
Bucket: aws.String(bucket),
|
|
|
|
Key: aws.String(object),
|
|
|
|
UploadId: aws.String(uploadID),
|
|
|
|
})
|
|
|
|
|
|
|
|
if errCode != ErrNone {
|
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-12 07:46:12 +00:00
|
|
|
// println("AbortMultipartUploadHandler", string(encodeResponse(response)))
|
|
|
|
|
2018-09-04 07:42:44 +00:00
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
2018-09-03 18:38:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListMultipartUploadsHandler - Lists multipart uploads.
|
2018-09-04 07:42:44 +00:00
|
|
|
func (s3a *S3ApiServer) ListMultipartUploadsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
2018-09-03 18:38:10 +00:00
|
|
|
|
2018-09-04 07:42:44 +00:00
|
|
|
prefix, keyMarker, uploadIDMarker, delimiter, maxUploads, encodingType := getBucketMultipartResources(r.URL.Query())
|
|
|
|
if maxUploads < 0 {
|
|
|
|
writeErrorResponse(w, ErrInvalidMaxUploads, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if keyMarker != "" {
|
|
|
|
// Marker not common with prefix is not implemented.
|
|
|
|
if !strings.HasPrefix(keyMarker, prefix) {
|
|
|
|
writeErrorResponse(w, ErrNotImplemented, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
response, errCode := s3a.listMultipartUploads(context.Background(), &s3.ListMultipartUploadsInput{
|
2018-09-04 07:42:44 +00: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),
|
|
|
|
})
|
|
|
|
|
|
|
|
if errCode != ErrNone {
|
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-09 23:25:43 +00:00
|
|
|
// TODO handle encodingType
|
2018-09-12 07:46:12 +00:00
|
|
|
// println("ListMultipartUploadsHandler", string(encodeResponse(response)))
|
2018-09-09 23:25:43 +00:00
|
|
|
|
2018-09-04 07:42:44 +00:00
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
2018-09-03 18:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjectPartsHandler - Lists object parts in a multipart upload.
|
2018-09-04 07:42:44 +00:00
|
|
|
func (s3a *S3ApiServer) ListObjectPartsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
2018-09-12 07:46:12 +00:00
|
|
|
object := getObject(vars)
|
2018-09-04 07:42:44 +00:00
|
|
|
|
|
|
|
uploadID, partNumberMarker, maxParts, _ := getObjectResources(r.URL.Query())
|
|
|
|
if partNumberMarker < 0 {
|
|
|
|
writeErrorResponse(w, ErrInvalidPartNumberMarker, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if maxParts < 0 {
|
|
|
|
writeErrorResponse(w, ErrInvalidMaxParts, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
response, errCode := s3a.listObjectParts(context.Background(), &s3.ListPartsInput{
|
2018-09-04 07:42:44 +00:00
|
|
|
Bucket: aws.String(bucket),
|
|
|
|
Key: aws.String(object),
|
|
|
|
MaxParts: aws.Int64(int64(maxParts)),
|
|
|
|
PartNumberMarker: aws.Int64(int64(partNumberMarker)),
|
|
|
|
UploadId: aws.String(uploadID),
|
|
|
|
})
|
|
|
|
|
|
|
|
if errCode != ErrNone {
|
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-12 07:46:12 +00:00
|
|
|
// println("ListObjectPartsHandler", string(encodeResponse(response)))
|
|
|
|
|
2018-09-04 07:42:44 +00:00
|
|
|
writeSuccessResponseXML(w, encodeResponse(response))
|
2018-09-03 18:38:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectPartHandler - Put an object part in a multipart upload.
|
2018-09-04 07:42:44 +00:00
|
|
|
func (s3a *S3ApiServer) PutObjectPartHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
|
|
|
rAuthType := getRequestAuthType(r)
|
|
|
|
|
2019-03-16 00:20:24 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-09-04 07:42:44 +00:00
|
|
|
uploadID := r.URL.Query().Get("uploadId")
|
2019-03-16 00:20:24 +00:00
|
|
|
exists, err := s3a.exists(ctx, s3a.genUploadsFolder(bucket), uploadID, true)
|
2018-09-04 07:42:44 +00:00
|
|
|
if !exists {
|
|
|
|
writeErrorResponse(w, ErrNoSuchUpload, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
partIDString := r.URL.Query().Get("partNumber")
|
|
|
|
partID, err := strconv.Atoi(partIDString)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponse(w, ErrInvalidPart, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if partID > globalMaxPartID {
|
|
|
|
writeErrorResponse(w, ErrInvalidMaxParts, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dataReader := r.Body
|
|
|
|
if rAuthType == authTypeStreamingSigned {
|
|
|
|
dataReader = newSignV4ChunkedReader(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadUrl := fmt.Sprintf("http://%s%s/%s/%04d.part",
|
|
|
|
s3a.option.Filer, s3a.genUploadsFolder(bucket), uploadID, partID-1)
|
|
|
|
|
|
|
|
etag, errCode := s3a.putToFiler(r, uploadUrl, dataReader)
|
2018-09-03 18:38:10 +00:00
|
|
|
|
2018-09-04 07:42:44 +00:00
|
|
|
if errCode != ErrNone {
|
|
|
|
writeErrorResponse(w, errCode, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setEtag(w, etag)
|
|
|
|
|
|
|
|
writeSuccessResponseEmpty(w)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s3a *S3ApiServer) genUploadsFolder(bucket string) string {
|
2018-09-20 05:03:16 +00:00
|
|
|
return fmt.Sprintf("%s/%s/.uploads", s3a.option.BucketsPath, bucket)
|
2018-09-03 18:38:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-04 07:42:44 +00: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-03 18:38:10 +00:00
|
|
|
|
2018-09-04 07:42:44 +00: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-03 18:38:10 +00:00
|
|
|
}
|
2018-09-04 07:42:44 +00: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 }
|