Skip to content

Commit 00efd1a

Browse files
committed
NuGet v1.3.1, fix to allow non-SSL connections to S3
1 parent b66ff14 commit 00efd1a

File tree

5 files changed

+135
-135
lines changed

5 files changed

+135
-135
lines changed

BlobHelper/AwsSettings.cs

Lines changed: 77 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,44 @@ public AwsSettings(string accessKey, string secretKey, AwsRegion region, string
8383
/// <summary>
8484
/// Initialize the object.
8585
/// </summary>
86-
/// <param name="hostname">Override the AWS S3 endpoint (if using non-Amazon storage). Use the form http://localhost:8000/.</param>
87-
/// <param name="ssl">Enable or disable SSL.</param>
8886
/// <param name="accessKey">Access key with which to access AWS S3.</param>
8987
/// <param name="secretKey">Secret key with which to access AWS S3.</param>
9088
/// <param name="region">AWS region.</param>
9189
/// <param name="bucket">Bucket in which to store BLOBs.</param>
92-
public AwsSettings(string hostname, bool ssl, string accessKey, string secretKey, AwsRegion region, string bucket)
90+
public AwsSettings(string accessKey, string secretKey, string region, string bucket)
9391
{
94-
if (String.IsNullOrEmpty(hostname)) throw new ArgumentNullException(nameof(hostname));
9592
if (String.IsNullOrEmpty(accessKey)) throw new ArgumentNullException(nameof(accessKey));
9693
if (String.IsNullOrEmpty(secretKey)) throw new ArgumentNullException(nameof(secretKey));
94+
if (String.IsNullOrEmpty(region)) throw new ArgumentNullException(nameof(region));
9795
if (String.IsNullOrEmpty(bucket)) throw new ArgumentNullException(nameof(bucket));
98-
Hostname = hostname;
99-
Ssl = ssl;
96+
AccessKey = accessKey;
97+
SecretKey = secretKey;
98+
Bucket = bucket;
99+
100+
if (!ValidateRegion(region)) throw new ArgumentException("Unable to validate region: " + region);
101+
Region = GetRegionFromString(region);
102+
}
103+
104+
/// <summary>
105+
/// Initialize the object.
106+
/// </summary>
107+
/// <param name="accessKey">Access key with which to access AWS S3.</param>
108+
/// <param name="secretKey">Secret key with which to access AWS S3.</param>
109+
/// <param name="region">AWS region.</param>
110+
/// <param name="bucket">Bucket in which to store BLOBs.</param>
111+
/// <param name="ssl">Enable or disable SSL.</param>
112+
public AwsSettings(string accessKey, string secretKey, AwsRegion region, string bucket, bool ssl)
113+
{
114+
if (String.IsNullOrEmpty(accessKey)) throw new ArgumentNullException(nameof(accessKey));
115+
if (String.IsNullOrEmpty(secretKey)) throw new ArgumentNullException(nameof(secretKey));
116+
if (String.IsNullOrEmpty(bucket)) throw new ArgumentNullException(nameof(bucket));
117+
Hostname = null;
118+
Ssl = true;
100119
AccessKey = accessKey;
101120
SecretKey = secretKey;
102121
Region = region;
103122
Bucket = bucket;
123+
Ssl = ssl;
104124
}
105125

106126
/// <summary>
@@ -110,75 +130,19 @@ public AwsSettings(string hostname, bool ssl, string accessKey, string secretKey
110130
/// <param name="secretKey">Secret key with which to access AWS S3.</param>
111131
/// <param name="region">AWS region.</param>
112132
/// <param name="bucket">Bucket in which to store BLOBs.</param>
113-
public AwsSettings(string accessKey, string secretKey, string region, string bucket)
133+
public AwsSettings(string accessKey, string secretKey, string region, string bucket, bool ssl)
114134
{
115135
if (String.IsNullOrEmpty(accessKey)) throw new ArgumentNullException(nameof(accessKey));
116136
if (String.IsNullOrEmpty(secretKey)) throw new ArgumentNullException(nameof(secretKey));
117137
if (String.IsNullOrEmpty(region)) throw new ArgumentNullException(nameof(region));
118138
if (String.IsNullOrEmpty(bucket)) throw new ArgumentNullException(nameof(bucket));
119139
AccessKey = accessKey;
120-
SecretKey = secretKey;
140+
SecretKey = secretKey;
121141
Bucket = bucket;
142+
Ssl = ssl;
122143

123144
if (!ValidateRegion(region)) throw new ArgumentException("Unable to validate region: " + region);
124-
125-
switch (region)
126-
{
127-
case "APNortheast1":
128-
Region = AwsRegion.APNortheast1;
129-
break;
130-
case "APSouth1":
131-
Region = AwsRegion.APSouth1;
132-
break;
133-
case "APSoutheast1":
134-
Region = AwsRegion.APSoutheast1;
135-
break;
136-
case "APSoutheast2":
137-
Region = AwsRegion.APSoutheast2;
138-
break;
139-
case "CACentral1":
140-
Region = AwsRegion.CACentral1;
141-
break;
142-
case "CNNorth1":
143-
Region = AwsRegion.CNNorth1;
144-
break;
145-
case "EUCentral1":
146-
Region = AwsRegion.EUCentral1;
147-
break;
148-
case "EUNorth1":
149-
Region = AwsRegion.EUNorth1;
150-
break;
151-
case "EUWest1":
152-
Region = AwsRegion.EUWest1;
153-
break;
154-
case "EUWest2":
155-
Region = AwsRegion.EUWest2;
156-
break;
157-
case "EUWest3":
158-
Region = AwsRegion.EUWest3;
159-
break;
160-
case "SAEast1":
161-
Region = AwsRegion.SAEast1;
162-
break;
163-
case "USEast1":
164-
Region = AwsRegion.USEast1;
165-
break;
166-
case "USEast2":
167-
Region = AwsRegion.USEast2;
168-
break;
169-
case "USGovCloudEast1":
170-
Region = AwsRegion.USGovCloudEast1;
171-
break;
172-
case "USGovCloudWest1":
173-
Region = AwsRegion.USGovCloudWest1;
174-
break;
175-
case "USWest1":
176-
Region = AwsRegion.USWest1;
177-
break;
178-
case "USWest2":
179-
Region = AwsRegion.USWest2;
180-
break;
181-
}
145+
Region = GetRegionFromString(region);
182146
}
183147

184148
/// <summary>
@@ -190,78 +154,18 @@ public AwsSettings(string accessKey, string secretKey, string region, string buc
190154
/// <param name="secretKey">Secret key with which to access AWS S3.</param>
191155
/// <param name="region">AWS region.</param>
192156
/// <param name="bucket">Bucket in which to store BLOBs.</param>
193-
public AwsSettings(string hostname, bool ssl, string accessKey, string secretKey, string region, string bucket)
157+
public AwsSettings(string hostname, bool ssl, string accessKey, string secretKey, AwsRegion region, string bucket)
194158
{
195159
if (String.IsNullOrEmpty(hostname)) throw new ArgumentNullException(nameof(hostname));
196160
if (String.IsNullOrEmpty(accessKey)) throw new ArgumentNullException(nameof(accessKey));
197161
if (String.IsNullOrEmpty(secretKey)) throw new ArgumentNullException(nameof(secretKey));
198-
if (String.IsNullOrEmpty(region)) throw new ArgumentNullException(nameof(region));
199162
if (String.IsNullOrEmpty(bucket)) throw new ArgumentNullException(nameof(bucket));
200163
Hostname = hostname;
201164
Ssl = ssl;
202165
AccessKey = accessKey;
203166
SecretKey = secretKey;
167+
Region = region;
204168
Bucket = bucket;
205-
206-
if (!ValidateRegion(region)) throw new ArgumentException("Unable to validate region: " + region);
207-
208-
switch (region)
209-
{
210-
case "APNortheast1":
211-
Region = AwsRegion.APNortheast1;
212-
break;
213-
case "APSouth1":
214-
Region = AwsRegion.APSouth1;
215-
break;
216-
case "APSoutheast1":
217-
Region = AwsRegion.APSoutheast1;
218-
break;
219-
case "APSoutheast2":
220-
Region = AwsRegion.APSoutheast2;
221-
break;
222-
case "CACentral1":
223-
Region = AwsRegion.CACentral1;
224-
break;
225-
case "CNNorth1":
226-
Region = AwsRegion.CNNorth1;
227-
break;
228-
case "EUCentral1":
229-
Region = AwsRegion.EUCentral1;
230-
break;
231-
case "EUNorth1":
232-
Region = AwsRegion.EUNorth1;
233-
break;
234-
case "EUWest1":
235-
Region = AwsRegion.EUWest1;
236-
break;
237-
case "EUWest2":
238-
Region = AwsRegion.EUWest2;
239-
break;
240-
case "EUWest3":
241-
Region = AwsRegion.EUWest3;
242-
break;
243-
case "SAEast1":
244-
Region = AwsRegion.SAEast1;
245-
break;
246-
case "USEast1":
247-
Region = AwsRegion.USEast1;
248-
break;
249-
case "USEast2":
250-
Region = AwsRegion.USEast2;
251-
break;
252-
case "USGovCloudEast1":
253-
Region = AwsRegion.USGovCloudEast1;
254-
break;
255-
case "USGovCloudWest1":
256-
Region = AwsRegion.USGovCloudWest1;
257-
break;
258-
case "USWest1":
259-
Region = AwsRegion.USWest1;
260-
break;
261-
case "USWest2":
262-
Region = AwsRegion.USWest2;
263-
break;
264-
}
265169
}
266170

267171
#endregion
@@ -348,6 +252,51 @@ private List<String> ValidRegions()
348252
return ret;
349253
}
350254

255+
private AwsRegion GetRegionFromString(string region)
256+
{
257+
switch (region)
258+
{
259+
case "APNortheast1":
260+
return AwsRegion.APNortheast1;
261+
case "APSouth1":
262+
return AwsRegion.APSouth1;
263+
case "APSoutheast1":
264+
return AwsRegion.APSoutheast1;
265+
case "APSoutheast2":
266+
return AwsRegion.APSoutheast2;
267+
case "CACentral1":
268+
return AwsRegion.CACentral1;
269+
case "CNNorth1":
270+
return AwsRegion.CNNorth1;
271+
case "EUCentral1":
272+
return AwsRegion.EUCentral1;
273+
case "EUNorth1":
274+
return AwsRegion.EUNorth1;
275+
case "EUWest1":
276+
return AwsRegion.EUWest1;
277+
case "EUWest2":
278+
return AwsRegion.EUWest2;
279+
case "EUWest3":
280+
return AwsRegion.EUWest3;
281+
case "SAEast1":
282+
return AwsRegion.SAEast1;
283+
case "USEast1":
284+
return AwsRegion.USEast1;
285+
case "USEast2":
286+
return AwsRegion.USEast2;
287+
case "USGovCloudEast1":
288+
return AwsRegion.USGovCloudEast1;
289+
case "USGovCloudWest1":
290+
return AwsRegion.USGovCloudWest1;
291+
case "USWest1":
292+
return AwsRegion.USWest1;
293+
case "USWest2":
294+
return AwsRegion.USWest2;
295+
default:
296+
throw new ArgumentException("Unknown region: " + region);
297+
}
298+
}
299+
351300
#endregion
352301
}
353302
}

