Skip to content

Commit

Permalink
Fix PutObject call for unknown size stream (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
poornas authored and deekoder committed Oct 2, 2017
1 parent c80a3ee commit 8405a21
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
32 changes: 32 additions & 0 deletions Minio.Functional.Tests/FunctionalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public static void Main(string[] args)
PutObject_Test5(minioClient).Wait();
PutObject_Test6(minioClient).Wait();
PutObject_Test7(minioClient).Wait();
PutObject_Test8(minioClient).Wait();

// Test StatObject function
StatObject_Test1(minioClient).Wait();
Expand Down Expand Up @@ -575,6 +576,37 @@ private async static Task PutObject_Test7(MinioClient minio)
new MintLogger("PutObject_Test7","Tests thread safety of minioclient on a parallel put operation",TestStatus.FAIL,(DateTime.Now - startTime),"",ex.Message, ex.ToString()).Log();
}
}
private async static Task PutObject_Test8(MinioClient minio)
{
DateTime startTime = DateTime.Now;
try {
// Putobject call with unknown stream size. See if PutObjectAsync call succeeds
string bucketName = GetRandomName(15);
string objectName = GetRandomName(10);
string contentType = "application/octet-stream";

await Setup_Test(minio, bucketName);
using (System.IO.MemoryStream filestream = rsg.GenerateStreamFromSeed(1 * MB))
{

long size = -1;
long file_write_size = filestream.Length;

await minio.PutObjectAsync(bucketName,
objectName,
filestream,
size,
contentType);
await minio.RemoveObjectAsync(bucketName, objectName);
await TearDown(minio, bucketName);
}
new MintLogger("PutObject_Test8","Tests whether PutObject with unknown stream-size passes",TestStatus.PASS,(DateTime.Now - startTime)).Log();
}
catch (Exception ex)
{
new MintLogger("PutObject_Test8","Tests whether PutObject with unknown stream-size passes",TestStatus.FAIL,(DateTime.Now - startTime),"",ex.Message, ex.ToString()).Log();
}
}
private async static Task PutObject_Task(MinioClient minio, string bucketName, string objectName, string fileName = null, string contentType = "application/octet-stream", long size = 0, Dictionary<string, string> metaData = null, MemoryStream mstream = null)
{
try
Expand Down
20 changes: 16 additions & 4 deletions Minio/ApiEndpoints/ObjectOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,16 @@ await GetObjectAsync(bucketName, objectName, (stream) =>

double expectedReadSize = partSize;
int partNumber;
int numPartsUploaded = 0;
bool skipUpload = false;
for (partNumber = 1; partNumber <= partCount; partNumber++)
{
byte[] dataToCopy = ReadFull(data, (int)partSize);

if (dataToCopy == null)
{
break;
}

if (partNumber == partCount)
{
expectedReadSize = lastPartSize;
Expand All @@ -278,22 +283,29 @@ await GetObjectAsync(bucketName, objectName, (stream) =>
{
skipUpload = false;
}

if (!skipUpload)
{
numPartsUploaded += 1;
string etag = await this.PutObjectAsync(bucketName, objectName, uploadId, partNumber, dataToCopy, metaData, cancellationToken);
totalParts[partNumber - 1] = new Part() { PartNumber = partNumber, ETag = etag, size = (long)expectedReadSize };
}

}
// This shouldn't happen where stream size is known.
if (partCount != numPartsUploaded && size != -1)
{
await this.RemoveUploadAsync(bucketName, objectName, uploadId, cancellationToken);
return;
}

Dictionary<int, string> etags = new Dictionary<int, string>();
for (partNumber = 1; partNumber <= partCount; partNumber++)
for (partNumber = 1; partNumber <= numPartsUploaded; partNumber++)
{
etags[partNumber] = totalParts[partNumber - 1].ETag;
}
await this.CompleteMultipartUploadAsync(bucketName, objectName, uploadId, etags, cancellationToken);

}

/// <summary>
/// Internal method to complete multi part upload of object to server.
/// </summary>
Expand Down

0 comments on commit 8405a21

Please sign in to comment.