在自定义表中存储GridFS文件信息。

huangapple go评论99阅读模式
英文:

store gridfs files information in a custom table

问题

我正在使用 mongodb 以及 Java Spring 平台作为存储系统来存储文件和文档。由于 MongoDB 有一个 15MB(Bson 存储限制)的文件存储限制,我已经使用了 GridFs 扩展来存储我的大文件。我已经按照以下方式实现了这部分内容:

  1. DBObject metaData = new BasicDBObject();
  2. metaData.put("uplad_dateTime", largeDocument.getUploadDateTime());
  3. metaData.put("title", largeDocument.getName());
  4. ObjectId id =gridFsTemplate.store(largeDocument.getData(), largeDocument.getName(), largeDocument.getContentType(), metaData);
  5. largeDocument.setId(id.toString());

问题是,默认情况下,GridFS 使用两个集合 fs.chunckfs.files,但我需要将文件信息与唯一文件 ID 存储在一个自定义文档模型中,如下所述:

  1. @Setter
  2. @Getter
  3. @org.springframework.data.mongodb.core.mapping.Document(collection = "document")
  4. public class Document {
  5. @Id
  6. private String id;
  7. @NotNull
  8. @Column(name = "document_size", nullable = false)
  9. private long size;
  10. @NotNull
  11. @Column(name = "document_name", nullable = false)
  12. private String name;
  13. @NotNull
  14. @Column(name = "content_type", nullable = false)
  15. private String contentType;
  16. @NotNull
  17. @Column(name = "content_data", nullable = false)
  18. private InputStream data;
  19. @NotNull
  20. @Column(name = "upload_date_time", nullable = false)
  21. private LocalDateTime uploadDateTime;
  22. @Column(name = "download_counter", nullable = false)
  23. private long downloadCounter;
  24. // ...(Builder 类的定义和方法)
  25. }

我该如何更改 GridFS,以便将文件信息存储在我的模型中,而不是 fs.files 中?非常感谢您的帮助。

英文:

I am using mongodb along with a java spring platform as my storage system to store files and documents. As mongo has a limit of 15MB(Bson storage limit) to store files I have used GridFs extension to store my large files. I have implemented this part as follow:

  1. DBObject metaData = new BasicDBObject();
  2. metaData.put("uplad_dateTime", largeDocument.getUploadDateTime());
  3. metaData.put("title", largeDocument.getName());
  4. ObjectId id =gridFsTemplate.store(largeDocument.getData(), largeDocument.getName(), largeDocument.getContentType(), metaData);
  5. largeDocument.setId(id.toString());

The problem is that Gridfs by default uses two fs.chunck and fs.files collections, But I need to store files information with unique file id in a custom document model described here:

  1. @Setter
  2. @Getter
  3. @org.springframework.data.mongodb.core.mapping.Document(collection = "document")
  4. public class Document {
  5. @Id
  6. private String id;
  7. @NotNull
  8. @Column(name = "document_size", nullable = false)
  9. private long size;
  10. @NotNull
  11. @Column(name = "document_name", nullable = false)
  12. private String name;
  13. @NotNull
  14. @Column(name = "content_type", nullable = false)
  15. private String contentType;
  16. @NotNull
  17. @Column(name = "content_data", nullable = false)
  18. private InputStream data;
  19. @NotNull
  20. @Column(name = "upload_date_time", nullable = false)
  21. private LocalDateTime uploadDateTime;
  22. @Column(name = "download_counter", nullable = false)
  23. private long downloadCounter;
  24. public static Builder builder() {
  25. return new Builder();
  26. }
  27. public static final class Builder {
  28. private Document document;
  29. private Builder() {
  30. document = new Document();
  31. document.setUploadDateTime(LocalDateTime.now());
  32. }
  33. public Builder id(String id) {
  34. document.setId(id);
  35. return this;
  36. }
  37. public Builder size(long size) {
  38. document.setSize(size);
  39. return this;
  40. }
  41. public Builder name(String name) {
  42. document.setName(name);
  43. return this;
  44. }
  45. public Builder contentType(String contentType) {
  46. document.setContentType(contentType);
  47. return this;
  48. }
  49. public Builder data(InputStream data) {
  50. document.setData(data);
  51. return this;
  52. }
  53. public Builder uploadDateTime(LocalDateTime uploadDateTime) {
  54. document.setUploadDateTime(uploadDateTime);
  55. return this;
  56. }
  57. public Builder downloadCounter(long downloadCounter) {
  58. document.setDownloadCounter(downloadCounter);
  59. return this;
  60. }
  61. public Document build() {
  62. return document;
  63. }
  64. }

}

How can I change Gridfs to store file information in my model instead of fs.files? I appreciate any help.

答案1

得分: 0

首先,gridfs提供了一个功能,可以将任意元数据与上传的文件关联起来。这应该适用于大多数用例。

如果您需要将应用程序对象与上传的文件关联,必须将其存储在单独的集合中。

英文:

First, gridfs provides a facility to associate arbitrary metadata with the uploaded files. This should serve most use cases.

If you require an application object associated with uploaded files, it must be stored in a separate collection.

huangapple
  • 本文由 发表于 2020年10月20日 18:51:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64443634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定