package s3
import (
"context"
"net/http"
"strconv"
"time"
)
type HeadObjectResponse struct {
Metadata map[string]string `json:"metadata"`
LastModified time.Time `json:"lastModified"`
CacheControl string `json:"cacheControl"`
ContentDisposition string `json:"contentDisposition"`
ContentEncoding string `json:"contentEncoding"`
ContentLanguage string `json:"contentLanguage"`
ContentType string `json:"contentType"`
ContentRange string `json:"contentRange"`
ETag string `json:"etag"`
ContentLength int64 `json:"contentLength"`
}
func (o *HeadObjectResponse) load(headers http.Header) {
o.LastModified, _ = time.Parse(time.RFC1123, headers.Get("Last-Modified"))
o.CacheControl = headers.Get("Cache-Control")
o.ContentDisposition = headers.Get("Content-Disposition")
o.ContentEncoding = headers.Get("Content-Encoding")
o.ContentLanguage = headers.Get("Content-Language")
o.ContentType = headers.Get("Content-Type")
o.ContentRange = headers.Get("Content-Range")
o.ETag = headers.Get("ETag")
o.ContentLength, _ = strconv.ParseInt(headers.Get("Content-Length"), 10, 0)
o.Metadata = extractMetadata(headers)
}
func (s3 *S3) HeadObject(ctx context.Context, key string, optFuncs ...func(*http.Request)) (*HeadObjectResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodHead, s3.URL(key), nil)
if err != nil {
return nil, err
}
for _, fn := range optFuncs {
if fn != nil {
fn(req)
}
}
resp, err := s3.SignAndSend(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
result := &HeadObjectResponse{}
result.load(resp.Header)
return result, nil
}