'Upload multiple file in amazon s3 bucket using java filechooser

I want to upload multiple file to Amazon s3 bucket by java file chooser. This code can upload a file to s3 but next time when I upload another file the previous file is replaced.

I know it is caused by the String key = "squre.jpg"; in the code. How to upload multiple file without replacing the previous one?

imageUpload.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            FileChooser fileChooser=new FileChooser();
            fileChooser.setInitialDirectory(new File("c:\\"));
            fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JPG Images","*.jpg"),
                    new FileChooser.ExtensionFilter("JPEG Images","*.jpeg"),
                    new FileChooser.ExtensionFilter("PNG Images","*.png"));
            File file=fileChooser.showOpenDialog(null);
            
            if (file!=null){
                try {
                     
AWSCredentials Credentials = new BasicAWSCredentials(
            "AWSAccessKeyId", 
            "AWSSecretKey");
    
    AmazonS3Client amazonS3Client = new AmazonS3Client(Credentials);
    String bucketName = "awsimagetrading";
    String key = "squre.jpg";
                    System.out.println("Uploading a new object to S3 from a file\n");
                    AmazonS3 s3client = new AmazonS3Client(Credentials);
                    s3client.putObject(new PutObjectRequest(bucketName,key,file));
                    URL url = amazonS3Client.generatePresignedUrl(bucketName,key,Date.from(Instant.now().plus(5,ChronoUnit.MINUTES)));
                    System.out.println(url);
                    //label.setText(url.toString());
                    
                } catch (AmazonClientException e) {
                    e.printStackTrace();
                }
            }
        }
    });


Solution 1:[1]

From your code it looks like you need to use filechooser's openMultipleDialog and then you can set key to file's name (file.getName()). Here is the modified code..

imageUpload.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            FileChooser  fileChooser=new FileChooser();
            fileChooser.setInitialDirectory(new File("c:\\"));
            fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JPG Images","*.jpg"),
                    new FileChooser.ExtensionFilter("JPEG Images","*.jpeg"),
                    new FileChooser.ExtensionFilter("PNG Images","*.png"));

            List<File> selectedFiles = fileChooser.showOpenMultipleDialog(null);
            if (selectedFiles != null) {
                for (File file : selectedFiles) {
                    try {

                        AWSCredentials Credentials = new BasicAWSCredentials(
                                "AWSAccessKeyId",
                                "AWSSecretKey");

                        AmazonS3Client amazonS3Client = new AmazonS3Client(Credentials);
                        String bucketName = "awsimagetrading";
                        String key = file.getName();
                        System.out.println("Uploading a new object to S3 from a file\n");
                        AmazonS3 s3client = new AmazonS3Client(Credentials);
                        s3client.putObject(new PutObjectRequest(bucketName,key,file));
                        URL url = amazonS3Client.generatePresignedUrl(bucketName,key,Date.from(Instant.now().plus(5,ChronoUnit.MINUTES)));
                        System.out.println(url);
                        //label.setText(url.toString());

                    } catch (AmazonClientException e) {
                        e.printStackTrace();
                    }
                }
        }
    });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jshcode