mirror of
https://github.com/distribution/distribution.git
synced 2025-08-31 06:34:11 +00:00
s3: Use HeadObject instead of ListObjects in Stat
This commit is contained in:
committed by
Adam Wolfe Gordon
parent
07956fa8da
commit
10d936eeef
@@ -599,31 +599,26 @@ func (d *driver) Writer(ctx context.Context, path string, append bool) (storaged
|
||||
// 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.ListObjects(&s3.ListObjectsInput{
|
||||
Bucket: aws.String(d.Bucket),
|
||||
Prefix: aws.String(d.s3Path(path)),
|
||||
MaxKeys: aws.Int64(1),
|
||||
resp, err := d.S3.HeadObjectWithContext(ctx, &s3.HeadObjectInput{
|
||||
Bucket: aws.String(d.Bucket),
|
||||
Key: aws.String(d.s3Path(path)),
|
||||
})
|
||||
if err != nil {
|
||||
if err, ok := err.(awserr.Error); ok && err.Code() == s3.ErrCodeNoSuchKey {
|
||||
return nil, storagedriver.PathNotFoundError{Path: path}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fi := storagedriver.FileInfoFields{
|
||||
Path: path,
|
||||
Path: path,
|
||||
IsDir: false,
|
||||
}
|
||||
|
||||
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 {
|
||||
fi.IsDir = true
|
||||
} else {
|
||||
return nil, storagedriver.PathNotFoundError{Path: path}
|
||||
if resp.ContentLength != nil {
|
||||
fi.Size = *resp.ContentLength
|
||||
}
|
||||
if resp.LastModified != nil {
|
||||
fi.ModTime = *resp.LastModified
|
||||
}
|
||||
|
||||
return storagedriver.FileInfoInternal{FileInfoFields: fi}, nil
|
||||
|
Reference in New Issue
Block a user