1.maven依赖
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3</artifactId> <version>1.11.543</version> </dependency>
2.application.yml
#亚马逊oos环境配置 amazon: aws: access-key-id: xxxxx access-key-secret: xxxxxx s3: default-bucket: xxxxx region: xxxx dirname: xxxx
3.控制层
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.util.List; @RestController @RequestMapping("/storage/") public class RestS3Controller { @Autowired private AmazonService amazonService; @PostMapping("/uploadFile") public String uploadFile(@RequestPart(value = "file") MultipartFile file) { return this.amazonService.uploadFile(file); } @DeleteMapping("/deleteFile") public String deleteFile(@RequestPart(value = "url") String fileUrl) { return this.amazonService.deleteFileFromS3Bucket(fileUrl); } @GetMapping("/getFileList") public List<String> getFileList() { return this.amazonService.listFiles(); } }
4.业务层
import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.*; import com.epbox.lite.cloud.util.DateUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.PostConstruct; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; @Service public class AmazonService { private AmazonS3 s3client; @Value("${amazon.s3.region}") private String region; @Value("${amazon.s3.default-bucket}") private String bucketName; @Value("${amazon.s3.dirname}") private String dirname; @Value("${amazon.aws.access-key-id}") private String accessKey; @Value("${amazon.aws.access-key-secret}") private String secretKey;; @PostConstruct private void initializeAmazon() { AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey); this.s3client = AmazonS3ClientBuilder.standard() .withRegion(Regions.fromName(region)) .withCredentials(new AWSStaticCredentialsProvider(credentials)) .build(); } public String uploadFile(MultipartFile multipartFile) { String fileUrl = ""; try { File file = convertMultiPartToFile(multipartFile); String fileName = generateFileName(multipartFile); uploadFileTos3bucket(fileName, file); file.delete(); } catch (Exception e) { e.printStackTrace(); } return fileUrl; } private File convertMultiPartToFile(MultipartFile file) throws IOException { File convFile = new File(file.getOriginalFilename()); FileOutputStream fos = new FileOutputStream(convFile); fos.write(file.getBytes()); fos.close(); return convFile; } private String generateFileName(MultipartFile multiPart) { return new Date().getTime() + "-" + multiPart.getOriginalFilename().replace(" ", "_"); } private void uploadFileTos3bucket(String fileName, File file) { s3client.putObject(new PutObjectRequest(bucketName+ "/" +dirname, fileName, file) .withCannedAcl(CannedAccessControlList.PublicRead)); } public String deleteFileFromS3Bucket(String fileUrl) { String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1); s3client.deleteObject(new DeleteObjectRequest(bucketName, fileName)); return "Successfully deleted"; } public List<String> listFiles() { ListObjectsRequest listObjectsRequest = new ListObjectsRequest() .withBucketName(bucketName) .withPrefix("/"); List<String> keys = new ArrayList<>(); ObjectListing objects = s3client.listObjects(listObjectsRequest); while (true) { List<S3ObjectSummary> summaries = objects.getObjectSummaries(); if (summaries.size() < 1) { break; } for (S3ObjectSummary item : summaries) { if (!item.getKey().endsWith("/")) keys.add(item.getKey()); } objects = s3client.listNextBatchOfObjects(objects); } return keys; } }