Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace ListObjectsV2 with HeadObject to speed up Stat #4326

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 16 additions & 17 deletions registry/storage/driver/s3-aws/s3.go
Expand Up @@ -725,32 +725,31 @@ func (d *driver) Writer(ctx context.Context, path string, appendParam bool) (sto

// Stat retrieves the FileInfo for the given path, including the current size
// in bytes and the creation time.
func (d *driver) Stat(ctx context.Context, path string) (storagedriver.FileInfo, error) {
resp, err := d.S3.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(d.Bucket),
Prefix: aws.String(d.s3Path(path)),
MaxKeys: aws.Int64(1),
func (d *driver) Stat(ctx context.Context, path string) (storagedriver.FileInfo, error) {

respHead, err := d.S3.HeadObject(&s3.HeadObjectInput{
Bucket: aws.String(d.Bucket),
Key: aws.String(d.s3Path(path)),
})
if err != nil {
if aErr, ok := err.(awserr.RequestFailure); ok {
if aErr.StatusCode() == http.StatusNotFound{
return nil, storagedriver.PathNotFoundError{Path: path}
}
}
return nil, err
}

fi := storagedriver.FileInfoFields{
Path: path,
}

if len(resp.Contents) == 1 {
if *resp.Contents[0].Key != d.s3Path(path) {
fi.IsDir = true
} else {
fi.IsDir = false
fi.Size = *resp.Contents[0].Size
fi.ModTime = *resp.Contents[0].LastModified
}
} else if len(resp.CommonPrefixes) == 1 {

if respHead.LastModified == nil {
fi.IsDir = true
} else {
return nil, storagedriver.PathNotFoundError{Path: path}
fi.IsDir = false
fi.Size = *respHead.ContentLength
fi.ModTime = *respHead.LastModified
}

return storagedriver.FileInfoInternal{FileInfoFields: fi}, nil
Expand Down