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
提交UriComponentsBuilder构建url
采用buffer.asInputStream访问文件移除冗余代码
  • Loading branch information
@liujiang157
liujiang157 committedJun 19, 2025
commit947c6954d4f285b2f781f6a48f0a9bccbc651b90
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,8 +30,6 @@ public class FileUploadProperties {

private String staticLocation = "/static";

private S3 s3;

//是否使用原始文件名进行存储
private boolean useOriginalFileName = false;

Expand DownExpand Up@@ -129,14 +127,4 @@ public static class StaticFileInfo {
private String relativeLocation;
private String location;
}

@Data
public static class S3 {
private String endpoint;
private String accessKey;
private String secretKey;
private String region;
private String bucket;
private String baseUrl;
}
}
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,13 +5,15 @@
import org.hswebframework.web.file.S3FileProperties;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.UUID;
Expand All@@ -26,23 +28,22 @@ public class S3FileStorageService implements FileStorageService {
@Override
public Mono<String> saveFile(FilePart filePart) {
String filename = buildFileName(filePart.filename());

return DataBufferUtils.join(filePart.content())
.publishOn(Schedulers.boundedElastic())
.map(dataBuffer -> {
byte[] bytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(bytes);
DataBufferUtils.release(dataBuffer);
return new ByteArrayInputStream(bytes);
})
.map(inputStream -> {
PutObjectRequest request = PutObjectRequest.builder()
.bucket(properties.getBucket())
.key(filename)
.build();
.flatMap(dataBuffer -> {
try (InputStream inputStream = dataBuffer.asInputStream(true)) {
PutObjectRequest request = PutObjectRequest.builder()
.bucket(properties.getBucket())
.key(filename)
.build();

s3Client.putObject(request, RequestBody.fromInputStream(inputStream, inputStream.available()));
return buildFileUrl(filename);
});
s3Client.putObject(request, RequestBody.fromInputStream(inputStream, dataBuffer.readableByteCount()));
return Mono.just(buildFileUrl(filename));
} catch (IOException e) {
return Mono.error(e);
}
})
.subscribeOn(Schedulers.boundedElastic());
}


Expand DownExpand Up@@ -73,8 +74,19 @@ private String buildFileName(String originalName) {

private String buildFileUrl(String key) {
if (properties.getBaseUrl() != null && !properties.getBaseUrl().isEmpty()) {
return properties.getBaseUrl() + "/" + key;
return UriComponentsBuilder
.fromUriString(properties.getBaseUrl())
.pathSegment(key)
.build()
.toUriString();
}
return "https://" + properties.getBucket() + "." + properties.getEndpoint().replace("https://", "").replace("http://", "") + "/" + key;
String host = properties.getBucket() + "." + properties.getEndpoint().replaceFirst("^https?://", "");
return UriComponentsBuilder
.newInstance()
.scheme("https")
.host(host)
.pathSegment(key)
.build()
.toUriString();
}
}

[8]ページ先頭

©2009-2025 Movatter.jp