Provide ETag with sha256Sum of input object data

master
Harshavardhana 10 years ago
parent 5b67da7d96
commit d44404dd81
  1. 9
      pkg/storage/storage.go
  2. 3
      pkg/webapi/minioapi/minioapi.go
  3. 2
      pkg/webapi/minioapi/minioapi_test.go

@ -18,10 +18,13 @@ package storage
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"log" "log"
"strings" "strings"
"time" "time"
"crypto/sha256"
) )
type Storage struct { type Storage struct {
@ -49,6 +52,7 @@ type ObjectMetadata struct {
Key string Key string
SecCreated int64 SecCreated int64
Size int Size int
ETag string
} }
func isValidBucket(bucket string) bool { func isValidBucket(bucket string) bool {
@ -108,10 +112,13 @@ func (storage *Storage) StoreObject(bucket string, key string, data io.Reader) e
var bytesBuffer bytes.Buffer var bytesBuffer bytes.Buffer
newObject := storedObject{} newObject := storedObject{}
if _, ok := io.Copy(&bytesBuffer, data); ok == nil { if _, ok := io.Copy(&bytesBuffer, data); ok == nil {
size := bytesBuffer.Len()
etag := fmt.Sprintf("%x", sha256.Sum256(bytesBuffer.Bytes()))
newObject.metadata = ObjectMetadata{ newObject.metadata = ObjectMetadata{
Key: key, Key: key,
SecCreated: time.Now().Unix(), SecCreated: time.Now().Unix(),
Size: len(bytesBuffer.Bytes()), Size: size,
ETag: etag,
} }
newObject.data = bytesBuffer.Bytes() newObject.data = bytesBuffer.Bytes()
} }

@ -43,6 +43,7 @@ func HttpHandler(storage *mstorage.Storage) http.Handler {
} }
mux.HandleFunc("/", api.listBucketsHandler).Methods("GET") mux.HandleFunc("/", api.listBucketsHandler).Methods("GET")
mux.HandleFunc("/{bucket}", api.putBucketHandler).Methods("PUT") mux.HandleFunc("/{bucket}", api.putBucketHandler).Methods("PUT")
mux.HandleFunc("/{bucket}", api.listObjectsHandler).Methods("GET")
mux.HandleFunc("/{bucket}/", api.listObjectsHandler).Methods("GET") mux.HandleFunc("/{bucket}/", api.listObjectsHandler).Methods("GET")
mux.HandleFunc("/{bucket}/{object:.*}", api.getObjectHandler).Methods("GET") mux.HandleFunc("/{bucket}/{object:.*}", api.getObjectHandler).Methods("GET")
mux.HandleFunc("/{bucket}/{object:.*}", api.putObjectHandler).Methods("PUT") mux.HandleFunc("/{bucket}/{object:.*}", api.putObjectHandler).Methods("PUT")
@ -200,7 +201,7 @@ func generateObjectsListResult(bucket string, objects []mstorage.ObjectMetadata)
content := Content{ content := Content{
Key: object.Key, Key: object.Key,
LastModified: formatDate(object.SecCreated), LastModified: formatDate(object.SecCreated),
ETag: object.Key, ETag: object.ETag,
Size: object.Size, Size: object.Size,
StorageClass: "STANDARD", StorageClass: "STANDARD",
Owner: owner, Owner: owner,

@ -47,7 +47,7 @@ func TestMinioApi(t *testing.T) {
Owner: owner, Owner: owner,
}, },
} }
data := &ListResponse{ data := &ObjectListResponse{
Name: "name", Name: "name",
Contents: contents, Contents: contents,
} }

Loading…
Cancel
Save