DEV Community

Mallikarjun H T
Mallikarjun H T

Posted on

Multipart upload from s3 using java Spring boot

   public boolean multipartUploadWithS3Client(String accessKey, String secretKey, String region, String bucketName, String key, String filePath) {

        log.info("for file {}, upload starting for bucket {}, to s3 location {} ", filePath, bucketName, key);
        StopWatch watch = new StopWatch();
        watch.start();
        // S3 Client
        S3Client s3Client = getAWSS3Client(accessKey, secretKey, region);

        // Initiate the multipart upload.
        CreateMultipartUploadResponse createMultipartUploadResponse = s3Client.createMultipartUpload(b -> b
                .bucket(bucketName)
                .key(key));
        // get the upload id
        String uploadId = createMultipartUploadResponse.uploadId();

        log.info("Upload ID: {}", uploadId);

        // Upload the parts of the file.
        int partNumber = 1;
        List<CompletedPart> completedParts = new ArrayList<>();
        ByteBuffer bb = ByteBuffer.allocate(1024 * 1024 * 10); // 10 MB byte buffer

        // Read the file and upload the parts.
        try (RandomAccessFile file = new RandomAccessFile(filePath, "r")) {
            long fileSize = file.length();
            long position = 0;
            while (position < fileSize) {
                file.seek(position);
                int read = file.getChannel().read(bb);

                bb.flip(); // Swap position and limit before reading from the buffer.
                UploadPartRequest uploadPartRequest = UploadPartRequest.builder()
                        .bucket(bucketName)
                        .key(key)
                        .uploadId(uploadId)
                        .partNumber(partNumber)
                        .build();

                UploadPartResponse partResponse = s3Client.uploadPart(
                        uploadPartRequest,
                        RequestBody.fromByteBuffer(bb));

                CompletedPart part = CompletedPart.builder()
                        .partNumber(partNumber)
                        .eTag(partResponse.eTag())
                        .build();
                completedParts.add(part);

                bb.clear();
                position += read;
                partNumber++;
            }
        } catch (IOException e) {
            log.error("Error while uploading file to s3", e);
            return false;
        }

        log.info("Completed parts: {}", completedParts.size());
        // Complete the multipart upload.
        s3Client.completeMultipartUpload(b -> b
                .bucket(bucketName)
                .key(key)
                .uploadId(uploadId)
                .multipartUpload(CompletedMultipartUpload.builder().parts(completedParts).build()));

        watch.stop();
        String timeTakenInHMSFormat = DateTimeFormatter.ofPattern("HH:mm:ss.SSS")
                .withZone(ZoneId.of("UTC"))
                .format(Instant.ofEpochMilli(watch.getTotalTimeMillis()));
        log.info("Time taken to upload file {} to s3 bucket {}for this location {} is: {}", filePath, bucketName, key,  timeTakenInHMSFormat);
        return true;
    }

Enter fullscreen mode Exit fullscreen mode

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more