Skip to content

Commit d77a0c7

Browse files
committed
Fixed an issue where IAmazonS3.EnsureBucketExists(Async) was throwing an exception if S3 bucket already exists in the executing account.
1 parent c822aec commit d77a0c7

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"services": [
3+
{
4+
"serviceName": "S3",
5+
"type": "patch",
6+
"changeLogMessages": [ "Fixed an issue where IAmazonS3.EnsureBucketExists(Async) was throwing an exception if S3 bucket already exists in the executing account." ]
7+
}
8+
]
9+
}

sdk/src/Services/S3/Custom/_async/AmazonS3Client.Extensions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,16 @@ Task ICoreAmazonS3.MakeObjectPublicAsync(string bucket, string objectKey, bool e
145145
return this.PutObjectAclAsync(request);
146146
}
147147

148-
Task ICoreAmazonS3.EnsureBucketExistsAsync(string bucketName)
148+
async Task ICoreAmazonS3.EnsureBucketExistsAsync(string bucketName)
149149
{
150-
return this.PutBucketAsync(bucketName);
150+
try
151+
{
152+
await this.PutBucketAsync(bucketName).ConfigureAwait(false);
153+
}
154+
catch (BucketAlreadyOwnedByYouException)
155+
{
156+
}
151157
}
152-
153158
#endregion
154159
}
155160
}

sdk/src/Services/S3/Custom/_bcl/AmazonS3Client.Extensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ void ICoreAmazonS3.MakeObjectPublic(string bucket, string objectKey, bool enable
143143

144144
void ICoreAmazonS3.EnsureBucketExists(string bucketName)
145145
{
146-
this.PutBucket(bucketName);
146+
try
147+
{
148+
this.PutBucket(bucketName);
149+
}
150+
catch (BucketAlreadyOwnedByYouException) { }
147151
}
148152
#endregion
149153
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Threading;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
using Amazon;
9+
10+
using Amazon.SecurityToken;
11+
using Amazon.SecurityToken.Model;
12+
13+
using Amazon.S3;
14+
using Amazon.S3.Model;
15+
using Amazon.S3.Transfer;
16+
17+
using Amazon.S3Control;
18+
using Amazon.S3Control.Model;
19+
using Amazon.Runtime.SharedInterfaces;
20+
21+
22+
23+
namespace AWSSDK_DotNet.IntegrationTests.Tests.S3
24+
{
25+
[TestClass]
26+
public class S3ExtensionsTests : TestBase<AmazonS3Client>
27+
{
28+
static string _bucketName;
29+
30+
[ClassInitialize]
31+
public static void Setup(TestContext context)
32+
{
33+
_bucketName = S3TestUtils.CreateBucketWithWait(Client);
34+
}
35+
36+
[ClassCleanup]
37+
public static void ClassCleanup()
38+
{
39+
Amazon.S3.Util.AmazonS3Util.DeleteS3BucketWithObjects(Client, _bucketName);
40+
}
41+
42+
43+
[TestMethod]
44+
public void EnsureBucketExists()
45+
{
46+
IAmazonS3 s3Client = Client;
47+
s3Client.EnsureBucketExists(_bucketName);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)