Search code examples
javaamazon-s3spring-webflux

Amazon S3 Transfer Manager v2.x and metadata


I would like to use the new version (2.x) of Amazon S3 Transfer Manager to upload a directory to a bucket. I can do it without problems except for metadata. I need to set some metadata (content-type and content-encoding) for each file. I can do it with the version 1.x (below an example) but not with the new version

MultipleFileUpload upload = tm.uploadDirectory(
                                bucket,
                                prefix,
                                filePath.toFile(),
                                true,
                                (file, metadata) -> {
                          
                                    String mimeType = "application/json";
                                    try {
                                        mimeType = Files.probeContentType(Paths.get(file.toURI()));

                                        if (mimeType == null) {
                                            if (file.getAbsolutePath().contains("bulkdata")) {
                                                mimeType = "application/octet-stream";
                                            }
                                            if (file.getAbsolutePath().contains(".raw")) {
                                                mimeType = "application/octet-stream";
                                            }
                                            if (file.getAbsolutePath().contains("frames")) {
                                                mimeType = "multipart/related";
                                            }
                                            if (file.getAbsolutePath().contains("thumbnail")) {
                                                mimeType = "image/jpeg";
                                            }
                                        }
                                    } catch (Exception ex) {
                                        mimeType = "application/octet-stream";
                                    }

                                    if (file.getName().endsWith(".gz")) {
                                        metadata.setContentEncoding("gzip");
                                        mimeType = "application/json";
                                    }

                                    metadata.setContentType(mimeType);
                                }
                        );

Do you know a way to accomplish that?


Solution

  • I found the solution.

    As suggested by @luk2302 the way is to implement the uploadFileRequestTransformer.

    UploadDirectoryRequest request = UploadDirectoryRequest.builder()
        .bucket(bucket)
        .s3Prefix(prefix)
        .source(filePath)
        .uploadFileRequestTransformer(uploadFileRequestBuilder -> {
             UploadFileRequest uploadFileRequest = uploadFileRequestBuilder.build();
    
             File file = uploadFileRequest.source().toFile();
                        
             String mimeType = "application/json";
             try {
                 mimeType = Files.probeContentType(uploadFileRequest.source());
    
                 if (mimeType == null) {
                     if (file.getAbsolutePath().contains("bulkdata")) {
                         mimeType = "application/octet-stream";
                     }
                     if (file.getAbsolutePath().contains(".raw")) {
                         mimeType = "application/octet-stream";
                     }
                     if (file.getAbsolutePath().contains("frames")) {
                         mimeType = "multipart/related";
                     }
                     if (file.getAbsolutePath().contains("thumbnail")) {
                         mimeType = "image/jpeg";
                     }
                 }
             } catch (Exception ex) {
                 mimeType = "application/octet-stream";
             }
    
             PutObjectRequest.Builder putObjectRequestBuilder = uploadFileRequest.putObjectRequest().toBuilder();
             if (file.getName().endsWith(".gz")) {
                 putObjectRequestBuilder.contentEncoding("gzip");
    
                 mimeType = "application/json";
             }
    
             putObjectRequestBuilder.contentType(mimeType);
    
             uploadFileRequestBuilder.putObjectRequest(putObjectRequestBuilder.build());
        }).build();