Example usage for com.mongodb.client.gridfs.model GridFSUploadOptions GridFSUploadOptions

List of usage examples for com.mongodb.client.gridfs.model GridFSUploadOptions GridFSUploadOptions

Introduction

In this page you can find the example usage for com.mongodb.client.gridfs.model GridFSUploadOptions GridFSUploadOptions.

Prototype

public GridFSUploadOptions() 

Source Link

Document

Construct a new instance.

Usage

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();// www. j  a v a2 s .c  o  m
    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));
}