Skip to content

Commit 0d84f95

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 8596685 commit 0d84f95

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,15 @@ Task ICoreAmazonS3.MakeObjectPublicAsync(string bucket, string objectKey, bool e
143143
return this.PutACLAsync(request);
144144
}
145145

146-
Task ICoreAmazonS3.EnsureBucketExistsAsync(string bucketName)
146+
async Task ICoreAmazonS3.EnsureBucketExistsAsync(string bucketName)
147147
{
148-
return this.PutBucketAsync(bucketName);
148+
try
149+
{
150+
await this.PutBucketAsync(bucketName).ConfigureAwait(false);
151+
}
152+
catch (BucketAlreadyOwnedByYouException)
153+
{
154+
}
149155
}
150156

151157
[Obsolete("This method is deprecated: its behavior is inconsistent and always uses HTTP. Please use Amazon.S3.Util.AmazonS3Util.DoesS3BucketExistV2Async instead.")]

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

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

142142
void ICoreAmazonS3.EnsureBucketExists(string bucketName)
143143
{
144-
this.PutBucket(bucketName);
144+
try
145+
{
146+
this.PutBucket(bucketName);
147+
}
148+
catch (BucketAlreadyOwnedByYouException) { }
145149
}
146150

147151
[Obsolete("This method is obsolete: its behavior is inconsistent and always uses HTTP.")]
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)