Skip to content

Commit 17abd87

Browse files
committed
Allow applications to set the files_id when uploading a GridFS file
JAVA-2188
1 parent 0722135 commit 17abd87

File tree

23 files changed

+710
-165
lines changed

23 files changed

+710
-165
lines changed

bson/src/main/org/bson/BsonObjectId.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ public class BsonObjectId extends BsonValue implements Comparable<BsonObjectId>
2727

2828
private final ObjectId value;
2929

30+
/**
31+
* Construct a new instance with a new {@code ObjectId}.
32+
*/
33+
public BsonObjectId() {
34+
this(new ObjectId());
35+
}
36+
3037
/**
3138
* Construct a new instance with the given {@code ObjectId} instance.
3239
* @param value the ObjectId

docs/reference/content/driver/reference/gridfs/index.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,14 @@ streamToDownloadTo.close();
128128
System.out.println(streamToDownloadTo.toString());
129129
```
130130

131-
### DownloadToStreamByName
132-
133-
If you don't know the [`ObjectId`]({{< apiref "org/bson/types/ObjectId.html">}}) of the file you want to download, then you use the [`downloadToStreamByName`]({{< apiref "com/mongodb/client/gridfs/GridFSBucket.html#downloadToStreamByName-java.lang.String-java.io.OutputStream-com.mongodb.client.gridfs.model.GridFSDownloadByNameOptions-" >}}) method. By default it will download the latest version of the file. Use the [`GridFSDownloadByNameOptions`]({{< apiref "com/mongodb/client/gridfs/model/GridFSDownloadByNameOptions.html" >}}) to configure which version to download.
131+
If you don't know the [`ObjectId`]({{< apiref "org/bson/types/ObjectId.html">}}) of the file but know the filename, you can use the [`downloadToStream`]({{< apiref "com/mongodb/client/gridfs/GridFSBucket.html#downloadToStream-java.lang.String-java.io.OutputStream-com.mongodb.client.gridfs.model.GridFSDownloadByNameOptions-" >}}) method. By default it will download the latest version of the file. Use the [`GridFSDownloadByNameOptions`]({{< apiref "com/mongodb/client/gridfs/model/GridFSDownloadByNameOptions.html" >}}) to configure which version to download.
134132

135133
The following example downloads the original version of the file named "mongodb-tutorial" into the `OutputStream`:
136134

137135
```java
138136
FileOutputStream streamToDownloadTo = new FileOutputStream("/tmp/mongodb-tutorial.pdf");
139137
GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(0);
140-
gridFSBucket.downloadToStreamByName("mongodb-tutorial", streamToDownloadTo, downloadOptions);
138+
gridFSBucket.downloadToStream("mongodb-tutorial", streamToDownloadTo, downloadOptions);
141139
streamToDownloadTo.close();
142140
```
143141

@@ -157,14 +155,12 @@ downloadStream.close();
157155
System.out.println(new String(bytesToWriteTo, StandardCharsets.UTF_8));
158156
```
159157

160-
### OpenDownloadStreamByName
161-
162-
You can also open a `GridFSDownloadStream` by searching against the filename, using the [`openDownloadStreamByName`]({{< apiref "com/mongodb/client/gridfs/GridFSBucket.html#openDownloadStreamByName-java.lang.String-com.mongodb.client.gridfs.model.GridFSDownloadByNameOptions-" >}}) method. By default it will download the latest version of the file. Use the [`GridFSDownloadByNameOptions`]({{< apiref "com/mongodb/client/gridfs/model/GridFSDownloadByNameOptions.html" >}}) to configure which version to download.
158+
You can also open a `GridFSDownloadStream` by searching against the filename, using the [`openDownloadStream`]({{< apiref "com/mongodb/client/gridfs/GridFSBucket.html#openDownloadStream-java.lang.String-com.mongodb.client.gridfs.model.GridFSDownloadByNameOptions-" >}}) method. By default it will download the latest version of the file. Use the [`GridFSDownloadByNameOptions`]({{< apiref "com/mongodb/client/gridfs/model/GridFSDownloadByNameOptions.html" >}}) to configure which version to download.
163159

164160
The following example downloads the latest version of the file named "sampleData" into the `OutputStream`:
165161

166162
```java
167-
GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStreamByName("sampleData");
163+
GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStream("sampleData");
168164
int fileLength = (int) downloadStream.getGridFSFile().getLength();
169165
byte[] bytesToWriteTo = new byte[fileLength];
170166
downloadStream.read(bytesToWriteTo);

driver-async/src/examples/gridfs/GridFSTour.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import com.mongodb.async.client.gridfs.GridFSBuckets;
2727
import com.mongodb.async.client.gridfs.GridFSDownloadStream;
2828
import com.mongodb.async.client.gridfs.GridFSUploadStream;
29-
import com.mongodb.client.gridfs.model.GridFSDownloadByNameOptions;
29+
import com.mongodb.client.gridfs.model.GridFSDownloadOptions;
3030
import com.mongodb.client.gridfs.model.GridFSFile;
3131
import com.mongodb.client.gridfs.model.GridFSUploadOptions;
3232
import org.bson.Document;
@@ -126,7 +126,7 @@ public void onResult(final Void result, final Throwable t) {
126126
@Override
127127
public void onResult(final Integer result, final Throwable t) {
128128
uploadLatch2.countDown();
129-
System.out.println("The fileId of the uploaded file is: " + uploadStream.getFileId().toHexString());
129+
System.out.println("The fileId of the uploaded file is: " + uploadStream.getObjectId().toHexString());
130130

131131
uploadStream.close(new SingleResultCallback<Void>() {
132132
@Override
@@ -199,8 +199,8 @@ public void onResult(final Long result, final Throwable t) {
199199
final CountDownLatch downloadLatch2 = new CountDownLatch(1);
200200
streamToDownloadTo = AsynchronousFileChannel.open(outputPath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE,
201201
StandardOpenOption.DELETE_ON_CLOSE);
202-
GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(0);
203-
gridFSBucket.downloadToStreamByName("mongodb-tutorial", channelToOutputStream(streamToDownloadTo), downloadOptions,
202+
GridFSDownloadOptions downloadOptions = new GridFSDownloadOptions().revision(0);
203+
gridFSBucket.downloadToStream("mongodb-tutorial", channelToOutputStream(streamToDownloadTo), downloadOptions,
204204
new SingleResultCallback<Long>() {
205205
@Override
206206
public void onResult(final Long result, final Throwable t) {
@@ -241,7 +241,7 @@ public void onResult(final Void result, final Throwable t) {
241241
System.out.println("ByName");
242242
dstByteBuffer.clear();
243243
final CountDownLatch downloadLatch4 = new CountDownLatch(1);
244-
final GridFSDownloadStream downloadStreamByName = gridFSBucket.openDownloadStreamByName("sampleData");
244+
final GridFSDownloadStream downloadStreamByName = gridFSBucket.openDownloadStream("sampleData");
245245
downloadStreamByName.read(dstByteBuffer, new SingleResultCallback<Integer>() {
246246
@Override
247247
public void onResult(final Integer result, final Throwable t) {

driver-async/src/main/com/mongodb/async/client/gridfs/GridFSBucket.java

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.mongodb.WriteConcern;
2222
import com.mongodb.annotations.ThreadSafe;
2323
import com.mongodb.async.SingleResultCallback;
24-
import com.mongodb.client.gridfs.model.GridFSDownloadByNameOptions;
24+
import com.mongodb.client.gridfs.model.GridFSDownloadOptions;
2525
import com.mongodb.client.gridfs.model.GridFSUploadOptions;
2626
import org.bson.BsonValue;
2727
import org.bson.conversions.Bson;
@@ -136,7 +136,38 @@ public interface GridFSBucket {
136136
GridFSUploadStream openUploadStream(String filename, GridFSUploadOptions options);
137137

138138
/**
139-
* Uploads a user file to a GridFS bucket.
139+
* Opens a AsyncOutputStream that the application can write the contents of the file to.
140+
* <p>
141+
* As the application writes the contents to the returned Stream, the contents are uploaded as chunks in the chunks collection. When
142+
* the application signals it is done writing the contents of the file by calling close on the returned Stream, a files collection
143+
* document is created in the files collection.
144+
* </p>
145+
*
146+
* @param id the custom id value of the file
147+
* @param filename the filename for the stream
148+
* @return the GridFSUploadStream that provides the ObjectId for the file to be uploaded and the Stream to which the
149+
* application will write the contents.
150+
*/
151+
GridFSUploadStream openUploadStream(BsonValue id, String filename);
152+
153+
/**
154+
* Opens a AsyncOutputStream that the application can write the contents of the file to.
155+
* <p>
156+
* As the application writes the contents to the returned Stream, the contents are uploaded as chunks in the chunks collection. When
157+
* the application signals it is done writing the contents of the file by calling close on the returned Stream, a files collection
158+
* document is created in the files collection.
159+
* </p>
160+
*
161+
* @param id the custom id value of the file
162+
* @param filename the filename for the stream
163+
* @param options the GridFSUploadOptions
164+
* @return the GridFSUploadStream that provides the ObjectId for the file to be uploaded and the Stream to which the
165+
* application will write the contents.
166+
*/
167+
GridFSUploadStream openUploadStream(BsonValue id, String filename, GridFSUploadOptions options);
168+
169+
/**
170+
* Uploads the contents of the given {@code AsyncInputStream} to a GridFS bucket.
140171
* <p>
141172
* Reads the contents of the user file from the {@code source} and uploads it as chunks in the chunks collection. After all the
142173
* chunks have been uploaded, it creates a files collection document for {@code filename} in the files collection.
@@ -149,7 +180,7 @@ public interface GridFSBucket {
149180
void uploadFromStream(String filename, AsyncInputStream source, SingleResultCallback<ObjectId> callback);
150181

151182
/**
152-
* Uploads a user file to a GridFS bucket.
183+
* Uploads the contents of the given {@code AsyncInputStream} to a GridFS bucket.
153184
* <p>
154185
* Reads the contents of the user file from the {@code source} and uploads it as chunks in the chunks collection. After all the
155186
* chunks have been uploaded, it creates a files collection document for {@code filename} in the files collection.
@@ -162,6 +193,36 @@ public interface GridFSBucket {
162193
*/
163194
void uploadFromStream(String filename, AsyncInputStream source, GridFSUploadOptions options, SingleResultCallback<ObjectId> callback);
164195

