2

我打算做什么:我想ContentStore在同一个系统中使用多个 s:一个用于新上传的文件(文件系统),一个用于(长期)归档(AWS S3 或 GCS)。

我尝试过的(以及实际有效的):

  1. File通过另一个属性扩展类private String contentStoreName;

  2. 像这里描述的那样创建两个ContentStoreSpring-Content:将文件从内容存储移动到另一个内容存储

  3. gettingstarted.FileContentController.setContent(Long, MultipartFile)通过为 used 设置标识符来扩展ContentStoref.get().setContentStoreName("regular");

  4. 根据存储的内容获取内容contentStoreName

    InputStream input;
    if (Objects.equals(f.get().getContentStoreName(), "archive")) {
        input = archiveContentStore.getContent(f.get());
    } else {
        input = regularContentStore.getContent(f.get());
    }
    
  5. contentStoreName从一个移动到另一个时更改ContentStore

    Resource resource = regularContentStore.getResource(fileEntity.get());
    archiveContentStore.setContent(fileEntity.get(), resource);
    fileEntity.get().setContentStoreName("archive");
    filesRepo.save(fileEntity.get());
    

关于这个的气味:尽管这段代码有效,但我想这不是预期的方式,因为Spring-content通常在后台做很多注释和一些魔法。但我找不到ContentStore.

问题:有没有更有意的方式来做到这一点Spring-Content

4

1 回答 1

1

除了在单个应用程序中支持多个存储模块(通过 FilesystemContentStore 等注释)之外,Spring Content 目前不提供任何支持存储类的逻辑。这将是您开始时需要在 Spring Content 之上创建的层。

就 Spring 内容注解而言,了解哪些模块管理哪些注解可能会有所帮助。

Spring 内容存储模块;FS、S3 等都实现ContentStore并在这样做时都提供了@ContentId@ContentLength属性的管理(除了管理实际的内容操作)。看起来您正在使用ContentStoreAPI ( getContent/ setContent),因此将为您管理实体的内容 id 和长度属性。

Spring Content REST 然后提供管理@MimeType@OriginalFileName属性(除了提供全面的 REST 端点)。我的猜测是您没有使用它,而是提供了您自己的自定义控制器 REST API,该 API 使用“contentStoreName”属性来决定从哪个商店放置/获取内容。这种方法似乎很好。

这就是说,一个稍微更优雅的方法,也许,可以让你使用 Spring Content REST可能是实现你自己的自定义Archiving存储模块,并将上面的“切换”逻辑封装在它的setContent//方法中getContentunsetContent请注意,这实际上很容易(只有 4 或 5 个类,我会向您指出 GCP 和 Azure 模块以获得灵感)。请注意,REST API(用于 ContentStore)也只调用这 3 个 API,因此您只需要实现这些 API。这意味着您可以使用 Spring Content REST 及其提供的所有功能;一组休息端点,字节范围支持等等,以及很好地封装你的“归档”逻辑。

于 2022-02-09T05:12:03.573 回答