Skip to content

Commit a26cf33

Browse files
committed
NuGet v1.3.3, S3 folder creation support
1 parent fa5dee3 commit a26cf33

File tree

4 files changed

+55
-24
lines changed

4 files changed

+55
-24
lines changed

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.2</Version>
6+
<Version>1.3.3</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>Modify test app and constructors to support S3 and S3-compatible storage.</PackageReleaseNotes>
14+
<PackageReleaseNotes>Support for creating S3 folders using keys containing '/'</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: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -825,17 +825,26 @@ private async Task<bool> S3Write(string key, string contentType, byte[] data)
825825
{
826826
try
827827
{
828-
Stream s = new MemoryStream(data);
829-
830-
PutObjectRequest request = new PutObjectRequest
828+
PutObjectRequest request = new PutObjectRequest();
829+
830+
if (data == null || data.Length < 0)
831+
{
832+
request.BucketName = _AwsSettings.Bucket;
833+
request.Key = key;
834+
request.ContentType = contentType;
835+
request.UseChunkEncoding = false;
836+
request.InputStream = new MemoryStream(new byte[0]);
837+
}
838+
else
831839
{
832-
BucketName = _AwsSettings.Bucket,
833-
Key = key,
834-
InputStream = s,
835-
ContentType = contentType,
836-
UseChunkEncoding = false
837-
};
838-
840+
Stream s = new MemoryStream(data);
841+
request.BucketName = _AwsSettings.Bucket;
842+
request.Key = key;
843+
request.ContentType = contentType;
844+
request.UseChunkEncoding = false;
845+
request.InputStream = s;
846+
}
847+
839848
PutObjectResponse response = await _S3Client.PutObjectAsync(request);
840849
int statusCode = (int)response.HttpStatusCode;
841850

@@ -852,15 +861,25 @@ private bool S3Write(string key, string contentType, long contentLength, Stream
852861
{
853862
try
854863
{
855-
PutObjectRequest request = new PutObjectRequest
856-
{
857-
BucketName = _AwsSettings.Bucket,
858-
Key = key,
859-
InputStream = stream,
860-
ContentType = contentType,
861-
UseChunkEncoding = false
862-
};
864+
PutObjectRequest request = new PutObjectRequest();
863865

866+
if (stream == null || contentLength < 1)
867+
{
868+
request.BucketName = _AwsSettings.Bucket;
869+
request.Key = key;
870+
request.ContentType = contentType;
871+
request.UseChunkEncoding = false;
872+
request.InputStream = new MemoryStream(new byte[0]);
873+
}
874+
else
875+
{
876+
request.BucketName = _AwsSettings.Bucket;
877+
request.Key = key;
878+
request.ContentType = contentType;
879+
request.UseChunkEncoding = false;
880+
request.InputStream = stream;
881+
}
882+
864883
PutObjectResponse response = _S3Client.PutObjectAsync(request).Result;
865884
int statusCode = (int)response.HttpStatusCode;
866885

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+
- Better support for creating S3 folders using objects with keys ending in '/' and no data
2021
- New constructor to better support S3-compatible storage, update to test app
2122
- Fix to allow non-SSL connections to S3
2223
- Added enumeration capabilities to list contents of a bucket or container

Test/Program.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,21 @@ static void Menu()
225225

226226
static void WriteBlob()
227227
{
228-
bool success = _Blobs.Write(
229-
InputString("Key:", null, false),
230-
InputString("Content type:", "text/plain", false),
231-
Encoding.UTF8.GetBytes(InputString("Data:", null, false))).Result;
228+
string key = InputString("Key:", null, false);
229+
string contentType = InputString("Content type:", "text/plain", true);
230+
string data = InputString("Data:", null, true);
231+
232+
bool success = false;
233+
234+
if (String.IsNullOrEmpty(data))
235+
{
236+
success = _Blobs.Write(key, contentType, null).Result;
237+
}
238+
else
239+
{
240+
success = _Blobs.Write(key, contentType, Encoding.UTF8.GetBytes(data)).Result;
241+
}
242+
232243
Console.WriteLine("Success: " + success);
233244
}
234245

0 commit comments

Comments
 (0)