java 从s3下载文件目录并打包成zip

2 篇文章 0 订阅

java 如何从s3直接下载文件目录,然后打包成zip?
废话少说,直接上代码。
添加依赖:

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>s3</artifactId>
    <version>2.17.77</version>
</dependency>

api接口(前端访问该接口直接下载zip):

	// bucket name
	private static final String BUCKET_NAME = "qq";
	// 文件前缀(文件夹的fileKey),下载的文件均以该fileKey为前缀
	// 比如:2023/05/22/100156/11111    2023/05/22/100156/22222   2023/05/22/100156/33333
    private static final String PREFIX = "2023/05/22/100156";
    // endpoint 
    private static final String endpoint = "http://192.168.1.123:1234/";
    // accessKey 
    private static final String accessKey = "accessKey";
    // secretKey 
    private static final String secretKey = "secretKey";

    private static final int BUFFER_SIZE = 8192;

    @GetMapping("/download")
    public void download(HttpServletResponse response) throws URISyntaxException {
        S3Client s3Client = S3Client.builder()
                .region(Region.US_EAST_1)
                .credentialsProvider(DefaultCredentialsProvider.create())
                .credentialsProvider(() -> new AwsCredentials() {
                    @Override
                    public String accessKeyId() {
                        return accessKey;
                    }
                    @Override
                    public String secretAccessKey() {
                        return secretKey;
                    }
                })
                .endpointOverride(new URI(endpoint))
                .build();

        ListObjectsV2Request listRequest = ListObjectsV2Request.builder()
                .bucket(BUCKET_NAME)
                .prefix(PREFIX)
                .build();

        ListObjectsV2Response listResponse = s3Client.listObjectsV2(listRequest);

        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=\"your-zip-file.zip\"");

        try (ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream())) {
            for (S3Object s3Object : listResponse.contents()) {
                String key = s3Object.key();

                try (BufferedInputStream inputStream = new BufferedInputStream(
                        s3Client.getObject(GetObjectRequest.builder()
                                .bucket(BUCKET_NAME)
                                .key(key)
                                .build()))) {

                    String entryName = key.substring(PREFIX.length());
                    zipOutputStream.putNextEntry(new ZipEntry("子文件的文件名+后缀"));

                    byte[] buffer = new byte[BUFFER_SIZE];
                    int bytesRead;

                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        zipOutputStream.write(buffer, 0, bytesRead);
                    }
                    zipOutputStream.closeEntry();
                    System.out.println("Added to ZIP: " + entryName);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("ZIP file created successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
        s3Client.close();
    }

为了防止有人导包导错,这里把导包也贴一下

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
import software.amazon.awssdk.services.s3.model.S3Object;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值