springBatch, s3 下载与上传

s3配置

public class AWSConfig {

    @Value("${cloud.aws.credentials.accessKey}")
    private String accessKey;

    @Value("${cloud.aws.credentials.secretKey}")
    private String secretKey;

    @Value("${cloud.aws.region.static}")
    private String region;

    @Bean
    public BasicAWSCredentials basicAWSCredentials() {
        return new BasicAWSCredentials(accessKey, secretKey);
    }

    @Bean
    public AmazonS3 amazonS3Client(AWSCredentials awsCredentials) {
        return AmazonS3ClientBuilder.standard().withRegion(Regions.fromName(region))
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .build();
    }
}

s3上传

public class S3UploadTasklet implements Tasklet {

    @Autowired
    private AmazonS3 amazonS3Client;

    @Autowired
    private InboundBatchConfiguration batchConfig;

    @Override
    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) {
        File dirFile = new File(batchConfig.getZipPath());
        File uploadFile = Objects.requireNonNull(dirFile.listFiles())[0];
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        String bucketPath = "CIF/" + df.format(LocalDateTime.now()) + "/" + batchConfig.getInboundId() + "/";
        amazonS3Client.putObject(batchConfig.getBucket(), bucketPath + uploadFile.getName(), uploadFile);
        return RepeatStatus.FINISHED;
    }
}

s3下载

public class S3DownloadTasklet implements Tasklet {

    @Autowired
    private AmazonS3 amazonS3Client;

    @Autowired
    private OutboundBatchConfiguration batchConfig;

    @Override
    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) {

        ListObjectsRequest objectsRequest = new ListObjectsRequest().withBucketName(batchConfig.getBucket());
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        String prefix = batchConfig.getIfId() + "/" + df.format(LocalDateTime.now()) + "/" + batchConfig.getOutboundId() + "/";
        objectsRequest = objectsRequest.withPrefix(prefix);

        ObjectListing objects = amazonS3Client.listObjects(objectsRequest);
        for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
            if (objectSummary.getKey().endsWith(".zip")) {
                try {
                    S3Object s3Object = amazonS3Client.getObject(batchConfig.getBucket(), objectSummary.getKey());
                    S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent();
                    FileOutputStream fileOutputStream = new FileOutputStream(
                            batchConfig.getZipPath() + objectSummary.getKey().replace(prefix, ""));
                    byte[] readBuffer = new byte[1024];
                    int readLength;
                    while ((readLength = s3ObjectInputStream.read(readBuffer)) > 0) {
                        fileOutputStream.write(readBuffer, 0, readLength);
                    }
                    s3ObjectInputStream.close();
                    fileOutputStream.close();
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
        }
        return RepeatStatus.FINISHED;
    }
}

 

posted @ 2019-04-11 17:20  大腮鱼  阅读(486)  评论(0编辑  收藏  举报