Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在Spring AWS示例上转换为org.springframework.core.io.WritableResource_Spring_Amazon Web Services_Amazon S3 - Fatal编程技术网

无法在Spring AWS示例上转换为org.springframework.core.io.WritableResource

无法在Spring AWS示例上转换为org.springframework.core.io.WritableResource,spring,amazon-web-services,amazon-s3,Spring,Amazon Web Services,Amazon S3,我正在阅读以下文档: 在Spring应用程序中使用AWS。我对S3特别感兴趣,因此,我设置了应用程序并复制了这段代码,以确保设置正常工作: Resource resource = this.resourceLoader.getResource("s3://myBucket/rootFile.log"); WritableResource writableResource = (WritableResource) resource; try (OutputStream outputStream

我正在阅读以下文档:

在Spring应用程序中使用AWS。我对S3特别感兴趣,因此,我设置了应用程序并复制了这段代码,以确保设置正常工作:

Resource resource = this.resourceLoader.getResource("s3://myBucket/rootFile.log");
WritableResource writableResource = (WritableResource) resource;
try (OutputStream outputStream = writableResource.getOutputStream()) {
  outputStream.write("test".getBytes());
}
但当我运行它时,我会得到以下错误:

java.lang.ClassCastException: org.springframework.web.context.support.ServletContextResource cannot be cast to org.springframework.core.io.WritableResource

你知道怎么了吗?这是安装问题吗?对我来说,它看起来不像,但我是新手。

尝试将资源类声明/转换为FileSystemResource:

FileSystemResource resource = this.resourceLoader.getResource("s3://myBucket/rootFile.log");
WritableResource writableResource = (WritableResource) resource;
try (OutputStream outputStream = writableResource.getOutputStream()) {
  outputStream.write("test".getBytes());
}

我有同样的问题,结果是我使用了错误的库。尝试从更改依赖项

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-aws</artifactId>
    </dependency>
希望这能解决您的问题。

我不得不删除

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

这将使用s3协议处理程序,并为您提供一种实现WritableResource的不同类型的资源。

我曾尝试遵循提出的解决方案,但没有成功。我解决它的唯一方法是声明一个新的
SimpleStorageProtocolResolver
,并将其作为
ProtocolResolver
添加到
默认资源加载程序中。使用Spring Boot 2.1.7 e Spring Cloud Aws 2.1.4

@Autowired
public void configureResourceLoader(AmazonS3 amazonS3, DefaultResourceLoader resourceLoader) {
    SimpleStorageProtocolResolver simpleStorageProtocolResolver = new SimpleStorageProtocolResolver(amazonS3);
    // As we are calling it by hand, we must initialize it properly.
    simpleStorageProtocolResolver.afterPropertiesSet();
    resourceLoader.addProtocolResolver(simpleStorageProtocolResolver);
}
链接如下:


对spring boot开发工具的注释也对我有用。谢谢。我试过这个办法,但没有成功。使用Spring Boot 2.1.7 e Spring Cloud Aws 2.1.4。我不得不用另一种方法来修复它。我会把它贴在这里。无论如何谢谢。谢谢,这对我的
SpringBoot2.3.1
SpringCloudStarter 2.2.2
有所帮助。但是,我仍然想知道如何使默认值起作用。
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>
public Resource getResource(String location) {
    if (this.resourceLoader != null) {
        return this.resourceLoader.getResource(location);
    }
    return super.getResource(location);
}
@Autowired
public void configureResourceLoader(AmazonS3 amazonS3, DefaultResourceLoader resourceLoader) {
    SimpleStorageProtocolResolver simpleStorageProtocolResolver = new SimpleStorageProtocolResolver(amazonS3);
    // As we are calling it by hand, we must initialize it properly.
    simpleStorageProtocolResolver.afterPropertiesSet();
    resourceLoader.addProtocolResolver(simpleStorageProtocolResolver);
}