• 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;
        }
    }
  • 相关阅读:
    php数组之选择排序算法参考
    php数组之插入排序算法参考
    php数组之冒泡排序算法参考
    MySQL查询中的条件控制(where,group by,having,order by,limit,from,exists)
    第四次作业
    第三次作业
    第二次作业
    作业
    DevExpress TreeList使用心得
    项目开发总结:解决多线程窗体与主风格不一致问题
  • 原文地址:https://www.cnblogs.com/CuiHongYu/p/10691003.html
Copyright © 2020-2023  润新知