【问题标题】:Upload multiple file in amazon s3 bucket using java filechooser使用 java filechooser 在 amazon s3 存储桶中上传多个文件
【发布时间】:2022-04-01 16:34:45
【问题描述】:

我想通过 java 文件选择器将多个文件上传到 Amazon s3 存储桶。此代码可以将文件上传到 s3,但下次我上传另一个文件时,之前的文件会被替换。

我知道这是由代码中的String key = "squre.jpg"; 引起的。如何在不替换前一个的情况下上传多个文件?

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();
                }
            }
        }
    });

【问题讨论】:

  • 是什么阻止您为每次上传使用不同的密钥?还是我在这里遗漏了什么?

标签: java amazon-web-services amazon-s3


【解决方案1】:

从您的代码看来,您需要使用文件选择器的 openMultipleDialog,然后您可以将密钥设置为文件名 (file.getName())。这是修改后的代码..

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();
                    }
                }
        }
    });

【讨论】:

    猜你喜欢
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2018-04-04
    • 2014-11-11
    相关资源
    最近更新 更多