spring-cloud-aws が のSimpleStorageResourceLoaderようなパス パターンで S3 からリソースをロードできるようにすることを理解していますs3://<bucket>/<key>。私が抱えている問題はResourceLoader、mvc コンポーネント内の静的リソースを解決するときに確実に使用されるようにする方法です。リソース マッピングの構成は次のとおりです。
@Configuration
public class TestWebConfigurer extends WebMvcConfigurerAdapter {
private String s3Location;
// ${test.s3.location} comes in as "s3://<bucket>/"
public TestWebConfigurer(@Value("${test.s3.location}") String s3Location) {
this.s3Location = s3Location;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/mytest/**").addResourceLocations(s3Location);
}
}
内のコードをステップ実行するaddResourceLocations()と、 への呼び出しがありresourceLoader.getResource(location)ます。残念ながら、それは単なる でDefaultResourceLocatorあり、Resourceが返されると、ServletContextResourcewith のパスになり/s3://<bucket>/ます。
次のように、application.yml ファイル内で aws 資格情報を構成しました。
cloud:
aws:
credentials:
accessKey: <myaccess>
secretKey: <mysecret>
region:
static: us-east-1
私の理解では、Spring Boot のコンテキスト内で spring-cloud-starter-aws がそれを行うということでした。
ApplicationRunnerAWS 接続が正しいことをテストするために、同じプロジェクト内に を作成できました。テストは次のとおりです。
@Component
public class TestSimpleStorageLoader implements ApplicationRunner {
private ResourceLoader loader;
public TestSimpleStorageLoader(ResourceLoader loader) {
this.loader = loader;
}
@Override
public void run(ApplicationArguments args) throws Exception {
Resource resource = this.loader.getResource("s3://<bucket>/test.txt");
IOUtils.copy(resource.getInputStream(), System.out);
}
}
このテストでは、test.txtアプリケーションの起動時にファイルの内容を出力できました。
これをリソースハンドラーで機能させるには何が欠けていますか?