spring项目s3文件夹下载
时间: 2023-10-04 20:13:55 浏览: 152
spring文件夹
在Spring项目中实现S3文件夹下载,你可以按照以下步骤进行操作:
1. 首先,确保你已经配置了AWS SDK和相关的依赖项。你可以在`pom.xml`文件中添加以下依赖项:
```xml
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3transfer</artifactId>
</dependency>
```
2. 创建一个S3文件下载的Controller类,例如`S3FileDownloadController.java`,并添加以下代码:
```java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@Controller
public class S3FileDownloadController {
@Value("${s3.bucket.name}")
private String bucketName;
@GetMapping("/download/{folderName}")
public ResponseEntity<InputStreamResource> downloadFolder(@PathVariable String folderName) throws FileNotFoundException {
// 创建一个临时文件夹,用于存储下载的文件夹
File tempFolder = new File("temp");
if (!tempFolder.exists()) {
tempFolder.mkdirs();
}
// 下载S3文件夹
S3Client s3Client = S3Client.builder().region(Region.US_EAST_1).build();
ListObjectsRequest listObjectsRequest = ListObjectsRequest.builder()
.bucket(bucketName)
.prefix(folderName + "/") // 指定文件夹路径
.build();
ListObjectsResponse listObjectsResponse = s3Client.listObjects(listObjectsRequest);
List<S3Object> s3Objects = listObjectsResponse.contents();
for (S3Object s3Object : s3Objects) {
// 下载文件
S3Object fullObject = s3Client.getObject(GetObjectRequest.builder()
.bucket(bucketName)
.key(s3Object.key())
.build());
// 将下载的文件保存到临时文件夹中
File file = new File(tempFolder.getPath() + "/" + s3Object.key());
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream is = fullObject.getObjectContent();
while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
fos.close();
}
// 创建压缩文件
String zipFileName = folderName + ".zip";
ZipUtil.pack(tempFolder, new File(zipFileName));
// 设置响应头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", zipFileName);
// 返回文件流
File zipFile = new File(zipFileName);
InputStreamResource resource = new InputStreamResource(new FileInputStream(zipFile));
return ResponseEntity.ok()
.headers(headers)
.contentLength(zipFile.length())
.body(resource);
}
}
```
3. 在`application.properties`文件中添加以下配置:
```properties
s3.bucket.name=your-bucket-name
```
确保将`your-bucket-name`替换为你的S3存储桶名称。
4. 现在,你可以通过调用`/download/{folderName}`接口来下载S3文件夹。例如,如果你要下载名为`example-folder`的文件夹,可以发送GET请求到`/download/example-folder`。
这样,Spring项目就可以实现S3文件夹的下载了。请注意,上述代码仅提供了一个基本的示例,你可能需要根据自己的需求进行修改和改进。
阅读全文