Spring-内容:将文件从内容存储移动到另一个内容存储
Spring-Content: Moving files from content store to another content store
我想要什么:我想将内容从一个 ContentStore
(常规)移动到另一个 ContentStore
(例如存档)Spring-Content
版本1.2.7.
我做的是这个(它至少对 DefaultFilesystemStoreImpl
s 有效):
- 像这样创建两个
ContentStore
:
@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());
}
- 将内容从一个
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
移动(/存档)内容的预期方式还是有更优雅/更方便/更预期的方式moving/archiving 文件(特别是从文件系统到 S3 并再次返回)?
Spring 内容在这里没有任何魔力。我尽量保持 API 相当 low-level 并且这个用例虽然有效,但有点太细化了。
因此,最终您必须自己构建“存档”,并在一天结束时将内容从一个商店复制到另一个商店。
几个 comments/pointers:
不确定为什么要自己实例化 mytmpfsstore1
。如果你有文件存储和 s3 存储,你可以让你的存储接口分别扩展 FileSystemContentStore 和 S3ContentStore,让框架为你实例化它们;即
public interface MyTmpFsStore extends FileSystemContentStore {}
public interface MyS3store extends S3ContentStore()
然后您可以将这两个 bean 连接到您的 'archive' 代码中。假设这是一个控制器,类似于:
@RequestMapping(...) public void archive(MyTmpFsStore fsStore, S3ContentStore s3Store { ... }
您在哪里进行复制操作。
ContentStore
扩展 AssociativeStore
并且 setting/unsetting 内容有一些不同的 API。 Resource-based 或 InputStream。他们在一天结束时都达到了相同的 objective。你可以很容易地使用 getContent
而不是 getResource
和 unsetContent
而不是 delete
.
'archive' 代码可能需要在 @Transactional 中,因此存档操作是原子的。
我想要什么:我想将内容从一个 ContentStore
(常规)移动到另一个 ContentStore
(例如存档)Spring-Content
版本1.2.7.
我做的是这个(它至少对 DefaultFilesystemStoreImpl
s 有效):
- 像这样创建两个
ContentStore
:
@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());
}
- 将内容从一个
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
移动(/存档)内容的预期方式还是有更优雅/更方便/更预期的方式moving/archiving 文件(特别是从文件系统到 S3 并再次返回)?
Spring 内容在这里没有任何魔力。我尽量保持 API 相当 low-level 并且这个用例虽然有效,但有点太细化了。 因此,最终您必须自己构建“存档”,并在一天结束时将内容从一个商店复制到另一个商店。
几个 comments/pointers:
不确定为什么要自己实例化
mytmpfsstore1
。如果你有文件存储和 s3 存储,你可以让你的存储接口分别扩展 FileSystemContentStore 和 S3ContentStore,让框架为你实例化它们;即public interface MyTmpFsStore extends FileSystemContentStore {}
public interface MyS3store extends S3ContentStore()
然后您可以将这两个 bean 连接到您的 'archive' 代码中。假设这是一个控制器,类似于:
@RequestMapping(...) public void archive(MyTmpFsStore fsStore, S3ContentStore s3Store { ... }
您在哪里进行复制操作。
ContentStore
扩展AssociativeStore
并且 setting/unsetting 内容有一些不同的 API。 Resource-based 或 InputStream。他们在一天结束时都达到了相同的 objective。你可以很容易地使用getContent
而不是getResource
和unsetContent
而不是delete
.'archive' 代码可能需要在 @Transactional 中,因此存档操作是原子的。