我很晚才回答你的问题
我昨天为我的最新项目做的,看看下面的完整代码
假设当我们在 s3 上上传文件时,它返回上传文件的 ObjectKey,这里我创建了一个相同的类并命名为 FileKey。
package com.myprojectName.model.key;
import java.time.Instant;
import javax.persistence.Entity;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Entity
@NoArgsConstructor
public class FileKey {
private String fileObjectKey;
private String fileName;
private int fileSize;
private String fileType;
}
我存储在DownloadDetailsDTO中的presignedUrl的返回值
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.net.URL;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Builder
public class FileDownloadDetailsDTO {
private String name;
private Long size;
private String contentType;
private URL preSignedDownloadUrl;
public FileDownloadDetailsDTO(PreSignedUrlAndMetadata objectMetadata) {
this.name = objectMetadata.getName();
this.size = objectMetadata.getSize();
this.contentType = objectMetadata.getContentType();
this.preSignedDownloadUrl = objectMetadata.getUrl();
}
}
PreSignedUrlAndMetaData 包含在 s3 存储桶上创建的 Url,如果不确定请查看以下代码
public class PreSignedUrlAndMetadata {
private final URL url;
private final String name;
private final String contentType;
private final Long size;
}
以下方法将s3存储桶的每个文件作为zip条目存储到zip文件中,并返回一个zip文件的预签名URL(无需存储在本地temp中)
public FileDownloadDetailsDTO getDownloadFilesInZipDetails(String zipFileName, List<FileKey> files) {
PreSignedUrlAndMetadata preSignedUrlAndMetadata;
File zipFile = null;
try {
zipFile = File.createTempFile(zipFileName, "file");
try (FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos)) {
for (FileKey file : files) {
String name = null;
if (ObjectUtils.isNotEmpty(file.getFileName())) {
name = file.getFileName();
}
ZipEntry entry = new ZipEntry(name);
try (InputStream inputStream = getInputStreamForFileKey(file.getFileObjectKey())) {
zos.putNextEntry(entry);
IOUtils.copy(inputStream, zos);
zos.closeEntry();
}
}
}
try (FileInputStream fis = new FileInputStream(zipFile)) {
TempFileObjectKey fileObjectKey =uploadTemp(fis, zipFile.length());
preSignedUrlAndMetadata = new PreSignedUrlAndMetadata(url, metadata.getUserMetaDataOf(USER_METADATA_NAME), contentType, metadata.getContentLength());
}
} catch (Exception e) {
throw new ApplicationException("Error while creating zip file for " + archiveRequestDTO.getArchiveName(), e, ApplicationErrorCode.INTERNAL_SERVER_ERROR);
} finally {
FileUtils.deleteQuietly(zipFile);
}
return FileDownloadDetailsDTO.builder().name(archiveRequestDTO.getArchiveName() + ".zip")
.size(preSignedUrlAndMetadata.getSize()).preSignedDownloadUrl(preSignedUrlAndMetadata.getUrl()).build();
}
public InputStream getInputStreamForFileKey(String key) {
TempFileObjectKey tempFileObjectKey = new TempFileObjectKey(getActualPrefix(key));
return storageService.getInputStream(tempFileObjectKey);
}
String getActualPrefix(String prefix){
return prefix.replaceAll("_","/");
}
public TempFileObjectKey uploadTemp(InputStream inputStream, Long length) {
TempFileObjectKey tempFileObjectKey = s3StorageManager.buildTempFileFullKey();
ObjectMetadata objectMetadata = new ObjectMetadata();
if (length != null) {
objectMetadata.setContentLength(length);
}
Upload upload = com.amazonaws.services.s3.transfer.TransferManager.upload(getBucketName(abstractObjectKey), abstractObjectKey.getObjectKey(), inputStream, objectMetadata);
try {
upload.waitForCompletion();
} catch (InterruptedException e) {
throw new ApplicationException(e.getMessage(), e, ApplicationErrorCode.INTERNAL_SERVER_ERROR);
}
return tempFileObjectKey;
}
我希望这对你们有帮助。
您可以问我是否仍有任何疑问。
谢谢,