How can one upload a large string or an array of bytes to an AWS S3 bucket in parts using Golang?
For instance, if I have a string with billions of characters and I want to avoid uploading it all at once due to memory constraints, how can I achieve this?
My use case involves exporting specific data from a database, encoding that data as JSON objects (stored as strings), and sequentially uploading these parts to an AWS S3 bucket until the complete JSON file is generated and uploaded. The intention is for the final file uploaded to S3 to be downloadable in JSON format.
I encountered this issue a few days ago and am sharing the question along with its answer here to assist other users who might face the same or a similar challenge. If you believe there are any further improvements needed for clarity, please let me know!
The process of uploading large data to an AWS S3 bucket in parts can be achieved using the
s3managerpackage. The key is to utilize theBodyfield of thes3manager.UploadInputstruct, which accepts anio.Reader.You can use
io.Pipewhich provides you a reader (which you’ll attach to theBodyfield ofUploadInput) and a writer (which you’ll write each chunk to). As you write each chunk, it will be uploaded to your S3 bucket. Remember to handle any errors that might arise during the upload process. Also, don’t forget to close the writer so S3 can finish the upload process.Here’s an example working code you can start with:
Feel free to adapt this code to your specific scenario. This approach allows you to upload large data to S3 in manageable chunks, avoiding memory issues and ensuring a smoother upload process.