feat:阿里云oss接入

This commit is contained in:
sugus 2025-07-21 15:04:02 +08:00
parent 28a1ae7298
commit 071d9a97fd
10 changed files with 195 additions and 184 deletions

View File

@ -1,7 +1,7 @@
package com.aircraft.modules.aircraft.domain; package com.aircraft.modules.aircraft.domain;
import com.aircraft.base.BaseEntity; import com.aircraft.base.BaseEntity;
import com.aircraft.domain.dto.LocalAttachmentMaterialDTO; import com.aircraft.domain.dto.AttachmentMaterialDTO;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
@ -96,6 +96,6 @@ public class AircraftDevice extends BaseEntity {
@NotNull @NotNull
@TableField(exist = false) @TableField(exist = false)
@ApiModelProperty(value = "设备图片") @ApiModelProperty(value = "设备图片")
private List<LocalAttachmentMaterialDTO> deviceImages; private List<AttachmentMaterialDTO> deviceImages;
} }

View File

@ -1,7 +1,7 @@
package com.aircraft.modules.aircraft.domain; package com.aircraft.modules.aircraft.domain;
import com.aircraft.base.BaseEntity; import com.aircraft.base.BaseEntity;
import com.aircraft.domain.dto.LocalAttachmentMaterialDTO; import com.aircraft.domain.dto.AttachmentMaterialDTO;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
@ -71,7 +71,7 @@ public class AircraftInsurance extends BaseEntity {
@NotNull @NotNull
@TableField(exist = false) @TableField(exist = false)
@ApiModelProperty(value = "保险附件") @ApiModelProperty(value = "保险附件")
private List<LocalAttachmentMaterialDTO> insuranceAttachment; private List<AttachmentMaterialDTO> insuranceAttachment;
} }

View File

