Skip to content

Commit

Permalink
Simplification of the API post chat with Klaus
Browse files Browse the repository at this point in the history
  • Loading branch information
zveinn committed Jan 2, 2024
1 parent e6c0700 commit beae3f9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 262 deletions.
94 changes: 34 additions & 60 deletions api-get-object-attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,95 +3,80 @@ package minio
import (
"context"
"encoding/xml"
"errors"
"net/http"
"net/url"
"strconv"
"time"

"github.com/minio/minio-go/v7/pkg/encrypt"
"github.com/minio/minio-go/v7/pkg/s3utils"
)

// ObjectAttributesOptions ...
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObjectAttributes.html
// ObjectAttributesOptions is an API call that combines
// HeadObject and ListParts.
//
// VersionID - The object version you want to attributes for
// ServerSideEncryption - The server-side encryption algorithm used when storing this object in Minio
type ObjectAttributesOptions struct {
ObjectAttributes string
VersionID string
MaxParts int
PartNumberMarker int
ServerSideEncryption encrypt.ServerSide

// Bucket onwer is an S3 specific parameter
BucketOwner string
}

// ObjectAttributes ...
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObjectAttributes.html
type ObjectAttributes struct {
ObjectAttributesResponse
LastModified time.Time
VersionID string
RequestCharged string
objectAttributesResponse
LastModified time.Time
VersionID string
}

// ParseResponse ...
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObjectAttributes.html
func (o *ObjectAttributes) ParseResponse(resp *http.Response) (err error) {
func (o *ObjectAttributes) parseResponse(resp *http.Response) (err error) {
mod, err := parseRFC7231Time(resp.Header.Get("Last-Modified"))
if err != nil {
return err
}
o.LastModified = mod
o.VersionID = resp.Header.Get(amzVersionID)

response := new(ObjectAttributesResponse)
response := new(objectAttributesResponse)
if err := xml.NewDecoder(resp.Body).Decode(response); err != nil {
return err
}
o.ObjectAttributesResponse = *response
o.objectAttributesResponse = *response

return
}

// ObjectAttributesResponse ...
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObjectAttributes.html
type ObjectAttributesResponse struct {
ETag string `xml:",omitempty"`
Checksum struct {
type objectAttributesResponse struct {
ETag string `xml:",omitempty"`
StorageClass string
ObjectSize int
Checksum struct {
ChecksumCRC32 string `xml:",omitempty"`
ChecksumCRC32C string `xml:",omitempty"`
ChecksumSHA1 string `xml:",omitempty"`
ChecksumSHA256 string `xml:",omitempty"`
}
ObjectParts struct {
PartsCount int
PartNumberMarker int
NextPartNumberMarker int
MaxParts int
IsTruncated bool
Parts []*struct {
ChecksumCRC32 string `xml:",omitempty"`
ChecksumCRC32C string `xml:",omitempty"`
ChecksumSHA1 string `xml:",omitempty"`
ChecksumSHA256 string `xml:",omitempty"`
PartNumber int
Size int
} `xml:"Part"`
PartsCount int
Parts []*objectPart `xml:"Part"`
}
StorageClass string
ObjectSize int
}

// GetObjectAttributes verifies if object exists, you have permission to access it
// and returns information about the object.
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObjectAttributes.html
func (c *Client) GetObjectAttributes(ctx context.Context, bucketName, objectName string, opts ObjectAttributesOptions) (ObjectAttributes, error) {
// Input validation.
type objectPart struct {
ChecksumCRC32 string `xml:",omitempty"`
ChecksumCRC32C string `xml:",omitempty"`
ChecksumSHA1 string `xml:",omitempty"`
ChecksumSHA256 string `xml:",omitempty"`
PartNumber int
Size int
}

// GetObjectAttributes ...
// This API combines HeadObject and ListParts.
func (c *Client) GetObjectAttributes(ctx context.Context, bucketName, objectName string, opts ObjectAttributesOptions) (ObjectAttributes, error) {
if err := s3utils.CheckValidBucketName(bucketName); err != nil {
return ObjectAttributes{}, err
}

if err := s3utils.CheckValidObjectName(objectName); err != nil {
return ObjectAttributes{}, err
}
Expand All @@ -103,21 +88,10 @@ func (c *Client) GetObjectAttributes(ctx context.Context, bucketName, objectName
}

headers := make(http.Header)
headers.Set(amzObjectAttributes, opts.ObjectAttributes)

if len(opts.ObjectAttributes) < 1 {
return ObjectAttributes{}, errors.New("object attribute tags are required")
}

headers.Set(amzPartNumberMarker, strconv.Itoa(opts.PartNumberMarker))
headers.Set(amzObjectAttributes, GetObjectAttributesTags)

if opts.BucketOwner != "" {
headers.Set(amzExpectedBucketOnwer, opts.BucketOwner)
}

if opts.MaxParts != 0 {
headers.Set(amzMaxParts, strconv.Itoa(opts.MaxParts))
}
headers.Set(amzPartNumberMarker, "0")
headers.Set(amzMaxParts, "0")

if opts.ServerSideEncryption != nil {
opts.ServerSideEncryption.Marshal(headers)
Expand Down Expand Up @@ -146,7 +120,7 @@ func (c *Client) GetObjectAttributes(ctx context.Context, bucketName, objectName
defer closeResponse(resp)

OA := new(ObjectAttributes)
err = OA.ParseResponse(resp)
err = OA.parseResponse(resp)
if err != nil {
return ObjectAttributes{}, err
}
Expand Down
5 changes: 5 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ const (
)

const (
GetObjectAttributesTags = "ETag,Checksum,StorageClass,ObjectSize,ObjectParts"

Check failure on line 63 in constants.go

View workflow job for this annotation

GitHub Actions / Test on Go 1.20.x and ubuntu-latest

exported: exported const GetObjectAttributesTags should have comment (or a comment on this block) or be unexported (revive)

Check failure on line 63 in constants.go

View workflow job for this annotation

GitHub Actions / Test on Go 1.21.x and ubuntu-latest

exported: exported const GetObjectAttributesTags should have comment (or a comment on this block) or be unexported (revive)
)

const (

// Storage class header.
amzStorageClass = "X-Amz-Storage-Class"

Expand Down
Loading

0 comments on commit beae3f9

Please sign in to comment.