1

我想要什么:我想使用版本 1.2.7将内容从一个ContentStore(常规)移动到另一个ContentStore(例如存档) 。Spring-Content

我所做的是这个(它至少对DefaultFilesystemStoreImpls 有效):

  1. 像这样创建两个ContentStores:
@Bean(name = "mytmpfsstore1")
public ContentStore<File, String> getFileContentStore1() {
    FileSystemResourceLoader loader = new FileSystemResourceLoader(".\\tmpstore1");
    PlacementService placementService = new PlacementServiceImpl();
    return new DefaultFilesystemStoreImpl<File, String>(loader, placementService, new FileServiceImpl());
}
  1. 像这样将内容从一个移动ContentStore到另一个:
Optional<File> fileEntity = filesRepo.findById(id);
if (fileEntity.isPresent()) {
    Resource resource = regularContentStore.getResource(fileEntity.get());
    archiveContentStore.setContent(fileEntity.get(), resource);
    filesRepo.save(fileEntity.get());
    if (resource instanceof DeletableResource) {
        ((DeletableResource) resource).delete();
    }
}

问题:这是移动(/归档)内容的预期方式,Spring-Content还是有更优雅/更方便/更预期的移动/归档文件的方式(尤其是从文件系统到 S3 并再次返回)?

4

1 回答 1

2

Spring Content 在这里没有提供任何魔法。我尝试将 API 保持在相当低的级别,这个用例虽然有效,但有点过于细化。因此,最终您必须自己构建“存档”,并在一天结束时将内容从一个商店复制到另一个商店。

一些评论/指针:

  1. 不知道你为什么要实例化mytmpfsstore1自己。如果你有文件存储和 s3 存储,你可以让你的存储接口分别扩展 FileSystemContentStore 和 S3ContentStore,让框架为你实例化它们;IE

    public interface MyTmpFsStore extends FileSystemContentStore {}

    public interface MyS3store extends S3ContentStore()

    然后,您可以将这两个 bean 连接到您的“存档”代码中。假设这是一个控制器,例如:

    @RequestMapping(...) public void archive(MyTmpFsStore fsStore, S3ContentStore s3Store { ... }

    您在哪里进行复制操作。

  2. ContentStore扩展AssociativeStore,并且有一些不同的 API 用于设置/取消设置内容。基于资源或 InputStream。他们最终都实现了相同的目标。您可以轻松地使用getContent代替getResourceunsetContent代替delete.

  3. “归档”代码可能需要在 @Transactional 中,因此归档操作是原子的。

于 2022-02-09T04:41:33.397 回答