Large Multipart.File uploads directly to AWS S3

I have a go-chi (golang) web application that is hosted behind nginx (reverse proxy – proxy_pass directives), that accepts file uploads that are then uploaded to AWS S3.

The request with a file is sent via a POST request with a content-type of multipart/form-data.

The application then receives the request and creates a multipart.file type, this entire file from my understanding is allocated in memory.

I would like to accept files that can be in the range of 5gb – 10gb, and cannot increase server memory of this size, as I only have 2gb.

S3 has multipart upload methods, which will allow to uploading of parts of the file to S3.

I have implemented this multipart file upload in go below, The issue I am facing is I feel there is duplicated effort since go-http library is already allocating memory in parts to make the *multipart.file type essentially doing what I am doing below.

  1. Is there a way to take these part files created by the go-http library in the ParseMultipartForm() and upload to S3 directly to avoid the the allocation of the entire file in memory? (uploading the file stream as it comes in)

  2. Would it be better to just avoid the parsing and allocating of the mutlipart.file in go-http and load it directly to disk, then use the multipart s3 methods to upload the file parts? Although I believe I would still need to stream the entire file in memory to do so, which will not work as I do not have enough memory

  3. Is Nginx parsing and parting this file as well, before passing it to the webapp?

I have implemented a light POC of S3 multipart uploads below.

partBufSize := int64(10000000)
byteOffset := int64(0)

for { 
    remainingStreamBytes := uploadSize - bytesOffset
    if remainingStreamBytes < partBufSize {
        partBufSize = remainingStreamBytes
    }

    _, _ = uploadedFile.Seek(bytesOffset, io.SeekStart)

    filePartBuffer := make([]byte, partBufSize)
    _, err := uploadedFile.Read(filePartBuffer)
    if err != nil {
        if err == io.EOF {
            break
        }
        log.Error().Msg(err.Error())
    }

    uploadPartRes, err := s3Client.UpParts(&aws.PartObjectInput{
        Key:      uploadPartsRequest.Key,
        Body:     bytes.NewReader(filePartBuffer),
        PartNum:  aws.Int64(int64(count)),
        UpId:     uploadPartsRequest.UploadId,
        PartSize: aws.Int64(partBufSize),
    })
    if err != nil {
        log.Error().Msg(err.Error())
    }
    bytesOffset = bytesOffset + partBufSize

    compParts = append(compParts, &s3.CompletedPart{
        ETag:       uploadPartRes.ETag,
        PartNumber: aws.Int64(int64(count)),
    })
    count++
}

6

The application then receives the request and creates a multipart.file type, this entire file from my understanding is allocated in memory.

I do not quite get this. If this is the way you’ve coded that, then just don’t do that: Go provides mime/multipart.Reader which can be used to read multipart/form-data-encoded payloads piecemeal.
To do that, you roll like this:

  1. Get the value of the Content-Type header field and parse it with the mime.ParseMediaType function to get the value of its “boundary” parameter—a string used to separate parts in the multipart payload.
  2. Create a multipart.Reader out of the request’s body and the boundary string.
  3. Iterate over the parts; at each part, you’ll get another io.Reader which will provide the part’s bytes. Do whatever you want with those bytes—possibly shoveling them into an active POST request using io.Copy or io.CopyN.

This way you will control the exact amount of buffering you want to provide.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật