2015-02-23 19:46:48 -05:00
|
|
|
/*
|
|
|
|
* Mini Object Storage, (C) 2015 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-02-15 20:03:27 -05:00
|
|
|
package minioapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
mstorage "github.com/minio-io/minio/pkg/storage"
|
|
|
|
)
|
|
|
|
|
2015-02-23 19:46:48 -05:00
|
|
|
// GET Bucket (List Objects)
|
|
|
|
// -------------------------
|
|
|
|
// This implementation of the GET operation returns some or all (up to 1000)
|
|
|
|
// of the objects in a bucket. You can use the request parameters as selection
|
|
|
|
// criteria to return a subset of the objects in a bucket.
|
|
|
|
//
|
2015-03-06 00:07:19 -05:00
|
|
|
func (server *minioAPI) listObjectsHandler(w http.ResponseWriter, req *http.Request) {
|
2015-02-15 20:03:27 -05:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
|
|
|
resources := getBucketResources(req.URL.Query())
|
2015-02-26 20:23:42 -05:00
|
|
|
if resources.Policy == true {
|
2015-02-15 20:03:27 -05:00
|
|
|
server.getBucketPolicyHandler(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:03:01 -05:00
|
|
|
if resources.Maxkeys == 0 {
|
|
|
|
resources.Maxkeys = maxObjectList
|
|
|
|
}
|
|
|
|
|
2015-02-15 20:03:27 -05:00
|
|
|
acceptsContentType := getContentType(req)
|
2015-02-26 20:23:42 -05:00
|
|
|
objects, resources, err := server.storage.ListObjects(bucket, resources)
|
2015-02-15 20:03:27 -05:00
|
|
|
switch err := err.(type) {
|
|
|
|
case nil: // success
|
|
|
|
{
|
2015-02-28 17:44:26 -05:00
|
|
|
response := generateObjectsListResult(bucket, objects, resources)
|
2015-02-15 20:03:27 -05:00
|
|
|
w.Write(writeObjectHeadersAndResponse(w, response, acceptsContentType))
|
|
|
|
}
|
|
|
|
case mstorage.BucketNotFound:
|
|
|
|
{
|
|
|
|
error := errorCodeError(NoSuchBucket)
|
|
|
|
errorResponse := getErrorResponse(error, bucket)
|
2015-03-06 00:07:19 -05:00
|
|
|
w.WriteHeader(error.HTTPStatusCode)
|
2015-02-15 20:03:27 -05:00
|
|
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
|
|
|
}
|
|
|
|
case mstorage.ImplementationError:
|
|
|
|
{
|
|
|
|
// Embed error log on server side
|
|
|
|
log.Println(err)
|
|
|
|
error := errorCodeError(InternalError)
|
|
|
|
errorResponse := getErrorResponse(error, bucket)
|
2015-03-06 00:07:19 -05:00
|
|
|
w.WriteHeader(error.HTTPStatusCode)
|
2015-02-15 20:03:27 -05:00
|
|
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
|
|
|
}
|
|
|
|
case mstorage.BucketNameInvalid:
|
|
|
|
{
|
|
|
|
error := errorCodeError(InvalidBucketName)
|
|
|
|
errorResponse := getErrorResponse(error, bucket)
|
2015-03-06 00:07:19 -05:00
|
|
|
w.WriteHeader(error.HTTPStatusCode)
|
2015-02-15 20:03:27 -05:00
|
|
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
|
|
|
}
|
|
|
|
case mstorage.ObjectNameInvalid:
|
|
|
|
{
|
|
|
|
error := errorCodeError(NoSuchKey)
|
2015-02-26 20:23:42 -05:00
|
|
|
errorResponse := getErrorResponse(error, resources.Prefix)
|
2015-03-06 00:07:19 -05:00
|
|
|
w.WriteHeader(error.HTTPStatusCode)
|
2015-02-15 20:03:27 -05:00
|
|
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 19:46:48 -05:00
|
|
|
// GET Service
|
|
|
|
// -----------
|
|
|
|
// This implementation of the GET operation returns a list of all buckets
|
|
|
|
// owned by the authenticated sender of the request.
|
2015-03-06 00:07:19 -05:00
|
|
|
func (server *minioAPI) listBucketsHandler(w http.ResponseWriter, req *http.Request) {
|
2015-02-15 20:03:27 -05:00
|
|
|
acceptsContentType := getContentType(req)
|
|
|
|
buckets, err := server.storage.ListBuckets()
|
|
|
|
switch err := err.(type) {
|
|
|
|
case nil:
|
|
|
|
{
|
|
|
|
response := generateBucketsListResult(buckets)
|
|
|
|
w.Write(writeObjectHeadersAndResponse(w, response, acceptsContentType))
|
|
|
|
}
|
|
|
|
case mstorage.ImplementationError:
|
|
|
|
{
|
|
|
|
log.Println(err)
|
|
|
|
error := errorCodeError(InternalError)
|
|
|
|
errorResponse := getErrorResponse(error, "")
|
2015-03-06 00:07:19 -05:00
|
|
|
w.WriteHeader(error.HTTPStatusCode)
|
2015-02-15 20:03:27 -05:00
|
|
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
|
|
|
}
|
|
|
|
case mstorage.BackendCorrupted:
|
|
|
|
{
|
|
|
|
log.Println(err)
|
|
|
|
error := errorCodeError(InternalError)
|
|
|
|
errorResponse := getErrorResponse(error, "")
|
2015-03-06 00:07:19 -05:00
|
|
|
w.WriteHeader(error.HTTPStatusCode)
|
2015-02-15 20:03:27 -05:00
|
|
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 19:46:48 -05:00
|
|
|
// PUT Bucket
|
|
|
|
// ----------
|
|
|
|
// This implementation of the PUT operation creates a new bucket for authenticated request
|
2015-03-06 00:07:19 -05:00
|
|
|
func (server *minioAPI) putBucketHandler(w http.ResponseWriter, req *http.Request) {
|
2015-02-15 20:03:27 -05:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
2015-03-11 23:14:40 -04:00
|
|
|
err := server.storage.CreateBucket(bucket)
|
2015-02-15 20:03:27 -05:00
|
|
|
|
|
|
|
resources := getBucketResources(req.URL.Query())
|
2015-02-26 20:23:42 -05:00
|
|
|
if resources.Policy == true {
|
2015-02-15 20:03:27 -05:00
|
|
|
server.putBucketPolicyHandler(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
acceptsContentType := getContentType(req)
|
|
|
|
switch err := err.(type) {
|
|
|
|
case nil:
|
|
|
|
{
|
|
|
|
w.Header().Set("Server", "Minio")
|
|
|
|
w.Header().Set("Connection", "close")
|
|
|
|
}
|
|
|
|
case mstorage.BucketNameInvalid:
|
|
|
|
{
|
|
|
|
error := errorCodeError(InvalidBucketName)
|
|
|
|
errorResponse := getErrorResponse(error, bucket)
|
2015-03-06 00:07:19 -05:00
|
|
|
w.WriteHeader(error.HTTPStatusCode)
|
2015-02-15 20:03:27 -05:00
|
|
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
|
|
|
}
|
|
|
|
case mstorage.BucketExists:
|
|
|
|
{
|
|
|
|
error := errorCodeError(BucketAlreadyExists)
|
|
|
|
errorResponse := getErrorResponse(error, bucket)
|
2015-03-06 00:07:19 -05:00
|
|
|
w.WriteHeader(error.HTTPStatusCode)
|
2015-02-15 20:03:27 -05:00
|
|
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
|
|
|
}
|
|
|
|
case mstorage.ImplementationError:
|
|
|
|
{
|
2015-02-23 05:11:27 -05:00
|
|
|
// Embed errors log on server side
|
2015-02-15 20:03:27 -05:00
|
|
|
log.Println(err)
|
|
|
|
error := errorCodeError(InternalError)
|
|
|
|
errorResponse := getErrorResponse(error, bucket)
|
2015-03-06 00:07:19 -05:00
|
|
|
w.WriteHeader(error.HTTPStatusCode)
|
2015-02-15 20:03:27 -05:00
|
|
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|