| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Minio Cloud Storage, (C) 2016 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/gorilla/mux" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Validate all the ListObjects query arguments, returns an APIErrorCode
 | 
					
						
							|  |  |  | // if one of the args do not meet the required conditions.
 | 
					
						
							|  |  |  | // Special conditions required by Minio server are as below
 | 
					
						
							|  |  |  | // - delimiter if set should be equal to '/', otherwise the request is rejected.
 | 
					
						
							|  |  |  | // - marker if set should have a common prefix with 'prefix' param, otherwise
 | 
					
						
							|  |  |  | //   the request is rejected.
 | 
					
						
							| 
									
										
										
										
											2016-11-22 05:51:05 +08:00
										 |  |  | func validateListObjectsArgs(prefix, marker, delimiter string, maxKeys int) APIErrorCode { | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 	// Max keys cannot be negative.
 | 
					
						
							|  |  |  | 	if maxKeys < 0 { | 
					
						
							|  |  |  | 		return ErrInvalidMaxKeys | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/// Minio special conditions for ListObjects.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Verify if delimiter is anything other than '/', which we do not support.
 | 
					
						
							|  |  |  | 	if delimiter != "" && delimiter != "/" { | 
					
						
							|  |  |  | 		return ErrNotImplemented | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Marker is set validate pre-condition.
 | 
					
						
							|  |  |  | 	if marker != "" { | 
					
						
							|  |  |  | 		// Marker not common with prefix is not implemented.
 | 
					
						
							| 
									
										
										
										
											2017-02-04 15:27:50 +08:00
										 |  |  | 		if !hasPrefix(marker, prefix) { | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 			return ErrNotImplemented | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Success.
 | 
					
						
							|  |  |  | 	return ErrNone | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ListObjectsV2Handler - GET Bucket (List Objects) Version 2.
 | 
					
						
							|  |  |  | // --------------------------
 | 
					
						
							|  |  |  | // 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.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // NOTE: It is recommended that this API to be used for application development.
 | 
					
						
							|  |  |  | // Minio continues to support ListObjectsV1 for supporting legacy tools.
 | 
					
						
							|  |  |  | func (api objectAPIHandlers) ListObjectsV2Handler(w http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 	vars := mux.Vars(r) | 
					
						
							|  |  |  | 	bucket := vars["bucket"] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 09:47:49 +08:00
										 |  |  | 	objectAPI := api.ObjectAPI() | 
					
						
							|  |  |  | 	if objectAPI == nil { | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 		writeErrorResponse(w, ErrServerNotInitialized, r.URL) | 
					
						
							| 
									
										
										
										
											2016-08-11 09:47:49 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-22 05:51:05 +08:00
										 |  |  | 	if s3Error := checkRequestAuthType(r, bucket, "s3:ListBucket", serverConfig.GetRegion()); s3Error != ErrNone { | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 		writeErrorResponse(w, s3Error, r.URL) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-22 05:51:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 	// Extract all the listObjectsV2 query params to their native values.
 | 
					
						
							| 
									
										
										
										
											2016-09-11 01:44:38 +08:00
										 |  |  | 	prefix, token, startAfter, delimiter, fetchOwner, maxKeys, _ := getListObjectsV2Args(r.URL.Query()) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// In ListObjectsV2 'continuation-token' is the marker.
 | 
					
						
							|  |  |  | 	marker := token | 
					
						
							|  |  |  | 	// Check if 'continuation-token' is empty.
 | 
					
						
							|  |  |  | 	if token == "" { | 
					
						
							|  |  |  | 		// Then we need to use 'start-after' as marker instead.
 | 
					
						
							|  |  |  | 		marker = startAfter | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-11 01:44:38 +08:00
										 |  |  | 	// Validate the query params before beginning to serve the request.
 | 
					
						
							|  |  |  | 	// fetch-owner is not validated since it is a boolean
 | 
					
						
							| 
									
										
										
										
											2016-11-22 05:51:05 +08:00
										 |  |  | 	if s3Error := validateListObjectsArgs(prefix, marker, delimiter, maxKeys); s3Error != ErrNone { | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 		writeErrorResponse(w, s3Error, r.URL) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Inititate a list objects operation based on the input params.
 | 
					
						
							|  |  |  | 	// On success would return back ListObjectsInfo object to be
 | 
					
						
							|  |  |  | 	// marshalled into S3 compatible XML header.
 | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 	listObjectsInfo, err := objectAPI.ListObjects(bucket, prefix, marker, delimiter, maxKeys) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		errorIf(err, "Unable to list objects.") | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 		writeErrorResponse(w, toAPIErrorCode(err), r.URL) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-17 13:17:00 +08:00
										 |  |  | 	response := generateListObjectsV2Response(bucket, prefix, token, listObjectsInfo.NextMarker, startAfter, delimiter, fetchOwner, listObjectsInfo.IsTruncated, maxKeys, listObjectsInfo.Objects, listObjectsInfo.Prefixes) | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 	// Write success response.
 | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 	writeSuccessResponseXML(w, encodeResponse(response)) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ListObjectsV1Handler - GET Bucket (List Objects) Version 1.
 | 
					
						
							|  |  |  | // --------------------------
 | 
					
						
							|  |  |  | // 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.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | func (api objectAPIHandlers) ListObjectsV1Handler(w http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 	vars := mux.Vars(r) | 
					
						
							|  |  |  | 	bucket := vars["bucket"] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 09:47:49 +08:00
										 |  |  | 	objectAPI := api.ObjectAPI() | 
					
						
							|  |  |  | 	if objectAPI == nil { | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 		writeErrorResponse(w, ErrServerNotInitialized, r.URL) | 
					
						
							| 
									
										
										
										
											2016-08-11 09:47:49 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-22 05:51:05 +08:00
										 |  |  | 	if s3Error := checkRequestAuthType(r, bucket, "s3:ListBucket", serverConfig.GetRegion()); s3Error != ErrNone { | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 		writeErrorResponse(w, s3Error, r.URL) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Extract all the litsObjectsV1 query params to their native values.
 | 
					
						
							|  |  |  | 	prefix, marker, delimiter, maxKeys, _ := getListObjectsV1Args(r.URL.Query()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Validate all the query params before beginning to serve the request.
 | 
					
						
							| 
									
										
										
										
											2016-11-22 05:51:05 +08:00
										 |  |  | 	if s3Error := validateListObjectsArgs(prefix, marker, delimiter, maxKeys); s3Error != ErrNone { | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 		writeErrorResponse(w, s3Error, r.URL) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Inititate a list objects operation based on the input params.
 | 
					
						
							|  |  |  | 	// On success would return back ListObjectsInfo object to be
 | 
					
						
							|  |  |  | 	// marshalled into S3 compatible XML header.
 | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 	listObjectsInfo, err := objectAPI.ListObjects(bucket, prefix, marker, delimiter, maxKeys) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		errorIf(err, "Unable to list objects.") | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 		writeErrorResponse(w, toAPIErrorCode(err), r.URL) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	response := generateListObjectsV1Response(bucket, prefix, marker, delimiter, maxKeys, listObjectsInfo) | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | 	// Write success response.
 | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 	writeSuccessResponseXML(w, encodeResponse(response)) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:32:05 +08:00
										 |  |  | } |