Merge pull request #54 from harshavardhana/pr_out_add_bucket_to_be_part_of_object_struct_reply_back_with_bucketnotfound_for_erroneous_object_put_requests

This commit is contained in:
Harshavardhana 2015-01-24 15:38:56 -08:00
commit 77210fead3
4 changed files with 65 additions and 60 deletions

View File

@ -5,7 +5,6 @@ import (
"crypto/sha256"
"fmt"
"io"
"log"
"strings"
"time"
@ -43,15 +42,21 @@ func (storage *storage) CopyObjectToWriter(w io.Writer, bucket string, object st
func (storage *storage) StoreObject(bucket string, key string, data io.Reader) error {
objectKey := bucket + ":" + key
if _, ok := storage.bucketdata[bucket]; ok == false {
return mstorage.BucketNotFound{Bucket: bucket}
}
if _, ok := storage.objectdata[objectKey]; ok == true {
return mstorage.ObjectExists{Bucket: bucket, Key: key}
}
var bytesBuffer bytes.Buffer
newObject := storedObject{}
var newObject = storedObject{}
if _, ok := io.Copy(&bytesBuffer, data); ok == nil {
size := bytesBuffer.Len()
etag := fmt.Sprintf("%x", sha256.Sum256(bytesBuffer.Bytes()))
newObject.metadata = mstorage.ObjectMetadata{
Bucket: bucket,
Key: key,
Created: time.Now(),
Size: size,
@ -71,13 +76,13 @@ func (storage *storage) StoreBucket(bucketName string) error {
if _, ok := storage.bucketdata[bucketName]; ok == true {
return mstorage.BucketExists{Bucket: bucketName}
}
newBucket := storedBucket{}
newBucket.metadata = mstorage.BucketMetadata{
Name: bucketName,
Created: time.Now(),
}
log.Println(bucketName)
var newBucket = storedBucket{}
newBucket.metadata = mstorage.BucketMetadata{}
newBucket.metadata.Name = bucketName
newBucket.metadata.Created = time.Now()
storage.bucketdata[bucketName] = newBucket
return nil
}
@ -85,8 +90,10 @@ func (storage *storage) ListObjects(bucket, prefix string, count int) []mstorage
// TODO prefix and count handling
var results []mstorage.ObjectMetadata
for key, object := range storage.objectdata {
if strings.HasPrefix(key, bucket+":") {
results = append(results, object.metadata)
if bucket == object.metadata.Bucket {
if strings.HasPrefix(key, bucket+":") {
results = append(results, object.metadata)
}
}
}
return results

View File

@ -40,6 +40,7 @@ type BucketMetadata struct {
}
type ObjectMetadata struct {
Bucket string
Key string
Created time.Time
Size int

View File

@ -10,16 +10,16 @@ type ObjectExists struct {
Key string
}
type BucketNameInvalid struct {
Bucket string
}
type BucketExists struct {
Bucket string
}
type ObjectNotFound GenericError
type GenericBucketError struct {
Bucket string
}
type BucketNameInvalid GenericBucketError
type BucketExists GenericBucketError
type BucketNotFound GenericBucketError
func (self ObjectNotFound) Error() string {
return "Object not Found: " + self.Bucket + "#" + self.Path
}
@ -35,3 +35,7 @@ func (self BucketNameInvalid) Error() string {
func (self BucketExists) Error() string {
return "Bucket exists: " + self.Bucket
}
func (self BucketNotFound) Error() string {
return "Bucket not Found: " + self.Bucket
}

View File

@ -51,9 +51,8 @@ type encoder interface {
func HttpHandler(storage mstorage.Storage) http.Handler {
mux := mux.NewRouter()
api := minioApi{
storage: storage,
}
var api = minioApi{}
api.storage = storage
mux.HandleFunc("/", api.listBucketsHandler).Methods("GET")
mux.HandleFunc("/{bucket}/", api.listObjectsHandler).Methods("GET")
@ -61,6 +60,7 @@ func HttpHandler(storage mstorage.Storage) http.Handler {
mux.HandleFunc("/{bucket}/{object:.*}", api.getObjectHandler).Methods("GET")
mux.HandleFunc("/{bucket}/{object:.*}", api.headObjectHandler).Methods("HEAD")
mux.HandleFunc("/{bucket}/{object:.*}", api.putObjectHandler).Methods("PUT")
return mux
}
@ -112,9 +112,8 @@ func (server *minioApi) headObjectHandler(w http.ResponseWriter, req *http.Reque
func (server *minioApi) listBucketsHandler(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
var prefix string
var ok bool
if prefix, ok = vars["prefix"]; ok == false {
prefix, ok := vars["prefix"]
if !ok {
prefix = ""
}
@ -144,7 +143,7 @@ func (server *minioApi) listObjectsHandler(w http.ResponseWriter, req *http.Requ
vars := mux.Vars(req)
bucket := vars["bucket"]
prefix, ok := vars["prefix"]
if ok == false {
if !ok {
prefix = ""
}
@ -195,8 +194,7 @@ func (server *minioApi) putBucketHandler(w http.ResponseWriter, req *http.Reques
}
}
// Helpers
// Write Object Header helper
func writeObjectHeaders(w http.ResponseWriter, metadata mstorage.ObjectMetadata) {
lastModified := metadata.Created.Format(time.RFC1123)
w.Header().Set("ETag", metadata.ETag)
@ -205,53 +203,48 @@ func writeObjectHeaders(w http.ResponseWriter, metadata mstorage.ObjectMetadata)
w.Header().Set("Content-Type", "text/plain")
}
func generateBucketsListResult(buckets []mstorage.BucketMetadata) (data BucketListResponse) {
func generateBucketsListResult(buckets []mstorage.BucketMetadata) BucketListResponse {
var listbuckets []*Bucket
var data = BucketListResponse{}
var owner = Owner{}
owner := Owner{
ID: "minio",
DisplayName: "minio",
}
owner.ID = "minio"
owner.DisplayName = "minio"
for _, bucket := range buckets {
listbucket := &Bucket{
Name: bucket.Name,
CreationDate: bucket.Created.Format(dateFormat),
}
var listbucket = &Bucket{}
listbucket.Name = bucket.Name
listbucket.CreationDate = bucket.Created.Format(dateFormat)
listbuckets = append(listbuckets, listbucket)
}
data = BucketListResponse{
Owner: owner,
}
data.Owner = owner
data.Buckets.Bucket = listbuckets
return
return data
}
func generateObjectsListResult(bucket string, objects []mstorage.ObjectMetadata) (data ObjectListResponse) {
func generateObjectsListResult(bucket string, objects []mstorage.ObjectMetadata) ObjectListResponse {
var contents []*Item
var owner = Owner{}
var data = ObjectListResponse{}
owner := Owner{
ID: "minio",
DisplayName: "minio",
}
owner.ID = "minio"
owner.DisplayName = "minio"
for _, object := range objects {
content := &Item{
Key: object.Key,
LastModified: object.Created.Format(dateFormat),
ETag: object.ETag,
Size: object.Size,
StorageClass: "STANDARD",
Owner: owner,
}
var content = &Item{}
content.Key = object.Key
content.LastModified = object.Created.Format(dateFormat)
content.ETag = object.ETag
content.Size = object.Size
content.StorageClass = "STANDARD"
content.Owner = owner
contents = append(contents, content)
}
data = ObjectListResponse{
Name: bucket,
Contents: contents,
MaxKeys: MAX_OBJECT_LIST,
IsTruncated: false,
}
return
data.Name = bucket
data.Contents = contents
data.MaxKeys = MAX_OBJECT_LIST
data.IsTruncated = false
return data
}