196+
/**
197+
* Uploads the contents of the given {@code AsyncInputStream} to a GridFS bucket.
198+
* <p>
199+
* Reads the contents of the user file from the {@code source} and uploads it as chunks in the chunks collection. After all the
200+
* chunks have been uploaded, it creates a files collection document for {@code filename} in the files collection.
201+
* </p>
202+
*
203+
* @param id the custom id value of the file
204+
* @param filename the filename for the stream
205+
* @param source the Stream providing the file data
206+
* @param callback with the ObjectId of the uploaded file.
207+
*/
208+
void uploadFromStream(BsonValue id, String filename, AsyncInputStream source, SingleResultCallback<Void> callback);
209+
210+
/**
211+
* Uploads the contents of the given {@code AsyncInputStream} to a GridFS bucket.
212+
* <p>
213+
* Reads the contents of the user file from the {@code source} and uploads it as chunks in the chunks collection. After all the
214+
* chunks have been uploaded, it creates a files collection document for {@code filename} in the files collection.
215+
* </p>
216+
*
217+
* @param id the custom id value of the file
218+
* @param filename the filename for the stream
219+
* @param source the Stream providing the file data
220+
* @param options the GridFSUploadOptions
221+
* @param callback with the ObjectId of the uploaded file.
222+
*/
223+
void uploadFromStream(BsonValue id, String filename, AsyncInputStream source, GridFSUploadOptions options,
224+
SingleResultCallback<Void> callback);
225+
165226
/**
166227
* Opens a AsyncInputStream from which the application can read the contents of the stored file specified by {@code id}.
167228
*
@@ -185,9 +246,7 @@ public interface GridFSBucket {
185246
*
186247
* @param id the custom id value of the file, to be put into a stream.
187248
* @return the stream
188-
* @deprecated using custom id values for with GridFS is no longer supported
189249
*/
190-
@Deprecated
191250
GridFSDownloadStream openDownloadStream(BsonValue id);
192251

