zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    类和对象
    类和对象1
    常见的子串问题
    常见的算法问题全排列
    第六届蓝桥杯java b组第五题
    第六届蓝桥杯java b组第四题
    第六届蓝桥杯java b组第三题
    第六届蓝桥杯java b组第二题
    第六届蓝桥杯java b组第一题
    第八届蓝桥杯java b组第三题
  • 原文地址:https://www.cnblogs.com/CuiHongYu/p/10691003.html
Copyright © 2011-2022 走看看