List of usage examples for com.mongodb.client.gridfs GridFSBucket uploadFromStream
ObjectId uploadFromStream(ClientSession clientSession, String filename, InputStream source);
From source file:UnitTest3.java
License:Open Source License
public static void testgridfs2() throws FileNotFoundException, IOException { long time1;/*from w w w .j a va 2 s.c om*/ long time2; long time3; long time4; String fileName = "makezip3.rar"; MongoClient mongo = new MongoClient("localhost", 27017); //DB db = mongo.getDB("JFileDB"); MongoDatabase db = mongo.getDatabase("JFileDB"); GridFSBucket gridFSBucket = GridFSBuckets.create(db); // Get the input stream time1 = System.currentTimeMillis(); InputStream streamToUploadFrom = new FileInputStream("/home/anees/workspace/testdata/in/" + fileName); // Create some custom options GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(1024 * 1024) .metadata(new Document("type", "class")); ObjectId fileId = gridFSBucket.uploadFromStream(fileName, streamToUploadFrom, options); streamToUploadFrom.close(); time2 = System.currentTimeMillis(); time3 = System.currentTimeMillis(); Date date = new Date(); FileOutputStream streamToDownloadTo = new FileOutputStream( "/home/anees/workspace/testdata/out/" + fileName + "_" + date.toString()); // latest file with same name in DB GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(-1); //original file with same file name in DB GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(0); //downloadOptions. gridFSBucket.downloadToStreamByName(fileName, streamToDownloadTo, downloadOptions); streamToDownloadTo.close(); time4 = System.currentTimeMillis(); System.out.println("The fileId of the uploaded file is: " + fileId.toHexString()); System.out.println("Upload time taken : time2 - time1 : " + (time2 - time1)); System.out.println("Download time taken : time4 - time3 : " + (time4 - time3)); /* Set the revision of the file to retrieve. Revision numbers are defined as follows: 0 = the original stored file 1 = the first revision 2 = the second revision etc.. -2 = the second most recent revision -1 = the most recent revision */ mongo.close(); }
From source file:UnitTest3.java
License:Open Source License
public static void testgridfs4() throws FileNotFoundException, IOException { MongoClient mongo = new MongoClient("localhost", 27017); //DB db = mongo.getDB("JFileDB"); MongoDatabase db = mongo.getDatabase("JFileDB"); GridFSBucket gridFSBucket = GridFSBuckets.create(db); // 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", "file")); ObjectId fileId = gridFSBucket.uploadFromStream("test", streamToUploadFrom, options); streamToUploadFrom.close();//from w w w . ja v a2 s .c om System.out.println("The fileId of the uploaded file is: " + fileId.toHexString()); // Get some data to write byte[] data = "some data to upload into GridFS".getBytes(StandardCharsets.UTF_8); GridFSUploadStream uploadStream = gridFSBucket.openUploadStream("sample_data"); uploadStream.write(data); uploadStream.close(); System.out.println("The fileId of the uploaded file is: " + uploadStream.getFileId().toHexString()); /* gridFSBucket.find().forEach(new Block<GridFSFile>() { public void apply(final GridFSFile gridFSFile) { System.out.println(gridFSFile.getFilename()); } }); */ /* gridFSBucket.find(eq("metadata.contentType", "image/png")).forEach( new Block<GridFSFile>() { public void apply(final GridFSFile gridFSFile) { System.out.println(gridFSFile.getFilename()); } }); */ FileOutputStream streamToDownloadTo = new FileOutputStream("/tmp/test.txt"); gridFSBucket.downloadToStream(fileId, streamToDownloadTo); streamToDownloadTo.close(); streamToDownloadTo = new FileOutputStream("/tmp/test.txt"); GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(0); gridFSBucket.downloadToStreamByName("test", streamToDownloadTo, downloadOptions); streamToDownloadTo.close(); 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)); downloadStream = gridFSBucket.openDownloadStreamByName("sample_data"); fileLength = (int) downloadStream.getGridFSFile().getLength(); bytesToWriteTo = new byte[fileLength]; downloadStream.read(bytesToWriteTo); downloadStream.close(); System.out.println(new String(bytesToWriteTo, StandardCharsets.UTF_8)); gridFSBucket.rename(fileId, "test2"); gridFSBucket.delete(fileId); mongo.close(); }
From source file:com.imaginea.mongodb.services.impl.GridFSServiceImpl.java
License:Apache License
/** * Service implementation for creating GridFS store in the specified database. * * @param dbName Name of Database/*www .j av a 2 s. c o m*/ * @param bucketName Name of GridFS Bucket * @return Status message. */ public String createStore(String dbName, String bucketName) throws DatabaseException, CollectionException, GridFSException { if (dbName == null) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Is Null"); } if (dbName.equals("")) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Empty"); } if (bucketName == null) { throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Bucket name is null"); } if (bucketName.equals("")) { throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Bucket Name Empty"); } if (getAllBuckets(dbName).contains(bucketName)) { throw new CollectionException(ErrorCodes.COLLECTION_ALREADY_EXISTS, "Collection [" + bucketName + "] already exists in Database [" + dbName + "]"); } else { try { GridFSBucket gridFSBucket = GridFSBuckets.create(mongoInstance.getDatabase(dbName), bucketName); // Get the input stream InputStream streamToUploadFrom = new FileInputStream(new File("/home/maheshk/sample.txt")); // Create some custom options GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(1024) .metadata(new Document("type", "presentation")); ObjectId fileId = gridFSBucket.uploadFromStream("temp-file", streamToUploadFrom, options); } catch (MongoException e) { throw new GridFSException(ErrorCodes.GRIDFS_CREATION_EXCEPTION, e.getMessage()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "GridFS bucket [" + bucketName + "] added to database [" + dbName + "]."; } }
From source file:com.imaginea.mongodb.services.impl.GridFSServiceImpl.java
License:Apache License
/** * Service implementation for uploading a file to GridFS. * * @param dbName Name of Database/* ww w . ja v a 2s . c om*/ * @param bucketName Name of GridFS Bucket * @param formData formDataBodyPart of the uploaded file * @param inputStream inputStream of the uploaded file * @param connectionId ConnectionId of the connection * @return Success message with additional file details such as name, size, download url & * deletion url as JSON Array string. */ public JSONArray insertFile(String dbName, String bucketName, String connectionId, InputStream inputStream, FormDataContentDisposition fileData) throws ApplicationException { if (dbName == null) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database name is null"); } if (dbName.equals("")) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Empty"); } if (bucketName == null) { throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Bucket name is null"); } if (bucketName.equals("")) { throw new CollectionException(ErrorCodes.COLLECTION_NAME_EMPTY, "Bucket Name Empty"); } JSONArray result = new JSONArray(); try { // if (!databaseService.getDbList().contains(dbName)) { // throw new DatabaseException(ErrorCodes.DB_DOES_NOT_EXISTS, // "DB [" + dbName + "] DOES NOT EXIST"); // } MongoDatabase db = mongoInstance.getDatabase(dbName); GridFSBucket gridFS = GridFSBuckets.create(db, bucketName); GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(1024) .metadata(new Document("type", "presentation")); ObjectId fileId = gridFS.uploadFromStream(fileData.getFileName(), inputStream, options); String objectId = JSON.serialize(fileId); JSONObject obj = new JSONObject(); obj.put("name", fileData.getFileName()); // obj.put("size", fileData.); obj.put("url", String.format("services/%s/%s/gridfs/getfile?id=%s&download=%s&connectionId=%s&ts=%s", dbName, bucketName, objectId, false, connectionId, new Date())); obj.put("delete_url", String.format("services/%s/%s/gridfs/dropfile?id=%s&connectionId=%s&ts=%s", dbName, bucketName, objectId, connectionId, new Date().getTime())); obj.put("delete_type", "GET"); result.put(obj); } catch (MongoException e) { throw new CollectionException(ErrorCodes.UPLOAD_FILE_EXCEPTION, e.getMessage()); } catch (JSONException e) { throw new ApplicationException(ErrorCodes.JSON_EXCEPTION, "Error creating json response obj", e.getCause()); } return result; }
From source file:gridfs.GridFSTour.java
License:Apache License
/** * 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 *//* www . j a v a 2 s.c o m*/ 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.getFileId().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"); GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(0); gridFSBucket.downloadToStreamByName("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.openDownloadStreamByName("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(); }
From source file:org.apache.nifi.processors.mongodb.gridfs.GridFSITTestBase.java
License:Apache License
public ObjectId writeTestFile(String fileName, String content, String bucketName, Map<String, Object> attrs) { GridFSBucket bucket = GridFSBuckets.create(client.getDatabase(DB), bucketName); GridFSUploadOptions options = new GridFSUploadOptions().metadata(new Document(attrs)); ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes()); ObjectId retVal = bucket.uploadFromStream(fileName, input, options); return retVal; }
From source file:org.apache.nifi.processors.mongodb.gridfs.PutGridFS.java
License:Apache License
@Override public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { FlowFile input = session.get();//from w ww .j av a 2s . c om if (input == null) { return; } GridFSBucket bucket = getBucket(input, context); if (!canUploadFile(context, input, bucket.getBucketName())) { getLogger().error("Cannot upload the file because of the uniqueness policy configured."); session.transfer(input, REL_DUPLICATE); return; } final int chunkSize = context.getProperty(CHUNK_SIZE).evaluateAttributeExpressions(input) .asDataSize(DataUnit.B).intValue(); try (InputStream fileInput = session.read(input)) { String fileName = context.getProperty(FILE_NAME).evaluateAttributeExpressions(input).getValue(); GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(chunkSize) .metadata(getMetadata(input, context)); ObjectId id = bucket.uploadFromStream(fileName, fileInput, options); fileInput.close(); if (id != null) { input = session.putAttribute(input, ID_ATTRIBUTE, id.toString()); session.transfer(input, REL_SUCCESS); session.getProvenanceReporter().send(input, getTransitUri(id, input, context)); } else { getLogger().error("ID was null, assuming failure."); session.transfer(input, REL_FAILURE); } } catch (Exception ex) { getLogger().error("Failed to upload file", ex); session.transfer(input, REL_FAILURE); } }
From source file:org.restheart.db.GridFsDAO.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public OperationResult createFile(final Database db, final String dbName, final String bucketName, final BsonDocument metadata, final Path filePath) throws IOException, DuplicateKeyException { final String bucket = extractBucketName(bucketName); GridFSBucket gridFSBucket = GridFSBuckets.create(db.getDatabase(dbName), bucket); String filename = extractFilenameFromProperties(metadata); //add etag to metadata ObjectId etag = new ObjectId(); metadata.put("_etag", new BsonObjectId(etag)); GridFSUploadOptions options = new GridFSUploadOptions().metadata(Document.parse(metadata.toJson())); InputStream sourceStream = new FileInputStream(filePath.toFile()); ObjectId _id = gridFSBucket.uploadFromStream(filename, sourceStream, options); return new OperationResult(HttpStatus.SC_CREATED, new BsonObjectId(etag), new BsonObjectId(_id)); }