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);
}
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)));
}
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);
}
}
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);
}
}
Aggregations