Skip to content

Commit 38b5019

Browse files
committed
objectstorage: add GetSize to Bucket interface
Add GetSize, which returns the size in bytes of an object by issuing a HeadObject request. This will be used by the MantleBackup controller to measure the size of the diff parts uploaded to the object storage. Signed-off-by: Kohya Shiozaki <kouyan120706@gmail.com>
1 parent b9bc4b8 commit 38b5019

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

internal/controller/internal/objectstorage/objectstorage.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ type Bucket interface {
77

88
// Delete deletes the specified object. Delete will return nil if the object is not found.
99
Delete(ctx context.Context, path string) error
10+
11+
// GetSize returns the size of the specified object in bytes.
12+
GetSize(ctx context.Context, path string) (int64, error)
1013
}

internal/controller/internal/objectstorage/objectstorage_mock.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/controller/internal/objectstorage/s3.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ func (b *S3Bucket) Exists(ctx context.Context, key string) (bool, error) {
7878
return true, nil
7979
}
8080

81+
func (b *S3Bucket) GetSize(ctx context.Context, key string) (int64, error) {
82+
output, err := b.s3Client.HeadObject(ctx, &s3.HeadObjectInput{
83+
Bucket: &b.bucketName,
84+
Key: &key,
85+
})
86+
if err != nil {
87+
return 0, fmt.Errorf("HeadObject failed: %s: %s: %s: %w", b.endpoint, b.bucketName, key, err)
88+
}
89+
if output.ContentLength == nil {
90+
return 0, fmt.Errorf("HeadObject returned nil ContentLength: %s: %s: %s", b.endpoint, b.bucketName, key)
91+
}
92+
93+
return *output.ContentLength, nil
94+
}
95+
8196
func (b *S3Bucket) Delete(ctx context.Context, key string) error {
8297
if _, err := b.s3Client.DeleteObject(ctx, &s3.DeleteObjectInput{
8398
Bucket: &b.bucketName,

0 commit comments

Comments
 (0)