Multipart upload to Amazon S3 overwrites the last part

0 votes

I am doing a multipart upload with the .NET client library. The problem is that each part sent to S3 is overwriting the last part. Here's what I've got:

var fileTransferUtility = new TransferUtility(_s3Client);
var request = new TransferUtilityUploadRequest
    {
        BucketName = "mybucket",
        InputStream = stream,
        StorageClass = S3StorageClass.ReducedRedundancy,
        PartSize = stream.Length,//stream is 10,000 bytes at a time
        Key = fileName
    };

What am I doing wrong? Can someone guide me on this?

Apr 23, 2022 in Others by Kichu
• 19,040 points
784 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Here's the working code for doing the multipart upload.

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
        {
            _s3Client.CompleteMultipartUpload(completeRequest);
        }
        catch
        {
            //do some logging and return null response
            return null;
        }
    }

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


This will help you implement a chunked file upload to S3.

answered Apr 24, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In Others

0 votes
1 answer

How to install a GUI on Amazon AWS EC2 or EMR with the Amazon AMI

The top-level script for installing the GUI ...READ MORE

answered Mar 9, 2022 in Others by gaurav
• 23,260 points
1,283 views
0 votes
1 answer

How to get password of the Amazon aws rds user?

Follow these procedures to update the master ...READ MORE

answered Mar 9, 2022 in Others by gaurav
• 23,260 points

edited Jun 20, 2023 by Khan Sarfaraz 12,337 views
0 votes
1 answer

NodeJS Amazon AWS S3 getObject how to send file in API response to download

Server Side const aws = require('aws-sdk'); router.get('/getfilefroms3', async (req, ...READ MORE

answered Mar 24, 2022 in Others by gaurav
• 23,260 points
9,962 views
0 votes
0 answers

How to find the username of the instance launched in amazon-ec2?

I m having trouble connecting to the ...READ MORE

Apr 4, 2022 in Others by Kichu
• 19,040 points
1,775 views
0 votes
0 answers

How to find the username of the instance launched in amazon-ec2?

I m having trouble connecting to the ...READ MORE

Apr 5, 2022 in Others by Kichu
• 19,040 points
774 views
0 votes
0 answers

How to disable Amazon S3 raw endpoint access

Hosting  a static website on S3 : You ...READ MORE

Apr 10, 2022 in Others by Kichu
• 19,040 points
1,132 views
0 votes
1 answer

AWS S3 uploading hidden files by default

versioning is enabled in your bucket. docs.aws.amazon.com/AmazonS3/latest/user-guide/….... the ...READ MORE

answered Oct 4, 2018 in AWS by Priyaj
• 58,020 points
6,265 views
–1 vote
1 answer

How to decrypt the encrypted S3 file using aws-encryption-cli --decrypt

Use command : aws s3 presign s3://mybucket/abc_count.png you get ...READ MORE

answered Oct 22, 2018 in AWS by Priyaj
• 58,020 points
5,411 views
0 votes
1 answer

Import my AWS credentials using python script

Using AWS Cli  Configure your IAM user then ...READ MORE

answered Nov 16, 2018 in AWS by Jino
• 5,820 points
3,004 views
0 votes
2 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP