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;