Search in sources :

Example 1 with GridFSDownloadOptions

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

the class GridFSTest method doDownloadByName.

private void doDownloadByName(final BsonDocument arguments, final BsonDocument assertion) {
    Throwable error = null;
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        final GridFSDownloadOptions options = new GridFSDownloadOptions();
        if (arguments.containsKey("options")) {
            int revision = arguments.getDocument("options").getInt32("revision").getValue();
            options.revision(revision);
        }
        new MongoOperation<Long>() {

            @Override
            public void execute() {
                gridFSBucket.downloadToStream(arguments.getString("filename").getValue(), toAsyncOutputStream(outputStream), options, getCallback());
            }
        }.get();
        outputStream.close();
    } catch (Throwable e) {
        error = e;
    }
    if (assertion.containsKey("result")) {
        assertNull("Should not have thrown an exception", error);
        assertEquals(printHexBinary(outputStream.toByteArray()).toLowerCase(), assertion.getDocument("result").getString("$hex").getValue());
    } else if (assertion.containsKey("error")) {
        assertNotNull("Should have thrown an exception", error);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) GridFSDownloadOptions(com.mongodb.client.gridfs.model.GridFSDownloadOptions)

Example 2 with GridFSDownloadOptions

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

the class GridFSTest method doDownloadByName.

private void doDownloadByName(final BsonDocument arguments, final BsonDocument assertion) {
    Throwable error = null;
    ByteArrayOutputStream outputStream = null;
    try {
        outputStream = new ByteArrayOutputStream();
        GridFSDownloadOptions options = new GridFSDownloadOptions();
        if (arguments.containsKey("options")) {
            int revision = arguments.getDocument("options").getInt32("revision").getValue();
            options = options.revision(revision);
        }
        gridFSBucket.downloadToStream(arguments.getString("filename").getValue(), outputStream, options);
        outputStream.close();
    } catch (Throwable e) {
        error = e;
    }
    if (assertion.containsKey("result")) {
        assertNull("Should not have thrown an exception", error);
        assertEquals(printHexBinary(outputStream.toByteArray()).toLowerCase(), assertion.getDocument("result").getString("$hex").getValue());
    } else if (assertion.containsKey("error")) {
        assertNotNull("Should have thrown an exception", error);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) GridFSDownloadOptions(com.mongodb.client.gridfs.model.GridFSDownloadOptions)

Example 3 with GridFSDownloadOptions

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

the class GridFSTour method main.

/**
     * Run this main method to see the output of this quick example.
     *
     * @param args takes an optional single argument for the connection string
     * @throws FileNotFoundException if the sample file cannot be found
     * @throws IOException if there was an exception closing an input stream
     */
public static void main(final String[] args) throws FileNotFoundException, IOException {
    MongoClient mongoClient;
    if (args.length == 0) {
        // connect to the local database server
        mongoClient = new MongoClient();
    } else {
        mongoClient = new MongoClient(new MongoClientURI(args[0]));
    }
    // get handle to "mydb" database
    MongoDatabase database = mongoClient.getDatabase("mydb");
    database.drop();
    GridFSBucket gridFSBucket = GridFSBuckets.create(database);
    /*
         * UploadFromStream Example
         */
    // Get the input stream
    InputStream streamToUploadFrom = new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8));
    // Create some custom options
    GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(1024).metadata(new Document("type", "presentation"));
    ObjectId fileId = gridFSBucket.uploadFromStream("mongodb-tutorial", streamToUploadFrom, options);
    streamToUploadFrom.close();
    System.out.println("The fileId of the uploaded file is: " + fileId.toHexString());
    /*
         * OpenUploadStream Example
         */
    // Get some data to write
    byte[] data = "Data to upload into GridFS".getBytes(StandardCharsets.UTF_8);
    GridFSUploadStream uploadStream = gridFSBucket.openUploadStream("sampleData");
    uploadStream.write(data);
    uploadStream.close();
    System.out.println("The fileId of the uploaded file is: " + uploadStream.getObjectId().toHexString());
    /*
         * Find documents
         */
    gridFSBucket.find().forEach(new Block<GridFSFile>() {

        @Override
        public void apply(final GridFSFile gridFSFile) {
            System.out.println(gridFSFile.getFilename());
        }
    });
    /*
         * Find documents with a filter
         */
    gridFSBucket.find(eq("metadata.contentType", "image/png")).forEach(new Block<GridFSFile>() {

        @Override
        public void apply(final GridFSFile gridFSFile) {
            System.out.println(gridFSFile.getFilename());
        }
    });
    /*
         * DownloadToStream
         */
    FileOutputStream streamToDownloadTo = new FileOutputStream("/tmp/mongodb-tutorial.txt");
    gridFSBucket.downloadToStream(fileId, streamToDownloadTo);
    streamToDownloadTo.close();
    /*
         * DownloadToStreamByName
         */
    streamToDownloadTo = new FileOutputStream("/tmp/mongodb-tutorial.txt");
    GridFSDownloadOptions downloadOptions = new GridFSDownloadOptions().revision(0);
    gridFSBucket.downloadToStream("mongodb-tutorial", streamToDownloadTo, downloadOptions);
    streamToDownloadTo.close();
    /*
         * OpenDownloadStream
         */
    GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStream(fileId);
    int fileLength = (int) downloadStream.getGridFSFile().getLength();
    byte[] bytesToWriteTo = new byte[fileLength];
    downloadStream.read(bytesToWriteTo);
    downloadStream.close();
    System.out.println(new String(bytesToWriteTo, StandardCharsets.UTF_8));
    /*
         * OpenDownloadStreamByName
         */
    downloadStream = gridFSBucket.openDownloadStream("sampleData");
    fileLength = (int) downloadStream.getGridFSFile().getLength();
    bytesToWriteTo = new byte[fileLength];
    downloadStream.read(bytesToWriteTo);
    downloadStream.close();
    System.out.println(new String(bytesToWriteTo, StandardCharsets.UTF_8));
    /*
         * Rename
         */
    gridFSBucket.rename(fileId, "mongodbTutorial");
    /*
         * Delete
         */
    gridFSBucket.delete(fileId);
    database.drop();
}
Also used : ObjectId(org.bson.types.ObjectId) GridFSUploadStream(com.mongodb.client.gridfs.GridFSUploadStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MongoClientURI(com.mongodb.MongoClientURI) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) Document(org.bson.Document) GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) MongoClient(com.mongodb.MongoClient) ByteArrayInputStream(java.io.ByteArrayInputStream) GridFSDownloadStream(com.mongodb.client.gridfs.GridFSDownloadStream) FileOutputStream(java.io.FileOutputStream) GridFSDownloadOptions(com.mongodb.client.gridfs.model.GridFSDownloadOptions) MongoDatabase(com.mongodb.client.MongoDatabase) GridFSUploadOptions(com.mongodb.client.gridfs.model.GridFSUploadOptions)

Aggregations

GridFSDownloadOptions (com.mongodb.client.gridfs.model.GridFSDownloadOptions)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 MongoClient (com.mongodb.MongoClient)1 MongoClientURI (com.mongodb.MongoClientURI)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 GridFSBucket (com.mongodb.client.gridfs.GridFSBucket)1 GridFSDownloadStream (com.mongodb.client.gridfs.GridFSDownloadStream)1 GridFSUploadStream (com.mongodb.client.gridfs.GridFSUploadStream)1 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)1 GridFSUploadOptions (com.mongodb.client.gridfs.model.GridFSUploadOptions)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 Document (org.bson.Document)1 ObjectId (org.bson.types.ObjectId)1