Java Code Examples for com.mongodb.client.gridfs.GridFSBucket
以下是显示如何使用com.mongodb.client.gridfs.GridFSBucket的示例。
Example 1
private void prepareData ( String filename, int size, Vertx vertx,
Handler< AsyncResult< String> > handler) {
vertx. < String> executeBlocking ( f - > {
try ( MongoClient client = new MongoClient ( mongoConnector. serverAddress) ) {
MongoDatabase db = client. getDatabase ( MongoDBTestConnector. MONGODB_DBNAME) ;
GridFSBucket gridFS = GridFSBuckets. create ( db) ;
try ( GridFSUploadStream os = gridFS. openUploadStream ( filename) ) {
for ( int i = 0 ; i < size; ++ i) {
os. write ( ( byte ) ( i & 0xFF ) ) ;
}
}
}
f. complete ( filename) ;
} , handler) ;
}
Example 2
@Override
protected void validateAfterStoreAdd ( TestContext context, Vertx vertx,
String path, Handler< AsyncResult< Void> > handler) {
vertx. executeBlocking ( f - > {
try ( MongoClient client = new MongoClient ( mongoConnector. serverAddress) ) {
MongoDatabase db = client. getDatabase ( MongoDBTestConnector. MONGODB_DBNAME) ;
GridFSBucket gridFS = GridFSBuckets. create ( db) ;
GridFSFindIterable files = gridFS. find ( ) ;
GridFSFile file = files. first ( ) ;
ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ;
gridFS. downloadToStream ( file. getFilename ( ) , baos) ;
String contents = new String ( baos. toByteArray ( ) , StandardCharsets. UTF_8) ;
context. assertEquals ( CHUNK_CONTENT, contents) ;
}
f. complete ( ) ;
} , handler) ;
}
Example 3
@Override
public InputStream getAssociatedDocumentStream ( String uniqueId, String fileName) {
GridFSBucket gridFS = createGridFSConnection ( ) ;
GridFSFile file = gridFS. find ( new Document ( ASSOCIATED_METADATA + "." + FILE_UNIQUE_ID_KEY, getGridFsId ( uniqueId, fileName) ) ) . first ( ) ;
if ( file == null) {
return null;
}
InputStream is = gridFS. openDownloadStream ( file. getObjectId ( ) ) ;
;
Document metadata = file. getMetadata ( ) ;
if ( metadata. containsKey ( COMPRESSED_FLAG) ) {
boolean compressed = ( boolean ) metadata. remove ( COMPRESSED_FLAG) ;
if ( compressed) {
is = new InflaterInputStream ( is) ;
}
}
return is;
}
Example 4
private void uploadStream ( SmofGridRef ref, String name, InputStream stream) {
final String bucketName = ref. getBucketName ( ) ;
final ObjectId id;
final GridFSBucket bucket;
Preconditions. checkNotNull ( bucketName, "No bucket specified" ) ;
final GridFSUploadOptions options = new GridFSUploadOptions ( ) . metadata ( ref. getMetadata ( ) ) ;
bucket = pool. getBucket ( bucketName) ;
id = bucket. uploadFromStream ( name, stream, options) ;
ref. setId ( id) ;
}
Example 5
@Override
public InputStream download ( SmofGridRef ref) {
final String bucketName = ref. getBucketName ( ) ;
final ObjectId id = ref. getId ( ) ;
Preconditions. checkArgument ( id != null, "No download source found" ) ;
Preconditions. checkArgument ( bucketName != null, "No bucket specified" ) ;
final GridFSBucket bucket = pool. getBucket ( bucketName) ;
return bucket. openDownloadStream ( id) ;
}
Example 6
@Override
public void drop ( SmofGridRef ref) {
final String bucketName = ref. getBucketName ( ) ;
final ObjectId id = ref. getId ( ) ;
Preconditions. checkArgument ( id != null, "No download source found" ) ;
Preconditions. checkArgument ( bucketName != null, "No bucket specified" ) ;
final GridFSBucket bucket = pool. getBucket ( bucketName) ;
bucket. delete ( id) ;
}
Example 7
private void deleteLog ( Date olderThan) {
MongoCollection< Log> logCollection = mongoService. getMongoClient ( ) . getDatabase ( database) . getCollection ( collection, Log. class ) ;
Bson filter = Filters. lt ( "timeStamp" , olderThan) ;
logCollection. find ( filter) . forEach ( ( Block< ? super Log> ) log - > {
log. getLogFiles ( ) . forEach ( logFile - > {
GridFSBucket gridFSBucket = GridFSBuckets. create ( mongoService. getMongoClient ( ) . getDatabase ( database) , logFile. getBucket ( ) ) ;
gridFSBucket. delete ( logFile. getFileObjectId ( )