Java Code Examples for com.mongodb.client.gridfs.GridFSBucket

本文提供了一系列的Java代码示例,详细展示了如何操作 MongoDB 的 GridFSBucket,包括各种不同的用例,从基本操作到复杂的应用。
摘要由CSDN通过智能技术生成

Java Code Examples for com.mongodb.client.gridfs.GridFSBucket

以下是显示如何使用com.mongodb.client.gridfs.GridFSBucket的示例。

Example 1

/**
 * Create a file in GridFS with the given filename and write
 * some random data to it.
 * @param filename the name of the file to create
 * @param size the number of random bytes to write
 * @param vertx the Vert.x instance
 * @param handler a handler that will be called when the file
 * has been written
 */
private void prepareData(String filename, int size, Vertx vertx,
    Handler<AsyncResult<String>> handler) {
   
  vertx.<String>executeBlocking(f -> {
   
    try (MongoClient client = new MongoClient(mongoConnector.serverAddress)) {
   
      MongoDatabase db = client.getDatabase(MongoDBTestConnector.MONGODB_DBNAME);
      GridFSBucket gridFS = GridFSBuckets.create(db);
      try (GridFSUploadStream os = gridFS.openUploadStream(filename)) {
   
        for (int i = 0; i < size; ++i) {
   
          os.write((byte)(i & 0xFF));
        }
      }
    }
    f.complete(filename);
  }, handler);
}

Example 2

@Override
protected void validateAfterStoreAdd(TestContext context, Vertx vertx,
    String path, Handler<AsyncResult<Void>> handler) {
   
  vertx.executeBlocking(f -> {
   
    try (MongoClient client = new MongoClient(mongoConnector.serverAddress)) {
   
      MongoDatabase db = client.getDatabase(MongoDBTestConnector.MONGODB_DBNAME);
      GridFSBucket gridFS = GridFSBuckets.create(db);

      GridFSFindIterable files = gridFS.find();

      GridFSFile file = files.first();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      gridFS.downloadToStream(file.getFilename(), baos);
      String contents = new String(baos.toByteArray(), StandardCharsets.UTF_8);
      context.assertEquals(CHUNK_CONTENT, contents);
    }
    f.complete();
  }, handler);
}

Example 3

@Override
public InputStream getAssociatedDocumentStream(String uniqueId, String fileName) {
   
	GridFSBucket gridFS = createGridFSConnection();
	GridFSFile file = gridFS.find(new Document(ASSOCIATED_METADATA + "." + FILE_UNIQUE_ID_KEY, getGridFsId(uniqueId, fileName))).first();

	if (file == null) {
   
		return null;
	}

	InputStream is = gridFS.openDownloadStream(file.getObjectId());
	;

	Document metadata = file.getMetadata();
	if (metadata.containsKey(COMPRESSED_FLAG)) {
   
		boolean compressed = (boolean) metadata.remove(COMPRESSED_FLAG);
		if (compressed) {
   
			is = new InflaterInputStream(is);
		}
	}

	return is;
}

Example 4

private void uploadStream(SmofGridRef ref, String name, InputStream stream) {
   
	final String bucketName = ref.getBucketName();
	final ObjectId id;
	final GridFSBucket bucket;
	Preconditions.checkNotNull(bucketName, "No bucket specified");
	final GridFSUploadOptions options = new GridFSUploadOptions().metadata(ref.getMetadata());
	bucket = pool.getBucket(bucketName);
	id = bucket.uploadFromStream(name, stream, options);
	ref.setId(id);
}

Example 5

@Override
public InputStream download(SmofGridRef ref) {
   
	final String bucketName = ref.getBucketName();
	final ObjectId id = ref.getId();
	Preconditions.checkArgument(id != null, "No download source found");
	Preconditions.checkArgument(bucketName != null, "No bucket specified");
	final GridFSBucket bucket = pool.getBucket(bucketName);
	return bucket.openDownloadStream(id);
}

Example 6

@Override
public void drop(SmofGridRef ref) {
   
	final String bucketName = ref.getBucketName();
	final ObjectId id = ref.getId();
	Preconditions.checkArgument(id != null, "No download source found");
	Preconditions.checkArgument(bucketName != null, "No bucket specified");
	final GridFSBucket bucket = pool.getBucket(bucketName);
	bucket.delete(id);
}

Example 7

private void deleteLog(Date olderThan) {
   
    MongoCollection<Log> logCollection = mongoService.getMongoClient().getDatabase(database).getCollection(collection, Log.class);
    Bson filter = Filters.lt("timeStamp", olderThan);
    logCollection.find(filter).forEach((Block<? super Log>) log -> {
   
        log.getLogFiles().forEach(logFile -> {
   
            GridFSBucket gridFSBucket = GridFSBuckets.create(mongoService.getMongoClient().getDatabase(database), logFile.getBucket());
            gridFSBucket.delete(logFile.getFileObjectId()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值