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

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

store gridfs files information in a custom table

问题

我正在使用 mongodb 以及 Java Spring 平台作为存储系统来存储文件和文档。由于 MongoDB 有一个 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.chunckfs.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;

// ...(Builder 类的定义和方法)
}

我该如何更改 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:

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());

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:

@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;
    }
}

}

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:

确定