Skip to content

Commit 0504739

Browse files
committed
Ensure we can incrementally deploy to empty remote s3 bucket
1 parent fe64c22 commit 0504739

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/tooling/docs-assembler/Deploying/AwsS3SyncPlanStrategy.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,25 @@ await Parallel.ForEachAsync(localObjects, ctx, async (localFile, token) =>
176176
MaxKeys = 1000
177177
};
178178
var objects = new List<S3Object>();
179+
var bucketExists = await S3BucketExists(ctx);
180+
if (!bucketExists)
181+
{
182+
context.Collector.EmitGlobalError("Bucket does not exist, cannot list objects");
183+
return (false, objects.ToDictionary(o => o.Key));
184+
}
185+
179186
var readToCompletion = true;
180187
ListObjectsV2Response response;
181188
do
182189
{
183190
response = await s3Client.ListObjectsV2Async(listBucketRequest, ctx);
184191
if (response is null or { S3Objects: null })
185192
{
186-
context.Collector.EmitGlobalError("Failed to list objects in S3 to completion");
187-
readToCompletion = false;
193+
if (response?.IsTruncated == true)
194+
{
195+
context.Collector.EmitGlobalError("Failed to list objects in S3 to completion");
196+
readToCompletion = false;
197+
}
188198
break;
189199
}
190200
objects.AddRange(response.S3Objects);
@@ -193,4 +203,21 @@ await Parallel.ForEachAsync(localObjects, ctx, async (localFile, token) =>
193203

194204
return (readToCompletion, objects.ToDictionary(o => o.Key));
195205
}
206+
207+
private async Task<bool> S3BucketExists(Cancel ctx)
208+
{
209+
//https://docs.aws.amazon.com/code-library/latest/ug/s3_example_s3_Scenario_DoesBucketExist_section.html
210+
try
211+
{
212+
_ = await s3Client.GetBucketAclAsync(new GetBucketAclRequest
213+
{
214+
BucketName = bucketName
215+
}, ctx);
216+
return true;
217+
}
218+
catch
219+
{
220+
return false;
221+
}
222+
}
196223
}

src/tooling/docs-assembler/Deploying/DocsSyncPlanValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public PlanValidationResult Validate(SyncPlan plan)
3636
}
3737
// if the total remote files are less than or equal to 100, we enforce a higher ratio of 0.8
3838
// this allows newer assembled documentation to be in a higher state of flux
39-
if (plan.TotalRemoteFiles <= 100)
39+
else if (plan.TotalRemoteFiles <= 100)
4040
{
4141
_logger.LogInformation("Plan has less than 100 total remote files ensuring delete threshold is at minimum 0.8");
4242
deleteThreshold = Math.Max(deleteThreshold, 0.8f);

0 commit comments

Comments
 (0)