Created
April 6, 2019 06:41
-
-
Save setu1421/7696c85d8dab0a94c4cd4e351ff22842 to your computer and use it in GitHub Desktop.
Utility helper for uploading large files to AWS S3 bucket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have the driver code?