BlobHelper/BlobHelper.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net462</TargetFrameworks>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>1.3.0</Version>
6+
<Version>1.3.1</Version>
77
<Authors>Joel Christner</Authors>
88
<Description>BLOB storage wrapper for Microsoft Azure, Amazon S3, Kvpbase, and local filesystem written in C#.</Description>
99
<Copyright>(c)2019 Joel Christner</Copyright>
1010
<PackageProjectUrl>https://github.com/jchristn/BlobHelper</PackageProjectUrl>
1111
<RepositoryUrl>https://github.com/jchristn/BlobHelper</RepositoryUrl>
1212
<RepositoryType>Github</RepositoryType>
1313
<PackageLicenseUrl>https://github.com/jchristn/BlobHelper/blob/master/LICENSE.TXT</PackageLicenseUrl>
14-
<PackageReleaseNotes>Enumeration and object metadata.</PackageReleaseNotes>
14+
<PackageReleaseNotes>Fix to allow connection to S3 without SSL.</PackageReleaseNotes>
1515
<PackageIconUrl>https://raw.githubusercontent.com/jchristn/BlobHelper/master/assets/icon.ico</PackageIconUrl>
1616
<PackageTags>blob azure storage s3 object rest</PackageTags>
1717
</PropertyGroup>

BlobHelper/Blobs.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,15 @@ private void InitializeClients()
344344
_S3Credentials = new Amazon.Runtime.BasicAWSCredentials(_AwsSettings.AccessKey, _AwsSettings.SecretKey);
345345

