mirror of
https://github.com/distribution/distribution.git
synced 2025-09-01 23:17:33 +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
|
// Stat retrieves the FileInfo for the given path, including the current size
|
||||||
// in bytes and the creation time.
|
// in bytes and the creation time.
|
||||||
func (d *driver) Stat(ctx context.Context, path string) (storagedriver.FileInfo, error) {
|
func (d *driver) Stat(ctx context.Context, path string) (storagedriver.FileInfo, error) {
|
||||||
resp, err := d.S3.ListObjects(&s3.ListObjectsInput{
|
resp, err := d.S3.HeadObjectWithContext(ctx, &s3.HeadObjectInput{
|
||||||
Bucket: aws.String(d.Bucket),
|
Bucket: aws.String(d.Bucket),
|
||||||
Prefix: aws.String(d.s3Path(path)),
|
Key: aws.String(d.s3Path(path)),
|
||||||
MaxKeys: aws.Int64(1),
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err, ok := err.(awserr.Error); ok && err.Code() == s3.ErrCodeNoSuchKey {
|
||||||
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fi := storagedriver.FileInfoFields{
|
fi := storagedriver.FileInfoFields{
|
||||||
Path: path,
|
Path: path,
|
||||||
|
IsDir: false,
|
||||||
}
|
}
|
||||||
|
if resp.ContentLength != nil {
|
||||||
if len(resp.Contents) == 1 {
|
fi.Size = *resp.ContentLength
|
||||||
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 resp.LastModified != nil {
|
||||||
fi.IsDir = true
|
fi.ModTime = *resp.LastModified
|
||||||
} else {
|
|
||||||
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return storagedriver.FileInfoInternal{FileInfoFields: fi}, nil
|
return storagedriver.FileInfoInternal{FileInfoFields: fi}, nil
|
||||||
|
Reference in New Issue
Block a user