193252
/**
@@ -197,9 +256,7 @@ public interface GridFSBucket {
197256
* @param id the custom id of the file, to be written to the destination stream
198257
* @param destination the destination stream
199258
* @param callback the callback that is completed once the file has been downloaded
200-
* @deprecated using custom id values for with GridFS is no longer supported
201259
*/
202-
@Deprecated
203260
void downloadToStream(BsonValue id, AsyncOutputStream destination, SingleResultCallback<Long> callback);
204261

205262
/**
@@ -209,7 +266,7 @@ public interface GridFSBucket {
209266
* @param filename the name of the file to be downloaded
210267
* @return the stream
211268
*/
212-
GridFSDownloadStream openDownloadStreamByName(String filename);
269+
GridFSDownloadStream openDownloadStream(String filename);
213270

214271
/**
215272
* Opens a Stream from which the application can read the contents of the stored file specified by {@code filename} and the revision
@@ -219,7 +276,7 @@ public interface GridFSBucket {
219276
* @param options the download options
220277
* @return the stream
221278
*/
222-
GridFSDownloadStream openDownloadStreamByName(String filename, GridFSDownloadByNameOptions options);
279+
GridFSDownloadStream openDownloadStream(String filename, GridFSDownloadOptions options);
223280

224281
/**
225282
* Downloads the contents of the latest version of the stored file specified by {@code filename} and writes the contents to
@@ -229,7 +286,7 @@ public interface GridFSBucket {
229286
* @param destination the destination stream
230287
* @param callback the callback that is completed once the file has been downloaded
231288
*/
232-
void downloadToStreamByName(String filename, AsyncOutputStream destination, SingleResultCallback<Long> callback);
289+
void downloadToStream(String filename, AsyncOutputStream destination, SingleResultCallback<Long> callback);
233290

