直接上硬菜:
1.依賴
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.625</version>
</dependency>
2.編碼
public static String uploadToS3(MultipartFile file) throws IOException {
String perfix = "https://xxxxx.s3-us-west-1.amazonaws.com/";
String bucketName = "txxxxx";
if (file.isEmpty()) {
return "上傳文件不能爲空";
}
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(file.getContentType());
metadata.setContentLength(file.getSize());
String key = UUID.randomUUID().toString().replaceAll("-", "") + "." + getFileType(file.getOriginalFilename());
String fileTypeByContentType = getFileTypeByContentType(file.getContentType());
if ("image".equals(fileTypeByContentType)) {
bucketName = "xxx-img";
perfix = "https://xxx-img.s3-us-west-1.amazonaws.com/";
} else if ("video".equals(fileTypeByContentType)) {
bucketName = "xxx-video1";
perfix = "https://xxx-video1.s3-us-west-1.amazonaws.com/";
} else {
bucketName = "xxxx-other";
perfix = "https://xxx-other.s3-us-west-1.amazonaws.com/";
}
try {
//驗證名稱爲bucketName的bucket是否存在,不存在則創建
if (!checkBucketExists(s3Client, bucketName)) {
s3Client.createBucket(bucketName);
}
zhi
/*之前被誤導,一直上傳上此的文件,返回一個鏈接帶有有效期,而且最七天,各種想辦法,其實是寫法就錯誤了,
應該用下面的這種寫法withCannedAcl,設置ACL權限就好,希望大家避坑*/
//開始上傳文件
s3Client.putObject(new PutObjectRequest(bucketName, key, file.getInputStream(), metadata)
.withCannedAcl(CannedAccessControlList.PublicRead));
String url = perfix + key;
if (url == null) {
throw new BizException(GlobalExceptionEnum.SERVER_ERROR.getCode(), " can't get s3 file url!");
}
return url.toString();
} catch (AmazonServiceException ase) {
ase.printStackTrace();
log.info("====================================AWS S3 UPLOAD ERROR START======================================");
log.info("Caught an AmazonServiceException, which means your request made it "
+ "to Amazon S3, but was rejected with an error response for some reason.");
log.info("Caught an AmazonServiceException, which means your request made it "
+ "to Amazon S3, but was rejected with an error response for some reason.");
log.info("Error Message: " + ase.getMessage());
log.info("HTTP Status Code: " + ase.getStatusCode());
log.info("AWS Error Code: " + ase.getErrorCode());
log.info("Error Type: " + ase.getErrorType());
log.info("Request ID: " + ase.getRequestId());
log.info(ase.getMessage(), ase);
log.info("====================================AWS S3 UPLOAD ERROR END======================================");
throw new BizException(GlobalExceptionEnum.SERVER_ERROR.getCode(), "error occurs during upload to s3!");
} catch (AmazonClientException ace) {
log.info("====================================AWS S3 UPLOAD ERROR START======================================");
log.info("Caught an AmazonClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with S3, "
+ "such as not being able to access the network.");
log.info("Error Message: " + ace.getMessage());
log.info("====================================AWS S3 UPLOAD ERROR END======================================");
throw new BizException(GlobalExceptionEnum.SERVER_ERROR.getCode(), "error occurs during upload to s3!");
} finally {
}
}
就是這麼硬