如何实现多文件上传至S3并返回文件URL及名称?
解决方案
1. 单个文件上传后获取Object URL
上传完成后,直接通过AmazonS3客户端的getUrl()方法即可获取文件的访问链接。如果你的S3对象是公开可读的,这个URL可以直接访问;如果是私有对象,可生成预签名URL提供临时访问权限。
修改后的单文件上传代码
import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.transfer.TransferManager; import com.amazonaws.services.s3.transfer.TransferManagerBuilder; import com.amazonaws.services.s3.transfer.Upload; import java.io.File; import java.net.URL; public class S3Uploader { public static void main(String[] args) { String region = "your-region"; // 替换为你的S3区域,如us-east-1 String mediaBucket = "your-bucket-name"; // 替换为你的存储桶名称 String keyName = "your-object-key"; // 替换为S3中的对象键(文件名) String filePath = "local-file-path"; // 替换为本地文件路径 try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withRegion(region) .withCredentials(new ProfileCredentialsProvider()) .build(); TransferManager tm = TransferManagerBuilder.standard() .withS3Client(s3Client) .build(); Upload upload = tm.upload(mediaBucket, keyName, new File(filePath)); System.out.println("Object upload started"); upload.waitForCompletion(); System.out.println("Object upload complete"); // 获取文件的访问URL URL objectUrl = s3Client.getUrl(mediaBucket, keyName); System.out.println("文件访问URL: " + objectUrl.toString()); System.out.println("文件名称: " + keyName); } catch (AmazonServiceException e) { e.printStackTrace(); } catch (SdkClientException | InterruptedException e) { e.printStackTrace(); } } }
2. 扩展为多文件上传并返回URL与名称
遍历本地文件列表,逐个执行上传逻辑,将每个文件的名称和URL收集到列表中返回即可。
多文件上传示例代码
import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.transfer.TransferManager; import com.amazonaws.services.s3.transfer.TransferManagerBuilder; import com.amazonaws.services.s3.transfer.Upload; import java.io.File; import java.net.URL; import java.util.ArrayList; import java.util.List; // 存储上传结果的实体类 class UploadResult { private String fileName; private String fileUrl; public UploadResult(String fileName, String fileUrl) { this.fileName = fileName; this.fileUrl = fileUrl; } // Getter方法 public String getFileName() { return fileName; } public String getFileUrl() { return fileUrl; } } public class S3MultiUploader { public static List<UploadResult> uploadFilesToS3(String region, String bucketName, List<File> localFiles) { List<UploadResult> results = new ArrayList<>(); try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withRegion(region) .withCredentials(new ProfileCredentialsProvider()) .build(); TransferManager tm = TransferManagerBuilder.standard() .withS3Client(s3Client) .build(); for (File file : localFiles) { // 使用本地文件名作为S3对象键,也可自定义键名 String keyName = file.getName(); Upload upload = tm.upload(bucketName, keyName, file); System.out.println("开始上传文件: " + file.getName()); upload.waitForCompletion(); System.out.println("文件上传完成: " + file.getName()); // 获取URL并添加到结果列表 URL objectUrl = s3Client.getUrl(bucketName, keyName); results.add(new UploadResult(file.getName(), objectUrl.toString())); } } catch (AmazonServiceException e) { e.printStackTrace(); } catch (SdkClientException | InterruptedException e) { e.printStackTrace(); } return results; } // 测试方法 public static void main(String[] args) { String region = "your-region"; String bucketName = "your-bucket-name"; // 构造本地文件列表 List<File> files = new ArrayList<>(); files.add(new File("path/to/file1.jpg")); files.add(new File("path/to/file2.pdf")); List<UploadResult> uploadResults = uploadFilesToS3(region, bucketName, files); // 打印所有上传结果 System.out.println("\n所有文件上传结果:"); for (UploadResult result : uploadResults) { System.out.println("文件名: " + result.getFileName() + " | 访问URL: " + result.getFileUrl()); } } }
3. 私有对象的预签名URL生成(可选)
如果S3对象是私有访问权限,直接通过getUrl()获取的URL无法直接访问,可生成预签名URL提供临时访问权限:
// 生成有效期为1小时的预签名URL java.util.Date expiration = new java.util.Date(); long expTimeMillis = expiration.getTime(); expTimeMillis += 1000 * 60 * 60; // 1小时 expiration.setTime(expTimeMillis); URL presignedUrl = s3Client.generatePresignedUrl(bucketName, keyName, expiration); System.out.println("预签名访问URL: " + presignedUrl.toString());
内容的提问来源于stack exchange,提问作者Teletubbies
相关产品推荐
相关产品推荐

