Skip to content

Commit 9a5d501

Browse files
authored
Merge pull request #100 from buildkite/feat_s3_copy_on_download
feat: ensure that when we read an object we also copy it
2 parents 9576f3a + 87f2ee0 commit 9a5d501

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

sam/app/cache-bucket.cfn.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Parameters:
1313
Type: String
1414
Description: The prefix for the cache bucket name
1515
Default: buildkite-caches
16+
ExpirationInDays:
17+
Type: Number
18+
Description: The number of days to expire cache objects
19+
Default: 7
1620
Outputs:
1721
CacheBucketAccessRole:
1822
Value:
@@ -95,6 +99,11 @@ Resources:
9599
NotificationConfiguration:
96100
EventBridgeConfiguration:
97101
EventBridgeEnabled: true
102+
LifecycleConfiguration:
103+
Rules:
104+
- Id: ExpireCacheObjects
105+
Status: Enabled
106+
ExpirationInDays: !Ref ExpirationInDays
98107

99108
CacheBucketPolicy:
100109
Type: AWS::S3::BucketPolicy

store/s3.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,24 @@ func (b *S3Blob) Download(ctx context.Context, key string, destPath string) (*Tr
329329
attribute.Int("concurrency", b.concurrency),
330330
)
331331

332+
// Copy the object to itself to reset the LastModified timestamp,
333+
// which extends the lifecycle expiration.
334+
copySource := fmt.Sprintf("%s/%s", b.bucketName, fullKey)
335+
_, err = b.client.CopyObject(ctx, &s3.CopyObjectInput{
336+
Bucket: aws.String(b.bucketName),
337+
Key: aws.String(fullKey),
338+
CopySource: aws.String(copySource),
339+
MetadataDirective: "REPLACE",
340+
})
341+
if err != nil {
342+
return nil, fmt.Errorf("failed to refresh object expiration: %w", err)
343+
}
344+
345+
slog.Debug("refreshed object expiration",
346+
"key", fullKey,
347+
"bucket", b.bucketName,
348+
)
349+
332350
return &TransferInfo{
333351
BytesTransferred: bytesWritten,
334352
TransferSpeed: averageSpeed,

0 commit comments

Comments
 (0)