Skip to content

Instantly share code, notes, and snippets.

@setu1421
Created April 6, 2019 06:41
Show Gist options
  • Save setu1421/7696c85d8dab0a94c4cd4e351ff22842 to your computer and use it in GitHub Desktop.
Save setu1421/7696c85d8dab0a94c4cd4e351ff22842 to your computer and use it in GitHub Desktop.
Utility helper for uploading large files to AWS S3 bucket
public class UploadUtilityHelper
{
private readonly string bucketName = ConfigurationManager.AppSettings["BucketName"];
private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USEast1;
public string UploadChunk(string fileName, string uploadId, int chunkIndex, int chunkMax, Stream stream, string prevETags)
{
var response = "";
try
{
// Retreiving Previous ETags
var eTags = new List<PartETag>();
if (!string.IsNullOrEmpty(prevETags))
{
eTags = SetAllETags(prevETags);
}
var _s3Client = new AmazonS3Client(ConfigurationManager.AppSettings["AWSAccessKey"],
ConfigurationManager.AppSettings["AWSSecretKey"], bucketRegion);
var lastPart = ((chunkMax - chunkIndex) == 1) ? true : false;
var partNumber = chunkIndex + 1;
var ms = new MemoryStream();
stream.CopyTo(ms);
ms.Position = 0;
//Step 1: build and send a multi upload request
if (chunkIndex == 0)
{
var initiateRequest = new InitiateMultipartUploadRequest
{
BucketName = bucketName,
Key = fileName
};
var initResponse = _s3Client.InitiateMultipartUpload(initiateRequest);
uploadId = initResponse.UploadId;
}
//Step 2: upload each chunk (this is run for every chunk unlike the other steps which are run once)
var uploadRequest = new UploadPartRequest
{
BucketName = bucketName,
Key = fileName,
UploadId = uploadId,
PartNumber = partNumber,
InputStream = ms,
IsLastPart = lastPart,
PartSize = ms.Length
};
var uploadResponse = _s3Client.UploadPart(uploadRequest);
//Step 3: build and send the multipart complete request
if (lastPart)
{
eTags.Add(new PartETag
{
PartNumber = partNumber,
ETag = uploadResponse.ETag
});
var completeRequest = new CompleteMultipartUploadRequest
{
BucketName = bucketName,
Key = fileName,
UploadId = uploadId,
PartETags = eTags
};
var result = _s3Client.CompleteMultipartUpload(completeRequest);
//Set the uploadId and fileURLs with the response.
response = uploadRequest.UploadId + "|success|" + result.Location + "|";
//For image get thumbnail url
if (HasImageExtension(fileName.ToLower()))
{
//Send the Thumbnail URL
response += result.Location.Replace(uploadRequest.Key, "thumbnail/" + uploadRequest.Key);
} else
{
response += "";
}
}
else
{
eTags.Add(new PartETag
{
PartNumber = partNumber,
ETag = uploadResponse.ETag
});
//Set the uploadId and eTags with the response
response = uploadRequest.UploadId + "|" + GetAllETags(eTags);
}
}
catch (Exception e)
{
return null;
}
return response;
}
private List<PartETag> SetAllETags(string prevETags)
{
var partETags = new List<PartETag>();
var splittedPrevETags = prevETags.Split(',');
for (int i = 0; i < splittedPrevETags.Length; i++)
{
partETags.Add(new PartETag
{
PartNumber = Int32.Parse(splittedPrevETags[i]),
ETag = splittedPrevETags[i + 1]
});
i = i + 1;
}
return partETags;
}
private string GetAllETags(List<PartETag> newETags)
{
var newPartETags = "";
var isNotFirstTag = false;
foreach (var eTag in newETags)
{
newPartETags += ((isNotFirstTag) ? "," : "") + (eTag.PartNumber.ToString() + ',' + eTag.ETag);
isNotFirstTag = true;
}
return newPartETags;
}
private bool HasImageExtension(string fileName)
{
return (fileName.EndsWith(".png") || fileName.EndsWith(".jpg") || fileName.EndsWith(".jpeg") || fileName.EndsWith(".bmp")
|| fileName.EndsWith(".gif") || fileName.EndsWith(".tif") || fileName.EndsWith(".tiff"));
}
}
@vineet1810
Copy link

Do you have the driver code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment