我在 Spring Boot 应用程序中使用适用于 Java 的 AWS 开发工具包配置 S3AsyncClient 时遇到问题。尽管设置了所有必要的属性,我仍然收到以下错误消息:
Unable to load region from software.amazon.awssdk.regions.providers.SystemSettingsRegionProvider@ee630de:Unable to load region from system settings. Region must be specified either via environment variable (AWS_REGION) or system property (aws.region).
s.a.a.r.p.AwsRegionProviderChain : Unable to load region from software.amazon.awssdk.regions.providers.AwsProfileRegionProvider@22590efb:No region provided in profile: default
s.a.a.r.p.AwsRegionProviderChain: Unable to load region from software.amazon.awssdk.regions.providers.InstanceProfileRegionProvider@6c5be4c6:Unable to contact EC2 metadata service.
Minio 类的配置:
minio.access.url=http://localhost:9000
minio.access.name=minioadmin
minio.access.secret=minioadmin
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import java.net.URI;
import static software.amazon.awssdk.transfer.s3.SizeConstant.MB;
@Configuration
public class MinioConfig {
@Value("${minio.access.url}")
private String minioUrl;
@Value("${minio.access.name}")
private String accessKey;
@Value("${minio.access.secret}")
private String accessSecret;
@Bean
public S3AsyncClient generateMinioClient(){
return S3AsyncClient.crtBuilder()
.endpointOverride(URI.create(minioUrl))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, accessSecret)))
.region(Region.EU_CENTRAL_2)
.targetThroughputInGbps(20.0)
.minimumPartSizeInBytes(8 * MB)
.build();
}
}
和服务:
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.transfer.s3.S3TransferManager;
import software.amazon.awssdk.transfer.s3.model.CompletedFileUpload;
import software.amazon.awssdk.transfer.s3.model.UploadFileRequest;
import software.amazon.awssdk.transfer.s3.progress.LoggingTransferListener;
import java.net.URI;
import java.nio.file.Paths;
import java.util.concurrent.CompletableFuture;
@Service
@AllArgsConstructor
public class FileStorageService {
private final StorageSystem storageSystem;
public CompletableFuture<CompletedFileUpload> uploadFile(String username, String fileName, URI fileUri) {
try (S3TransferManager transferManager = S3TransferManager.create()) {
UploadFileRequest request = UploadFileRequest.builder()
.putObjectRequest(req -> req.bucket(username).key("key"))
.addTransferListener(LoggingTransferListener.create())
.source(Paths.get(fileName))
.build();
return transferManager.uploadFile(request).completionFuture();
}
}
}
控制器:
import com.wallhack.clouddrive.service.FileStorageService;
import com.wallhack.clouddrive.service.FolderStorageService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.transfer.s3.model.CompletedFileUpload;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@Controller
@AllArgsConstructor
public class FileUploadController {
private FileStorageService fileService;
@GetMapping("/upload")
public String showUploadForm() {
return "upload";
}
@PostMapping("/upload")
public String handleFileUpload(@RequestParam("username") String username,
@RequestParam("file") MultipartFile file,
Model model) {
try {
Path tempFile = Files.createTempFile("upload-", file.getOriginalFilename());
Files.copy(file.getInputStream(), tempFile, StandardCopyOption.REPLACE_EXISTING);
URI fileUri = tempFile.toUri();
CompletableFuture<CompletedFileUpload> uploadFuture = fileService.uploadFile(username, file.getOriginalFilename(), fileUri);
uploadFuture.join();
model.addAttribute("message", "File uploaded successfully: ");
Files.delete(tempFile);
} catch (Exception e) {
model.addAttribute("message", "File upload failed: " + e.getMessage());
}
return "home";
}
什么可能导致错误消息?
我已经检查了application.properties中的设置并确认变量设置正确。我应该在 application.properties 或环境变量中设置其他内容吗?
我已在 application.properties 中设置 aws.region=us-east-1,但问题仍然存在。 S3AsyncClient 构建器中 Region.of("us-east-1") 的用法是否正确?
错误出现在 S3AsyncClient 中,这是正确的版本:
@Bean
public S3AsyncClient generateMinioClient(){
return S3AsyncClient
.builder()
.forcePathStyle(true) // add this
.endpointOverride(URI.create(minioUrl))
.region(Region.AWS_GLOBAL)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, accessSecret)))
.build();
}