346346
if (String.IsNullOrEmpty(_AwsSettings.Hostname))
347-
{
348-
_S3Config = null;
349-
_S3Client = new AmazonS3Client(_S3Credentials, _S3Region);
347+
{
348+
_S3Config = new AmazonS3Config
349+
{
350+
RegionEndpoint = _S3Region,
351+
UseHttp = !_AwsSettings.Ssl,
352+
};
353+
354+
// _S3Client = new AmazonS3Client(_S3Credentials, _S3Region);
355+
_S3Client = new AmazonS3Client(_S3Credentials, _S3Config);
350356
}
351357
else
352358
{
@@ -355,7 +361,7 @@ private void InitializeClients()
355361
RegionEndpoint = _S3Region,
356362
ServiceURL = _AwsSettings.Hostname,
357363
ForcePathStyle = true,
358-
UseHttp = _AwsSettings.Ssl
364+
UseHttp = !_AwsSettings.Ssl
359365
};
360366

361367
_S3Client = new AmazonS3Client(_S3Credentials, _S3Config);

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This project was built to provide a simple interface over external storage to he
1717

1818
## New in v1.3.x
1919

20+
- Fix to allow non-SSL connections to S3
2021
- Added enumeration capabilities to list contents of a bucket or container
2122
- Added metadata capabilities to retrieve metadata for a given BLOB
2223
- Stream support for object read and write

Test/Program.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ static void InitializeClient()
107107
InputString("Access key :", null, false),
108108
InputString("Secret key :", null, false),
109109
InputString("Region :", "USWest1", false),
110-
InputString("Bucket :", null, false));
110+
InputString("Bucket :", null, false),
111+
InputBoolean("SSL :", true));
111112
_Blobs = new Blobs(_AwsSettings);
112113
break;
113114
case StorageType.Azure:
@@ -160,6 +161,49 @@ static string InputString(string question, string defaultAnswer, bool allowNull)
160161
}
161162
}
162163

164+
static bool InputBoolean(string question, bool yesDefault)
165+
{
166+
Console.Write(question);
167+
168+
if (yesDefault) Console.Write(" [Y/n]? ");
169+
else Console.Write(" [y/N]? ");
170+
171+
string userInput = Console.ReadLine();
172+
173+
if (String.IsNullOrEmpty(userInput))
174+
{
175+
if (yesDefault) return true;
176+
return false;
177+
}
178+
179+
userInput = userInput.ToLower();
180+
181+
if (yesDefault)
182+
{
183+
if (
184+
(String.Compare(userInput, "n") == 0)
185+
|| (String.Compare(userInput, "no") == 0)
186+
)
187+
{
188+
return false;
189+
}
190+
191+
return true;
192+
}
193+
else
194+
{
195+
if (
196+
(String.Compare(userInput, "y") == 0)
197+
|| (String.Compare(userInput, "yes") == 0)
198+
)
199+
{
200+
return true;
201+
}
202+
203+
return false;
204+
}
205+
}
206+
163207
static void Menu()
164208
{
165209
Console.WriteLine("Available commands:");

0 commit comments

Comments
 (0)