仅从Java中的S3存储桶获取文件名

仅从Java中的S3存储桶获取文件名,java,amazon-web-services,amazon-s3,minio,Java,Amazon Web Services,Amazon S3,Minio,我试图从S3存储桶中获取文件,然后在本地下载以进行处理。我能够做到这一点,使用下面的代码。但是,S3存储桶中可能有多个文件,我只需要文件名。 当前代码还返回文件夹名和文件名。有没有办法跳过文件夹,只获取文件名?对于单个文件,我手动传递文件名,但若我有多个文件,那个么就不可能了 文件位置:s3bucket/4275/input/Test.csv import io.minio.*; import io.minio.errors.*; import io.minio.messages.Item; i

我试图从S3存储桶中获取文件,然后在本地下载以进行处理。我能够做到这一点,使用下面的代码。但是,S3存储桶中可能有多个文件,我只需要文件名。 当前代码还返回文件夹名和文件名。有没有办法跳过文件夹,只获取文件名?对于单个文件,我手动传递文件名,但若我有多个文件,那个么就不可能了

文件位置:s3bucket/4275/input/Test.csv

import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Configuration
public class S3BucketConfig {
    @Value("${s3.bucket.accessKey}")
    private String s3BucketAccessKey;

    @Value("${s3.bucket.secretKey}")
    private String s3BucketSecretKey;

    @Value("${s3.bucket.endpoint}")
    private String s3BucketEndpoint;

    @Value("${s3.bucket.name}")
    private String s3BucketName;

    @Value("${dest.path.to.download}")
    private String destDownloadPath;

    private MinioClient minioClient;

    public void getS3BucketObject() throws IOException, NoSuchAlgorithmException,
            InvalidKeyException, ServerException, InsufficientDataException, InternalException, InvalidResponseException,
            XmlParserException, ErrorResponseException {

        //creating minioClient to access S3 bucket
        minioClient =
                MinioClient.builder()
                        .endpoint(s3BucketEndpoint)
                        .credentials(s3BucketAccessKey, s3BucketSecretKey)
                        .build();

        //check for bucket existance
        boolean found =
                minioClient.bucketExists(BucketExistsArgs.builder().bucket(s3BucketName).build());
        if (!found) {
           System.out.println("Bucket doesn't exist ");
        } else {
            // check for .csv files in the bucket
            Iterable<Result<Item>> results =
                    minioClient.listObjects(ListObjectsArgs.builder().bucket(s3BucketName).prefix("4275/input/").build());
            for (Result<Item> result : results) {
                if (result.get().objectName().endsWith("csv")) {
                    //loop for file presence if it is not there every 3 minutes for 5-6hours
                            System.out.println(result.get().objectName()); <--- output :4275/input/Test.csv
                            S3BucketDownlodObjects(result.get().objectName());
                }
            }
        }
    }

        public void S3BucketDownlodObjects(String fileObject ){
            try {
                minioClient.downloadObject(
                        DownloadObjectArgs.builder()
                                .bucket(s3BucketName)
                                .object(fileObject)
                                .filename(data\\Test.csv") --> this will place file on my local with Test.csv name
                                .build());
            } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e ) {
                e.printStackTrace();
            }
        }
}
import io.minio.*;
导入io.minio.errors.*;
导入io.minio.messages.Item;
导入org.springframework.beans.factory.annotation.Value;
导入org.springframework.context.annotation.Configuration;
导入java.io.IOException;
导入java.security.InvalidKeyException;
导入java.security.NoSuchAlgorithmException;
@配置
公共类S3BucketConfig{
@值(${s3.bucket.accessKey}”)
私有字符串s3BucketAccessKey;
@值(${s3.bucket.secretKey}”)
私有字符串s3BucketSecretKey;
@值(${s3.bucket.endpoint}”)
私有字符串s3BucketEndpoint;
@值(${s3.bucket.name}”)
私有字符串s3BucketName;
@值(${dest.path.to.download}”)
私有字符串下载路径;
私人微型客户;
public void getS3BucketObject()抛出IOException、NoSuchAlgorithmException、,
InvalidKeyException、ServerException、InsufficientDataException、InternalException、InvalidResponseException、,
XmlParserException,ErrorResponseException{
//创建minioClient以访问S3存储桶
微型客户=
MinioClient.builder()
.端点(s3BucketEndpoint)
.凭证(s3BucketAccessKey、s3BucketSecretKey)
.build();
//检查铲斗是否存在
布尔发现=
minioClient.bucketExists(BucketExistsArgs.builder().bucket(s3BucketName.build());
如果(!找到){
System.out.println(“Bucket不存在”);
}否则{
//检查bucket中的.csv文件
不可预测的结果=
minioClient.listObjects(ListObjectsArgs.builder().bucket(s3BucketName).prefix(“4275/input/”).build());
对于(结果:结果){
if(result.get().objectName().endsWith(“csv”)){
//如果文件不存在,则每3分钟循环一次,持续5-6小时

System.out.println(result.get().objectName());您是否可以提供一个示例说明您正在尝试执行的操作?您只是说希望从S3对象键(文件名)中删除路径要在本地保存它吗?难道你不能从Java代码中的字符串中去掉路径吗?谢谢!我在想库中是否有东西可以获得文件名,但我可以通过用普通Java方式去掉文件夹名来获得文件名。