在自定义 table 中存储 gridfs 文件信息
store gridfs files information in a custom table
我正在使用 mongodb
和 java spring 平台作为我的存储系统来存储文件和文档。由于 mongo 存储文件的限制为 15MB
(Bson 存储限制),因此我使用 GridFs
扩展名来存储我的大文件。我已经实现了这部分如下:
DBObject metaData = new BasicDBObject();
metaData.put("uplad_dateTime", largeDocument.getUploadDateTime());
metaData.put("title", largeDocument.getName());
ObjectId id =gridFsTemplate.store(largeDocument.getData(), largeDocument.getName(), largeDocument.getContentType(), metaData);
largeDocument.setId(id.toString());
问题是 Gridfs 默认使用两个 fs.chunck
和 fs.files
集合,但我需要在此处描述的自定义文档模型中存储具有唯一文件 ID 的文件信息:
@Setter
@Getter
@org.springframework.data.mongodb.core.mapping.Document(collection = "document")
public class Document {
@Id
private String id;
@NotNull
@Column(name = "document_size", nullable = false)
private long size;
@NotNull
@Column(name = "document_name", nullable = false)
private String name;
@NotNull
@Column(name = "content_type", nullable = false)
private String contentType;
@NotNull
@Column(name = "content_data", nullable = false)
private InputStream data;
@NotNull
@Column(name = "upload_date_time", nullable = false)
private LocalDateTime uploadDateTime;
@Column(name = "download_counter", nullable = false)
private long downloadCounter;
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private Document document;
private Builder() {
document = new Document();
document.setUploadDateTime(LocalDateTime.now());
}
public Builder id(String id) {
document.setId(id);
return this;
}
public Builder size(long size) {
document.setSize(size);
return this;
}
public Builder name(String name) {
document.setName(name);
return this;
}
public Builder contentType(String contentType) {
document.setContentType(contentType);
return this;
}
public Builder data(InputStream data) {
document.setData(data);
return this;
}
public Builder uploadDateTime(LocalDateTime uploadDateTime) {
document.setUploadDateTime(uploadDateTime);
return this;
}
public Builder downloadCounter(long downloadCounter) {
document.setDownloadCounter(downloadCounter);
return this;
}
public Document build() {
return document;
}
}
}
如何更改 Gridfs 以在我的模型中而不是 fs.files
中存储文件信息?感谢您的帮助。
首先,gridfs 提供了一种将任意元数据与上传文件相关联的工具。这应该适用于大多数用例。
如果您需要与上传文件关联的应用程序对象,则必须将其存储在单独的集合中。
我正在使用 mongodb
和 java spring 平台作为我的存储系统来存储文件和文档。由于 mongo 存储文件的限制为 15MB
(Bson 存储限制),因此我使用 GridFs
扩展名来存储我的大文件。我已经实现了这部分如下:
DBObject metaData = new BasicDBObject();
metaData.put("uplad_dateTime", largeDocument.getUploadDateTime());
metaData.put("title", largeDocument.getName());
ObjectId id =gridFsTemplate.store(largeDocument.getData(), largeDocument.getName(), largeDocument.getContentType(), metaData);
largeDocument.setId(id.toString());
问题是 Gridfs 默认使用两个 fs.chunck
和 fs.files
集合,但我需要在此处描述的自定义文档模型中存储具有唯一文件 ID 的文件信息:
@Setter
@Getter
@org.springframework.data.mongodb.core.mapping.Document(collection = "document")
public class Document {
@Id
private String id;
@NotNull
@Column(name = "document_size", nullable = false)
private long size;
@NotNull
@Column(name = "document_name", nullable = false)
private String name;
@NotNull
@Column(name = "content_type", nullable = false)
private String contentType;
@NotNull
@Column(name = "content_data", nullable = false)
private InputStream data;
@NotNull
@Column(name = "upload_date_time", nullable = false)
private LocalDateTime uploadDateTime;
@Column(name = "download_counter", nullable = false)
private long downloadCounter;
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private Document document;
private Builder() {
document = new Document();
document.setUploadDateTime(LocalDateTime.now());
}
public Builder id(String id) {
document.setId(id);
return this;
}
public Builder size(long size) {
document.setSize(size);
return this;
}
public Builder name(String name) {
document.setName(name);
return this;
}
public Builder contentType(String contentType) {
document.setContentType(contentType);
return this;
}
public Builder data(InputStream data) {
document.setData(data);
return this;
}
public Builder uploadDateTime(LocalDateTime uploadDateTime) {
document.setUploadDateTime(uploadDateTime);
return this;
}
public Builder downloadCounter(long downloadCounter) {
document.setDownloadCounter(downloadCounter);
return this;
}
public Document build() {
return document;
}
}
}
如何更改 Gridfs 以在我的模型中而不是 fs.files
中存储文件信息?感谢您的帮助。
首先,gridfs 提供了一种将任意元数据与上传文件相关联的工具。这应该适用于大多数用例。
如果您需要与上传文件关联的应用程序对象,则必须将其存储在单独的集合中。