@ -1,6 +1,6 @@
package com.aircraft.modules.aircraft.service.impl; package com.aircraft.modules.aircraft.service.impl;
import com.aircraft.domain.dto.LocalAttachmentMaterialDTO; import com.aircraft.domain.dto.AttachmentMaterialDTO;
import com.aircraft.exception.BadRequestException; import com.aircraft.exception.BadRequestException;
import com.aircraft.modules.aircraft.domain.AircraftDevice; import com.aircraft.modules.aircraft.domain.AircraftDevice;
import com.aircraft.modules.aircraft.domain.dto.AircraftDevicePageDTO; import com.aircraft.modules.aircraft.domain.dto.AircraftDevicePageDTO;
@ -113,14 +113,14 @@ public class AircraftDeviceServiceImpl extends ServiceImpl<AircraftDeviceMapper,
public AircraftDevice findById(Long id) { public AircraftDevice findById(Long id) {
AircraftDevice aircraftDevice = getById(id); AircraftDevice aircraftDevice = getById(id);
if (null != aircraftDevice) { if (null != aircraftDevice) {
List<LocalAttachmentMaterialDTO> deviceImages = attachmentMaterialService List<AttachmentMaterialDTO> deviceImages = attachmentMaterialService
.lambdaQuery() .lambdaQuery()
.eq(AttachmentMaterial::getBusinessType, BUSINESS_TYPE) .eq(AttachmentMaterial::getBusinessType, BUSINESS_TYPE)
.eq(AttachmentMaterial::getBusinessId, id) .eq(AttachmentMaterial::getBusinessId, id)
.list() .list()
.stream() .stream()
.map(attachmentMaterial -> { .map(attachmentMaterial -> {
LocalAttachmentMaterialDTO localAttachmentMaterialDTO = new LocalAttachmentMaterialDTO(); AttachmentMaterialDTO localAttachmentMaterialDTO = new AttachmentMaterialDTO();
BeanUtils.copyProperties(attachmentMaterial, localAttachmentMaterialDTO); BeanUtils.copyProperties(attachmentMaterial, localAttachmentMaterialDTO);
return localAttachmentMaterialDTO; return localAttachmentMaterialDTO;
}).toList(); }).toList();

View File

@ -1,6 +1,6 @@
package com.aircraft.modules.aircraft.service.impl; package com.aircraft.modules.aircraft.service.impl;
import com.aircraft.domain.dto.LocalAttachmentMaterialDTO; import com.aircraft.domain.dto.AttachmentMaterialDTO;
import com.aircraft.modules.aircraft.domain.AircraftInsurance; import com.aircraft.modules.aircraft.domain.AircraftInsurance;
import com.aircraft.modules.aircraft.mapper.AircraftInsuranceMapper; import com.aircraft.modules.aircraft.mapper.AircraftInsuranceMapper;
import com.aircraft.modules.aircraft.service.AircraftInsuranceService; import com.aircraft.modules.aircraft.service.AircraftInsuranceService;
@ -102,14 +102,14 @@ public class AircraftInsuranceServiceImpl extends ServiceImpl<AircraftInsuranceM
public AircraftInsurance findById(Long id) { public AircraftInsurance findById(Long id) {
AircraftInsurance aircraftInsurance = getById(id); AircraftInsurance aircraftInsurance = getById(id);
if (null != aircraftInsurance) { if (null != aircraftInsurance) {
List<LocalAttachmentMaterialDTO> insuranceAttachment = attachmentMaterialService List<AttachmentMaterialDTO> insuranceAttachment = attachmentMaterialService
.lambdaQuery() .lambdaQuery()
.eq(AttachmentMaterial::getBusinessType, BUSINESS_TYPE) .eq(AttachmentMaterial::getBusinessType, BUSINESS_TYPE)
.eq(AttachmentMaterial::getBusinessId, id) .eq(AttachmentMaterial::getBusinessId, id)
.list() .list()
.stream() .stream()
.map(attachmentMaterial -> { .map(attachmentMaterial -> {
LocalAttachmentMaterialDTO localAttachmentMaterialDTO = new LocalAttachmentMaterialDTO(); AttachmentMaterialDTO localAttachmentMaterialDTO = new AttachmentMaterialDTO();
BeanUtils.copyProperties(attachmentMaterial, localAttachmentMaterialDTO); BeanUtils.copyProperties(attachmentMaterial, localAttachmentMaterialDTO);
return localAttachmentMaterialDTO; return localAttachmentMaterialDTO;
}).toList(); }).toList();

View File

@ -127,15 +127,16 @@ file:
amz: amz:
s3: s3:
# 地域 # 地域
region: test region: cn-wulanchabu
# 地域对应的 endpoint # 地域对应的 endpoint
endPoint: https://s3.test.com endPoint: https://oss-cn-wulanchabu.aliyuncs.com
# 访问的域名 # 访问的域名
domain: https://s3.test.com domain: https://aishangyun.oss-cn-wulanchabu.aliyuncs.com/
# 账号的认证信息,或者子账号的认证信息 # 账号的认证信息,或者子账号的认证信息
accessKey: 填写你的AccessKey accessKey: LTAI5tRK1mszQC82s2rCkowq
secretKey: 填写你的SecretKey secretKey: Ns8VAkRBauIuC207s3eGiqMrLbpF4e
# 存储桶Bucket # 存储桶Bucket
defaultBucket: 填写你的存储桶名称 defaultBucket: aishangyun
# 文件存储路径 # 文件存储路径
timeformat: yyyy-MM timeformat: yyyy-MM

View File

@ -6,8 +6,13 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.auth.signer.AwsS3V4Signer;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption;
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Configuration;
import java.net.URI; import java.net.URI;
/** /**
@ -72,6 +77,16 @@ public class AmzS3Config {
return S3Client.builder().region(Region.of(region)) return S3Client.builder().region(Region.of(region))
.endpointOverride(URI.create(endPoint)) .endpointOverride(URI.create(endPoint))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey))) .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey)))
.overrideConfiguration(
ClientOverrideConfiguration.builder()
.putAdvancedOption(SdkAdvancedClientOption.SIGNER, AwsS3V4Signer.create())
.build()
)
.serviceConfiguration(
S3Configuration.builder()
.chunkedEncodingEnabled(false) // 关闭chunked encoding
.build()
)
.build(); .build();
} }
} }

View File

@ -5,10 +5,10 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
/** /**
* 本地附件材料DTO * 附件材料DTO
*/ */
@Data @Data
public class LocalAttachmentMaterialDTO { public class AttachmentMaterialDTO {
@ApiModelProperty(value = "id") @ApiModelProperty(value = "id")

View File

@ -1,112 +1,112 @@
/* ///*
* Copyright 2019-2025 Zheng Jie // * Copyright 2019-2025 Zheng Jie
* // *
* Licensed under the Apache License, Version 2.0 (the "License"); // * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. // * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at // * You may obtain a copy of the License at
* // *
* http://www.apache.org/licenses/LICENSE-2.0 // * http://www.apache.org/licenses/LICENSE-2.0
* // *
* Unless required by applicable law or agreed to in writing, software // * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, // * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and // * See the License for the specific language governing permissions and
* limitations under the License. // * limitations under the License.
*/ // */
package com.aircraft.rest; //package com.aircraft.rest;
//
import com.aircraft.domain.dto.LocalAttachmentMaterialDTO; //import com.aircraft.domain.dto.LocalAttachmentMaterialDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; //import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor; //import lombok.RequiredArgsConstructor;
import com.aircraft.annotation.Log; //import com.aircraft.annotation.Log;
import com.aircraft.domain.LocalStorage; //import com.aircraft.domain.LocalStorage;
import com.aircraft.exception.BadRequestException; //import com.aircraft.exception.BadRequestException;
import com.aircraft.service.LocalStorageService; //import com.aircraft.service.LocalStorageService;
import com.aircraft.domain.dto.LocalStorageQueryCriteria; //import com.aircraft.domain.dto.LocalStorageQueryCriteria;
import com.aircraft.utils.FileUtil; //import com.aircraft.utils.FileUtil;
import com.aircraft.utils.PageResult; //import com.aircraft.utils.PageResult;
import org.springframework.http.HttpStatus; //import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; //import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; //import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated; //import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; //import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*; //import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile; //import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse; //import javax.servlet.http.HttpServletResponse;
import java.io.IOException; //import java.io.IOException;
//
/** ///**
* @author Zheng Jie //* @author Zheng Jie
* @date 2019-09-05 //* @date 2019-09-05
*/ //*/
@RestController //@RestController
@RequiredArgsConstructor //@RequiredArgsConstructor
@Api(tags = "工具:本地存储管理") //@Api(tags = "工具:本地存储管理")
@RequestMapping("/api/localStorage") //@RequestMapping("/api/localStorage")
public class LocalStorageController { //public class LocalStorageController {
//
private final LocalStorageService localStorageService; // private final LocalStorageService localStorageService;
//
@GetMapping // @GetMapping
@ApiOperation("查询文件") // @ApiOperation("查询文件")
@PreAuthorize("@el.check('storage:list')") // @PreAuthorize("@el.check('storage:list')")
public ResponseEntity<PageResult<LocalStorage>> queryFile(LocalStorageQueryCriteria criteria){ // public ResponseEntity<PageResult<LocalStorage>> queryFile(LocalStorageQueryCriteria criteria){
Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize()); // Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
return new ResponseEntity<>(localStorageService.queryAll(criteria,page),HttpStatus.OK); // return new ResponseEntity<>(localStorageService.queryAll(criteria,page),HttpStatus.OK);
}
@ApiOperation("导出数据")
@GetMapping(value = "/download")
@PreAuthorize("@el.check('storage:list')")
public void exportFile(HttpServletResponse response, LocalStorageQueryCriteria criteria) throws IOException {
localStorageService.download(localStorageService.queryAll(criteria), response);
}
@PostMapping
@ApiOperation("上传文件")
public ResponseEntity<Object> createFile(@RequestPart("file") MultipartFile file){
LocalStorage localStorage = localStorageService.create(file.getOriginalFilename(), file);
LocalAttachmentMaterialDTO localAttachmentMaterialDTO = new LocalAttachmentMaterialDTO();
localAttachmentMaterialDTO.setFileType(localStorage.getSuffix());
localAttachmentMaterialDTO.setFileSize(file.getSize());
localAttachmentMaterialDTO.setNewFileName(localStorage.getRealName());
localAttachmentMaterialDTO.setSourceFileName(localStorage.getName());
localAttachmentMaterialDTO.setFileFullPath("/file/" + localStorage.getType() + "/" + localStorage.getRealName());
return new ResponseEntity<>(localAttachmentMaterialDTO, HttpStatus.OK);
}
@ApiOperation("上传图片")
@PostMapping("/pictures")
public ResponseEntity<LocalAttachmentMaterialDTO> uploadPicture(@RequestPart("file") MultipartFile file){
// 判断文件是否为图片
String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
if(!FileUtil.IMAGE.equals(FileUtil.getFileType(suffix))){
throw new BadRequestException("只能上传图片");
}
LocalStorage localStorage = localStorageService.create(file.getOriginalFilename(), file);
LocalAttachmentMaterialDTO localAttachmentMaterialDTO = new LocalAttachmentMaterialDTO();
localAttachmentMaterialDTO.setFileType(localStorage.getSuffix());
localAttachmentMaterialDTO.setFileSize(file.getSize());
localAttachmentMaterialDTO.setNewFileName(localStorage.getRealName());
localAttachmentMaterialDTO.setSourceFileName(localStorage.getName());
localAttachmentMaterialDTO.setFileFullPath("/file/" + localStorage.getType() + "/" + localStorage.getRealName());
return new ResponseEntity<>(localAttachmentMaterialDTO, HttpStatus.OK);
}
// @PutMapping
// @Log("修改文件")
// @ApiOperation("修改文件")
// public ResponseEntity<Object> updateFile(@Validated @RequestBody LocalStorage resources){
// localStorageService.update(resources);
// return new ResponseEntity<>(HttpStatus.NO_CONTENT);
// } // }
// //
// @Log("删除文件") // @ApiOperation("导出数据")
// @DeleteMapping // @GetMapping(value = "/download")
// @ApiOperation("多选删除") // @PreAuthorize("@el.check('storage:list')")
// @PreAuthorize("@el.check('storage:del')") // public void exportFile(HttpServletResponse response, LocalStorageQueryCriteria criteria) throws IOException {
// public ResponseEntity<Object> deleteFile(@RequestBody Long[] ids) { // localStorageService.download(localStorageService.queryAll(criteria), response);
// localStorageService.deleteAll(ids);
// return new ResponseEntity<>(HttpStatus.OK);
// } // }
} //
// @PostMapping
// @ApiOperation("上传文件")
// public ResponseEntity<Object> createFile(@RequestPart("file") MultipartFile file){
// LocalStorage localStorage = localStorageService.create(file.getOriginalFilename(), file);
// LocalAttachmentMaterialDTO localAttachmentMaterialDTO = new LocalAttachmentMaterialDTO();
// localAttachmentMaterialDTO.setFileType(localStorage.getSuffix());
// localAttachmentMaterialDTO.setFileSize(file.getSize());
// localAttachmentMaterialDTO.setNewFileName(localStorage.getRealName());
// localAttachmentMaterialDTO.setSourceFileName(localStorage.getName());
// localAttachmentMaterialDTO.setFileFullPath("/file/" + localStorage.getType() + "/" + localStorage.getRealName());
// return new ResponseEntity<>(localAttachmentMaterialDTO, HttpStatus.OK);
// }
//
// @ApiOperation("上传图片")
// @PostMapping("/pictures")
// public ResponseEntity<LocalAttachmentMaterialDTO> uploadPicture(@RequestPart("file") MultipartFile file){
// // 判断文件是否为图片
// String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
// if(!FileUtil.IMAGE.equals(FileUtil.getFileType(suffix))){
// throw new BadRequestException("只能上传图片");
// }
// LocalStorage localStorage = localStorageService.create(file.getOriginalFilename(), file);
// LocalAttachmentMaterialDTO localAttachmentMaterialDTO = new LocalAttachmentMaterialDTO();
// localAttachmentMaterialDTO.setFileType(localStorage.getSuffix());
// localAttachmentMaterialDTO.setFileSize(file.getSize());
// localAttachmentMaterialDTO.setNewFileName(localStorage.getRealName());
// localAttachmentMaterialDTO.setSourceFileName(localStorage.getName());
// localAttachmentMaterialDTO.setFileFullPath("/file/" + localStorage.getType() + "/" + localStorage.getRealName());
// return new ResponseEntity<>(localAttachmentMaterialDTO, HttpStatus.OK);
// }
//
//// @PutMapping
//// @Log("修改文件")
//// @ApiOperation("修改文件")
//// public ResponseEntity<Object> updateFile(@Validated @RequestBody LocalStorage resources){
//// localStorageService.update(resources);
//// return new ResponseEntity<>(HttpStatus.NO_CONTENT);
//// }
////
//// @Log("删除文件")
//// @DeleteMapping
//// @ApiOperation("多选删除")
//// @PreAuthorize("@el.check('storage:del')")
//// public ResponseEntity<Object> deleteFile(@RequestBody Long[] ids) {
//// localStorageService.deleteAll(ids);
//// return new ResponseEntity<>(HttpStatus.OK);
//// }
//}

View File

@ -15,27 +15,18 @@
*/ */
package com.aircraft.rest; package com.aircraft.rest;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.aircraft.domain.dto.AttachmentMaterialDTO;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import com.aircraft.annotation.Log;
import com.aircraft.config.AmzS3Config; import com.aircraft.config.AmzS3Config;
import com.aircraft.domain.S3Storage; import com.aircraft.domain.S3Storage;
import com.aircraft.domain.dto.S3StorageQueryCriteria;
import com.aircraft.service.S3StorageService; import com.aircraft.service.S3StorageService;
import com.aircraft.utils.PageResult;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* amz S3 协议云存储管理 * amz S3 协议云存储管理
@ -51,55 +42,57 @@ public class S3StorageController {
private final AmzS3Config amzS3Config; private final AmzS3Config amzS3Config;
private final S3StorageService s3StorageService; private final S3StorageService s3StorageService;
//
@ApiOperation("导出数据") // @ApiOperation("导出数据")
@GetMapping(value = "/download") // @GetMapping(value = "/download")
@PreAuthorize("@el.check('storage:list')") // @PreAuthorize("@el.check('storage:list')")
public void exportS3Storage(HttpServletResponse response, S3StorageQueryCriteria criteria) throws IOException { // public void exportS3Storage(HttpServletResponse response, S3StorageQueryCriteria criteria) throws IOException {
s3StorageService.download(s3StorageService.queryAll(criteria), response); // s3StorageService.download(s3StorageService.queryAll(criteria), response);
} // }
//
@GetMapping // @GetMapping
@ApiOperation("查询文件") // @ApiOperation("查询文件")
@PreAuthorize("@el.check('storage:list')") // @PreAuthorize("@el.check('storage:list')")
public ResponseEntity<PageResult<S3Storage>> queryS3Storage(S3StorageQueryCriteria criteria){ // public ResponseEntity<PageResult<S3Storage>> queryS3Storage(S3StorageQueryCriteria criteria){
Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize()); // Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
return new ResponseEntity<>(s3StorageService.queryAll(criteria, page),HttpStatus.OK); // return new ResponseEntity<>(s3StorageService.queryAll(criteria, page),HttpStatus.OK);
} // }
@PostMapping @PostMapping
@ApiOperation("上传文件") @ApiOperation("上传文件")
public ResponseEntity<Object> uploadS3Storage(@RequestPart("file") MultipartFile file){ public ResponseEntity<AttachmentMaterialDTO> uploadS3Storage(@RequestPart("file") MultipartFile file){
S3Storage storage = s3StorageService.upload(file); S3Storage storage = s3StorageService.upload(file);
Map<String,Object> map = new HashMap<>(3); AttachmentMaterialDTO attachmentMaterialDTO = new AttachmentMaterialDTO();
map.put("id",storage.getId()); attachmentMaterialDTO.setFileType(storage.getFileType());
map.put("errno",0); attachmentMaterialDTO.setFileSize(file.getSize());
map.put("data",new String[]{amzS3Config.getDomain() + "/" + storage.getFilePath()}); attachmentMaterialDTO.setNewFileName(storage.getFileRealName());
return new ResponseEntity<>(map,HttpStatus.OK); attachmentMaterialDTO.setSourceFileName(storage.getFileName());
attachmentMaterialDTO.setFileFullPath(storage.getFileFullPath());
return new ResponseEntity<>(attachmentMaterialDTO, HttpStatus.OK);
} }
@Log("下载文件") // @Log("下载文件")
@ApiOperation("下载文件") // @ApiOperation("下载文件")
@GetMapping(value = "/download/{id}") // @GetMapping(value = "/download/{id}")
public ResponseEntity<Object> downloadS3Storage(@PathVariable Long id){ // public ResponseEntity<Object> downloadS3Storage(@PathVariable Long id){
Map<String,Object> map = new HashMap<>(1); // Map<String,Object> map = new HashMap<>(1);
S3Storage storage = s3StorageService.getById(id); // S3Storage storage = s3StorageService.getById(id);
if (storage == null) { // if (storage == null) {
map.put("message", "文件不存在或已被删除"); // map.put("message", "文件不存在或已被删除");
return new ResponseEntity<>(map, HttpStatus.NOT_FOUND); // return new ResponseEntity<>(map, HttpStatus.NOT_FOUND);
} // }
// 仅适合公开文件访问私有文件可以使用服务中的 privateDownload 方法 // // 仅适合公开文件访问私有文件可以使用服务中的 privateDownload 方法
String url = amzS3Config.getDomain() + "/" + storage.getFilePath(); // String url = amzS3Config.getDomain() + "/" + storage.getFilePath();
map.put("url", url); // map.put("url", url);
return new ResponseEntity<>(map,HttpStatus.OK); // return new ResponseEntity<>(map,HttpStatus.OK);
} // }
//
@Log("删除多个文件") // @Log("删除多个文件")
@DeleteMapping // @DeleteMapping
@ApiOperation("删除多个文件") // @ApiOperation("删除多个文件")
@PreAuthorize("@el.check('storage:del')") // @PreAuthorize("@el.check('storage:del')")
public ResponseEntity<Object> deleteAllS3Storage(@RequestBody List<Long> ids) { // public ResponseEntity<Object> deleteAllS3Storage(@RequestBody List<Long> ids) {
s3StorageService.deleteAll(ids); // s3StorageService.deleteAll(ids);
return new ResponseEntity<>(HttpStatus.OK); // return new ResponseEntity<>(HttpStatus.OK);
} // }
} }

View File

@ -33,6 +33,8 @@ import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.aircraft.utils.PageUtil; import com.aircraft.utils.PageUtil;
import java.io.InputStream;
import java.util.*; import java.util.*;
import java.io.IOException; import java.io.IOException;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -129,9 +131,9 @@ public class S3StorageServiceImpl extends ServiceImpl<S3StorageMapper, S3Storage
.build(); .build();
// 创建 S3Storage 实例 // 创建 S3Storage 实例
S3Storage s3Storage = new S3Storage(); S3Storage s3Storage = new S3Storage();
try { try (InputStream inputStream = file.getInputStream()){
// 上传文件到 S3 // 上传文件到 S3
s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(file.getInputStream(), file.getSize())); s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(inputStream, file.getSize()));
// 设置 S3Storage 属性 // 设置 S3Storage 属性
s3Storage.setFileMimeType(FileUtil.getMimeType(originalName)); s3Storage.setFileMimeType(FileUtil.getMimeType(originalName));
s3Storage.setFileName(originalName); s3Storage.setFileName(originalName);