0

I have a code where I was uploading and downloading pdf files , and it was working perfectly , everywhere . but when i upload a file which is zip format, while downloading this file I am getting this download as without .zip extension , it just downloads as an entire folder . Don't know what i am missing.

In aws console, this is the name of file Form_8949_2018_2023-03-07_11:14:00.zip and under file type section it shows , zip too. but not able to download as zip. while downloading if i manually add ".zip" to download name it works fine as a zip. Attaching code snippets below . Please help

public String insertFile(String key, byte arr[], EnumS3FileMetaData fileMetaData) throws IOException {
        ByteArrayInputStream stream = null;
        try {
            System.out.println("key while storing file is " + key);
            Map<String, String> pdf_metadata = new HashMap<>();
            pdf_metadata.put("Content-Type", fileMetaData.value);
            pdf_metadata.put("Content-Disposition", "attachment");
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setUserMetadata(pdf_metadata);
            objectMetadata.setContentLength(arr.length);
            stream = new ByteArrayInputStream(arr);
            this.s3Client.putObject(this.bucketName, key, (InputStream) stream, objectMetadata);
            return this.getPresignedUrl(key);
        } catch (S3Exception e) {
            String errorMsg = String.format(ErrorConstants.FILE_UPLOAD_FAILURE, key);
            log.error(e.getMessage());
            throw new FileUploadFailureException(errorMsg);
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
    }


    public String getPresignedUrl(String key) {
        String errorMsg = ErrorConstants.GENERATE_PRESIGNED_URL_FAILED;
        try {
            GeneratePresignedUrlRequest generatePresignedUrlRequest =
                    new GeneratePresignedUrlRequest(bucketName, key)
                            .withExpiration(Date.from(Instant.now().plus(expiryTime, ChronoUnit.MINUTES)))
                            .withMethod(HttpMethod.GET);
            URL url = this.s3Client.generatePresignedUrl(generatePresignedUrlRequest);
            return url.toExternalForm();
        } catch (S3Exception exception) {
            log.error(errorMsg);
            throw new PresignedURLException(exception.getMessage());
        }
    }

@Getter
public enum EnumS3FileMetaData {
    PDF_META_DATA("application/pdf"),
    CSV_META_DATA("text/csv"),
    ZIP("application/zip"),
    TXF_META_DATA("text/txf");

    public String value;

    EnumS3FileMetaData(String value) {
        this.value = value;
    }
}

1 Answers1

0

It might be your content type, zip type has more than one recognized MIME Have a look here .rar, .zip files MIME Type

  • I think it is related to my browser , because i tried with chrome browser and only zip downloads there , but when i download using safari , whole folder appears . I think browser is downloading , and extracting itself. – Inderjeet Chawla Mar 07 '23 at 10:48