Multipart upload from s3 using java Spring boot
Tags: aws, s3, java, springboot
Published: 2024-06-12 06:15:51

```java
   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;
    }

```