234291
/**
235292
* Downloads the contents of the stored file specified by {@code filename} and by the revision in {@code options} and writes the
@@ -240,8 +297,8 @@ public interface GridFSBucket {
240297
* @param options the download options
241298
* @param callback the callback that is completed once the file has been downloaded
242299
*/
243-
void downloadToStreamByName(String filename, AsyncOutputStream destination, GridFSDownloadByNameOptions options,
244-
SingleResultCallback<Long> callback);
300+
void downloadToStream(String filename, AsyncOutputStream destination, GridFSDownloadOptions options,
301+
SingleResultCallback<Long> callback);
245302

246303
/**
247304
* Finds all documents in the files collection.
@@ -275,6 +332,14 @@ void downloadToStreamByName(String filename, AsyncOutputStream destination, Grid
275332
*/
276333
void delete(ObjectId id, SingleResultCallback<Void> callback);
277334

335+
/**
336+
* Given a {@code id}, delete this stored file's files collection document and associated chunks from a GridFS bucket.
337+
*
338+
* @param id the ObjectId of the file to be deleted
339+
* @param callback the callback that is completed once the file has been deleted
340+
*/
341+
void delete(BsonValue id, SingleResultCallback<Void> callback);
342+
278343
/**
279344
* Renames the stored file with the specified {@code id}.
280345
*
@@ -284,6 +349,15 @@ void downloadToStreamByName(String filename, AsyncOutputStream destination, Grid
284349
*/
285350
void rename(ObjectId id, String newFilename, SingleResultCallback<Void> callback);
286351

352+
/**
353+
* Renames the stored file with the specified {@code id}.
354+
*
355+
* @param id the id of the file in the files collection to rename
356+
* @param newFilename the new filename for the file
357+
* @param callback the callback that is completed once the file has been renamed
358+
*/
359+
void rename(BsonValue id, String newFilename, SingleResultCallback<Void> callback);
360+
287361
/**
288362
* Drops the data associated with this bucket from the database.
289363
*

0 commit comments

Comments
 (0)