Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

文件上传增加S3协议的OSS支持#328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
liujiang157 wants to merge9 commits intohs-web:master
base:master
Choose a base branch
Loading
fromliujiang157:feature/oss-support
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
修改代码规范问题
  • Loading branch information
@liujiang157
liujiang157 committedJun 17, 2025
commitec8edfaf8dc342b403dbe7ee56320c7ce98fefc5
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,17 +35,9 @@ public S3Client s3Client(S3FileProperties properties) {
}

@Bean
@ConditionalOnMissingBean(FileStorageService.class)
public FileStorageService s3FileStorageService(FileUploadProperties uploadProperties,
public FileStorageService s3FileStorageService(
S3FileProperties s3Properties,
S3Client s3Client) {
return new S3FileStorageService(uploadProperties, s3Properties, s3Client);
}

@Bean
@ConditionalOnMissingBean(name = "reactiveFileController")
public ReactiveFileController reactiveFileController(FileUploadProperties properties,
FileStorageService storageService) {
return new ReactiveFileController(properties, storageService);
return new S3FileStorageService(s3Properties, s3Client);
}
}
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,6 @@

import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.hswebframework.web.file.FileUploadProperties;
import org.hswebframework.web.file.S3FileProperties;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.codec.multipart.FilePart;
Expand All@@ -19,9 +18,8 @@

@AllArgsConstructor
public class S3FileStorageService implements FileStorageService {

private final FileUploadProperties fileProperties;
private final S3FileProperties s3Properties;

private final S3FileProperties properties;
private final S3Client s3Client;


Expand All@@ -38,7 +36,7 @@ public Mono<String> saveFile(FilePart filePart) {
})
.map(inputStream -> {
PutObjectRequest request = PutObjectRequest.builder()
.bucket(s3Properties.getBucket())
.bucket(properties.getBucket())
.key(filename)
.build();

Expand All@@ -55,7 +53,7 @@ public Mono<String> saveFile(InputStream inputStream, String fileType) {
String key = UUID.randomUUID().toString() + (fileType.startsWith(".") ? fileType : "." + fileType);

PutObjectRequest request = PutObjectRequest.builder()
.bucket(s3Properties.getBucket())
.bucket(properties.getBucket())
.key(key)
.build();

Expand All@@ -74,9 +72,9 @@ private String buildFileName(String originalName) {
}

private String buildFileUrl(String key) {
if (s3Properties.getBaseUrl() != null && !s3Properties.getBaseUrl().isEmpty()) {
returns3Properties.getBaseUrl() + "/" + key;
if (properties.getBaseUrl() != null && !properties.getBaseUrl().isEmpty()) {
returnproperties.getBaseUrl() + "/" + key;
}
return "https://" +s3Properties.getBucket() + "." +s3Properties.getEndpoint().replace("https://", "").replace("http://", "") + "/" + key;
return "https://" +properties.getBucket() + "." +properties.getEndpoint().replace("https://", "").replace("http://", "") + "/" + key;
}
}
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

@RestController
@Resource(id = "file", name = "文件上传")
Expand DownExpand Up@@ -61,27 +62,7 @@ public Mono<String> uploadStatic(@RequestPart("file")

}

@PostMapping("/oss/static")
@ResourceAction(id = "upload-static", name = "静态文件")
@Operation(summary = "上传静态文件")
public Mono<String> uploadOssStatic(@RequestPart("file")
@Parameter(name = "file", description = "文件", style = ParameterStyle.FORM) Mono<Part> partMono) {
return partMono
.flatMap(part -> {
if (part instanceof FilePart) {
FilePart filePart = ((FilePart) part);
if (properties.denied(filePart.filename(), filePart.headers().getContentType())) {
return Mono.error( new AccessDenyException());
}
return fileStorageService.saveFile(filePart);
} else {
return Mono.error(() -> new IllegalArgumentException("[file] part is not a file"));
}
});

}

@PostMapping(value = "/oss/stream", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@PostMapping(value = "/static/stream", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@Operation(summary = "上传文件流")
public Mono<String> uploadOssStream(ServerHttpRequest request,
@RequestParam("fileType") String fileType) {
Expand All@@ -91,13 +72,10 @@ public Mono<String> uploadOssStream(ServerHttpRequest request,
}

return DataBufferUtils.join(request.getBody())
.map(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
DataBufferUtils.release(dataBuffer);
return new ByteArrayInputStream(bytes);
})
.flatMap(inputStream -> fileStorageService.saveFile(inputStream, fileType));
.flatMap(dataBuffer -> {
InputStream inputStream = dataBuffer.asInputStream(true);
return fileStorageService.saveFile(inputStream, fileType);
});
}

}
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,7 +34,7 @@ public class OssUploadTest {
@Test
public void testStatic(){
client.post()
.uri("/file/oss/static")
.uri("/file/static")
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData("file",new HttpEntity<>(new ClassPathResource("test.json"))))
.exchange()
Expand All@@ -49,7 +49,7 @@ public void testStream() throws Exception {

client.post()
.uri(uriBuilder ->
uriBuilder.path("/file/oss/stream")
uriBuilder.path("/file/static/stream")
.queryParam("fileType", "json")
.build())
.contentType(MediaType.APPLICATION_OCTET_STREAM)
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp