|
|
|
@@ -1,11 +1,24 @@ |
|
|
|
package com.simple.controller; |
|
|
|
|
|
|
|
|
|
|
|
import com.amazonaws.AmazonClientException; |
|
|
|
import com.amazonaws.AmazonServiceException; |
|
|
|
import com.amazonaws.auth.AWSCredentials; |
|
|
|
import com.amazonaws.auth.AWSCredentialsProvider; |
|
|
|
import com.amazonaws.auth.BasicAWSCredentials; |
|
|
|
import com.amazonaws.services.s3.AmazonS3; |
|
|
|
import com.amazonaws.services.s3.AmazonS3ClientBuilder; |
|
|
|
import com.amazonaws.services.s3.model.CannedAccessControlList; |
|
|
|
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; |
|
|
|
import com.amazonaws.services.s3.model.ObjectMetadata; |
|
|
|
import com.amazonaws.services.s3.model.PutObjectRequest; |
|
|
|
import com.simple.common.ResultData; |
|
|
|
import com.simple.config.AwsProperty; |
|
|
|
import io.swagger.annotations.Api; |
|
|
|
import io.swagger.annotations.ApiOperation; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.web.bind.annotation.PostMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
@@ -17,6 +30,7 @@ import java.io.BufferedInputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.net.URL; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.UUID; |
|
|
|
@@ -34,6 +48,9 @@ public class UploadController { |
|
|
|
@Value("${fileUpload.server}") |
|
|
|
private String server; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private AwsProperty awsProperty; |
|
|
|
|
|
|
|
/** |
|
|
|
* 上传文件 |
|
|
|
* |
|
|
|
@@ -41,8 +58,7 @@ public class UploadController { |
|
|
|
* @return |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
@PostMapping(value = "/fileUpload", consumes = "multipart/*" |
|
|
|
, headers = "content-type=multipart/form-data") |
|
|
|
@PostMapping(value = "/fileUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data") |
|
|
|
@ApiOperation("上传文件") |
|
|
|
public ResultData fileUpload(@RequestParam("file") MultipartFile multiReq) { |
|
|
|
ResultData data = new ResultData(); |
|
|
|
@@ -94,5 +110,77 @@ public class UploadController { |
|
|
|
return data; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 上传文件 |
|
|
|
* |
|
|
|
* @param multiReq |
|
|
|
* @return |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
@PostMapping(value = "/awsFileUpload", consumes = "multipart/*", headers = "content-type=multipart/form-data") |
|
|
|
@ApiOperation("上传文件") |
|
|
|
public ResultData awsfileUpload(@RequestParam("file") MultipartFile multiReq) { |
|
|
|
ResultData data = new ResultData(); |
|
|
|
|
|
|
|
|
|
|
|
ObjectMetadata metadata = new ObjectMetadata(); |
|
|
|
metadata.setContentType(multiReq.getContentType()); |
|
|
|
metadata.setContentLength(multiReq.getSize()); |
|
|
|
|
|
|
|
FileOutputStream fos = null; |
|
|
|
BufferedInputStream fs = null; |
|
|
|
try { |
|
|
|
AmazonS3 s3 = AmazonS3ClientBuilder.standard() |
|
|
|
.withRegion(awsProperty.getClientRegion()) |
|
|
|
.withCredentials(new AWSCredentialsProvider() { |
|
|
|
@Override |
|
|
|
public AWSCredentials getCredentials() { |
|
|
|
return new BasicAWSCredentials(awsProperty.getAccess(), awsProperty.getSecret()); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void refresh() { |
|
|
|
|
|
|
|
} |
|
|
|
}) |
|
|
|
.build(); |
|
|
|
String fileName = UUID.randomUUID().toString(); |
|
|
|
int dot = multiReq.getOriginalFilename().lastIndexOf('.'); |
|
|
|
fileName = fileName + multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); |
|
|
|
|
|
|
|
s3.putObject( |
|
|
|
new PutObjectRequest(awsProperty.getBucketName(), fileName, multiReq.getInputStream(), metadata) |
|
|
|
.withCannedAcl(CannedAccessControlList.PublicRead)); |
|
|
|
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(awsProperty.getBucketName(), fileName); |
|
|
|
URL url = s3.generatePresignedUrl(urlRequest); |
|
|
|
logger.info(url.toString()); |
|
|
|
|
|
|
|
data.code = ResultData.SUCCESS; |
|
|
|
Map<String, String> map = new HashMap<>(); |
|
|
|
map.put("imgarray", url.toString()); |
|
|
|
data.data = map; |
|
|
|
return data; |
|
|
|
} catch (AmazonServiceException ase) { |
|
|
|
data.code = ResultData.ERROR; |
|
|
|
logger.warn("Caught an AmazonServiceException, which " + |
|
|
|
"means your request made it " + |
|
|
|
"to Amazon S3, but was rejected with an error response" + |
|
|
|
" for some reason."); |
|
|
|
logger.warn(ase.getMessage()); |
|
|
|
} catch (AmazonClientException ace) { |
|
|
|
data.code = ResultData.ERROR; |
|
|
|
logger.warn("Caught an AmazonClientException, which " + |
|
|
|
"means the client encountered " + |
|
|
|
"an internal error while trying to " + |
|
|
|
"communicate with S3, " + |
|
|
|
"such as not being able to access the network."); |
|
|
|
logger.warn("Error Message: " + ace.getMessage()); |
|
|
|
} catch (IOException ioe) { |
|
|
|
data.code = ResultData.ERROR; |
|
|
|
logger.warn("Caught an IOException: " + ioe.getMessage()); |
|
|
|
} |
|
|
|
return data; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |