* @return
public static File downloadFileToLocalDir(String bucket, String key, String localDir) {
logger.info("Download object from s3, bucket: {}, key: {}, to local dir: {}", bucket, key, localDir);
try {
S3Object s3Object = s3Client.getObject(bucket, key);
File fileDir = new File(localDir);
if (!fileDir.exists()) {
fileDir.mkdirs();
File file = new File(localDir + key);
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(s3Object.getObjectContent(), outputStream);
s3Object.getObjectContent().close();
outputStream.close();
return file;
} catch (Exception e) {
logger.error("Fail to download file from amazon s3, bucket: {}, key: {}, to local dir: {}", bucket, key, localDir, e);
throw new UdsRuntimeException(UdsErrorCode.INTERNAL_SERVER_ERROR,
"Fail to download file from amazon s3, bucket: {}, key: {}, to local dir: {}", bucket, key, localDir);
对 Amazon S3 对象执行操作
https://docs.aws.amazon.com/zh_cn/sdk-for-java/v1/developer-guide/examples-s3-objects.html
使用 AWS SDK for Java 获取对象
https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/dev/RetrievingObjectUsingJava.html
Interface AmazonS3
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3.html
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3.html
使用 AWS SDK for Java 上传对象
https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/dev/UploadObjSingleOpJava.html