Search in sources :

Example 11 with GridFSBucket

use of com.mongodb.client.gridfs.GridFSBucket in project georocket by georocket.

the class MongoDBStoreTest method prepareData.

@Override
protected void prepareData(TestContext context, Vertx vertx, String path, Handler<AsyncResult<String>> handler) {
    String filename = PathUtils.join(path, ID);
    vertx.<String>executeBlocking(f -> {
        try (MongoClient client = new MongoClient(mongoConnector.serverAddress)) {
            MongoDatabase db = client.getDatabase(MongoDBTestConnector.MONGODB_DBNAME);
            GridFSBucket gridFS = GridFSBuckets.create(db);
            byte[] contents = CHUNK_CONTENT.getBytes(StandardCharsets.UTF_8);
            gridFS.uploadFromStream(filename, new ByteArrayInputStream(contents));
            f.complete(filename);
        }
    }, handler);
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) MongoClient(com.mongodb.MongoClient) ByteArrayInputStream(java.io.ByteArrayInputStream) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 12 with GridFSBucket

use of com.mongodb.client.gridfs.GridFSBucket in project mongo-java-driver by mongodb.

the class JsonPoweredCrudTestHelper method getUploadResult.

BsonDocument getUploadResult(final BsonDocument collectionOptions, final BsonDocument rawArguments, @Nullable final ClientSession clientSession) {
    ObjectId objectId = null;
    BsonDocument arguments = parseHexDocument(rawArguments, "source");
    GridFSBucket gridFSUploadBucket = gridFSBucket;
    String filename = arguments.getString("filename").getValue();
    InputStream input = new ByteArrayInputStream(arguments.getBinary("source").getData());
    GridFSUploadOptions options = new GridFSUploadOptions();
    BsonDocument rawOptions = arguments.getDocument("options", new BsonDocument());
    if (rawOptions.containsKey("chunkSizeBytes")) {
        options.chunkSizeBytes(rawOptions.getInt32("chunkSizeBytes").getValue());
    }
    if (rawOptions.containsKey("metadata")) {
        options.metadata(Document.parse(rawOptions.getDocument("metadata").toJson()));
    }
    return new BsonDocument("objectId", new BsonObjectId(gridFSUploadBucket.uploadFromStream(filename, input, options)));
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) BsonDocument(org.bson.BsonDocument) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BsonString(org.bson.BsonString) BsonObjectId(org.bson.BsonObjectId) GridFSUploadOptions(com.mongodb.client.gridfs.model.GridFSUploadOptions)

Example 13 with GridFSBucket

use of com.mongodb.client.gridfs.GridFSBucket in project mongo-java-driver by mongodb.

the class UnifiedGridFSHelper method executeDownloadByName.

public OperationResult executeDownloadByName(final BsonDocument operation) {
    GridFSBucket bucket = entities.getBucket(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    String filename = arguments.getString("filename").getValue();
    requireNonNull(filename);
    GridFSDownloadOptions options = getDownloadOptions(arguments);
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bucket.downloadToStream(filename, baos, options);
        return OperationResult.of(new BsonString(HexUtils.toHex(baos.toByteArray())));
    } catch (Exception e) {
        return OperationResult.of(e);
    }
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GridFSDownloadOptions(com.mongodb.client.gridfs.model.GridFSDownloadOptions)

Example 14 with GridFSBucket

use of com.mongodb.client.gridfs.GridFSBucket in project vcell by virtualcell.

the class VCMongoDbDriver method removeBLOB.

public void removeBLOB(ObjectId objectId) {
    try {
        if (m == null) {
            String mongoDbHost = PropertyLoader.getRequiredProperty(PropertyLoader.mongodbHostInternal);
            // default 27017
            int mongoDbPort = Integer.parseInt(PropertyLoader.getRequiredProperty(PropertyLoader.mongodbPortInternal));
            m = new MongoClient(mongoDbHost, mongoDbPort);
        }
        MongoDatabase db = m.getDatabase(mongoDbDatabaseName);
        GridFSBucket gridFSBucket = GridFSBuckets.create(db);
        gridFSBucket.delete(objectId);
    } catch (Exception e) {
        e.printStackTrace(System.out);
        try {
            if (m != null) {
                m.close();
            }
        } catch (Exception e2) {
            e2.printStackTrace(System.out);
        } finally {
            m = null;
        }
        throw new RuntimeException("failed to retrieve BLOB with ObjectId " + objectId.toHexString() + ": " + e.getMessage(), e);
    }
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) MongoClient(com.mongodb.MongoClient) MongoException(com.mongodb.MongoException) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

GridFSBucket (com.mongodb.client.gridfs.GridFSBucket)14 MongoDatabase (com.mongodb.client.MongoDatabase)9 MongoClient (com.mongodb.MongoClient)8 ByteArrayInputStream (java.io.ByteArrayInputStream)5 BsonDocument (org.bson.BsonDocument)5 GridFSUploadOptions (com.mongodb.client.gridfs.model.GridFSUploadOptions)4 BsonString (org.bson.BsonString)4 MongoException (com.mongodb.MongoException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 BsonValue (org.bson.BsonValue)3 ObjectId (org.bson.types.ObjectId)3 GridFSFindIterable (com.mongodb.client.gridfs.GridFSFindIterable)2 GridFSUploadStream (com.mongodb.client.gridfs.GridFSUploadStream)2 GridFSDownloadOptions (com.mongodb.client.gridfs.model.GridFSDownloadOptions)2 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)2 InputStream (java.io.InputStream)2 BsonObjectId (org.bson.BsonObjectId)2 Document (org.bson.Document)2 MongoClient (com.mongodb.client.MongoClient)1 GridFSDownloadStream (com.mongodb.client.gridfs.GridFSDownloadStream)1