0

I am trying to upload large images to AWS S3 using the Multipart Upload API. From UI, i am sending the chunks(blob) of an image and when the last part arrives, completing the upload and getting the upload file url. It is working very nicely.

Sample Code:

public UploadPartResponse UploadChunk(Stream stream, string fileName, string uploadId, List<PartETag> eTags, int partNumber, bool lastPart)
{
    stream.Position = 0;

    //Step 1: build and send a multi upload request
    if (partNumber == 1)
    {
        var initiateRequest = new InitiateMultipartUploadRequest
        {
            BucketName = _settings.Bucket,
            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 = _settings.Bucket,
                            Key = fileName,
                            UploadId = uploadId,
                            PartNumber = partNumber,
                            InputStream = stream,
                            IsLastPart = lastPart,
                            PartSize = stream.Length
                        };

    var response = _s3Client.UploadPart(uploadRequest);

    //Step 3: build and send the multipart complete request
    if (lastPart)
    {
        eTags.Add(new PartETag
        {
            PartNumber = partNumber,
            ETag = response.ETag
        });

        var completeRequest = new CompleteMultipartUploadRequest
        {
            BucketName = _settings.Bucket,
            Key = fileName,
            UploadId = uploadId,
            PartETags = eTags
        };

        try
        {
            var res = _s3Client.CompleteMultipartUpload(completeRequest);
            return res.Location;
        }
        catch
        {
            //do some logging and return null response
            return null;
        }
    }

    response.ResponseMetadata.Metadata["uploadid"] = uploadRequest.UploadId;
    return response;
}

Now, i need to get the thumbnail of the uploaded image and upload that image too in a Thumbnails directory.

So basically, when the last part(chunk) arrives for the original image, i am completing the upload and retrieving the file url. At that time, i need to upload the thumbnail also and get back the thumbnail url.

I saw that people are referring of lambda function but don't know how to incorporate into my multipart api code setup.

Can anyone give me some direction here? Thanks in advance.

Setu Kumar Basak
  • 11,460
  • 9
  • 53
  • 85
  • 1
    How about doing it automatically with Lambda once the original image is uploaded? See: [Tutorial: Using AWS Lambda with Amazon S3 - AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html) – John Rotenstein Mar 10 '19 at 00:24
  • Is there any sample code with C#? I want to return the thumbnail url from WCF service. – Setu Kumar Basak Mar 10 '19 at 03:34
  • 1
    That tutorial has sample code in Node, Python and Java. You can use it standalone, so it doesn't need to be written in the same language as the rest of your code base. The tutorial will create a Lambda function that creates a thumbnail in another bucket whenever an image is uploaded. It won't "return" a URL, but you can assume that the same URL exists in the other bucket. You could also modify the code to use a different path in the same bucket (be sure to modify the Event configuration accordingly so it doesn't produce an infinite loop). – John Rotenstein Mar 10 '19 at 03:38
  • The problem is i can't use it as standalone because of the project requirement :( . Somehow need to get the thumbnail url and save it to database with the original file url – Setu Kumar Basak Mar 10 '19 at 05:34
  • If the "project requirement" is to get a URL, then you can save the URL immediately after uploading the file. The Lambda process will produce the thumbnail asynchronously, in a known location, so you will know the expected URL at the time of upload. – John Rotenstein Mar 10 '19 at 08:24
  • I am very new in aws and getting confused. I could see i have to write a function there in node.js or python or java. And this lambda function will trigger automatically to upload a thumbnail image. Now point here is that, i upload zip, image, video files in the same bucket. But i need thumbnails for the images only. Is it possible? It will be better for me if you can show me with more details. I am getting lost in the aws tutorials. – Setu Kumar Basak Mar 10 '19 at 14:56
  • Another thing is that, is there any way to do it with MultipartUpload API if it is not affordable to use AWS lamda? – Setu Kumar Basak Mar 10 '19 at 16:16
  • I am confused about what you are asking. And, by the way, using Lambda for resizing will either be very cheap or free (due to free usage tier). – John Rotenstein Mar 10 '19 at 19:58
  • I will try the AWS Lambda function – Setu Kumar Basak Mar 11 '19 at 04:09

0 Answers0