-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquick-start-write.js
executable file
·81 lines (67 loc) · 3 KB
/
quick-start-write.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const os = require('os');
const path = require('path');
const ulid = require('ulid').ulid;
const BlobStore = require('scalable-blob-store');
const data = 'The quick brown fox jumps over the lazy dog.';
const line = '='.repeat(80);
const options = {
blobStoreRoot: os.tmpdir() + '/blobs', // TODO: Change this!
idFunction: ulid,
dirDepth: 3,
dirWidth: 1000,
};
// Creating the blobStore Object
const blobStore = new BlobStore(options);
// Calling the quick start async function
quickStartWrite(data);
async function quickStartWrite(writeData) {
console.log(line);
console.log('Quick Start Write Example');
console.log(line);
console.log('Saving data using blobStore.write:');
const firstBlobPath = await blobStore.write(writeData);
console.log('First file is located in the following blobPath:');
console.log(firstBlobPath);
console.log('First file full path is:');
console.log(path.join(options.blobStoreRoot, firstBlobPath));
// firstBlobPath is not a full file system path
// The full path will be the blobStoreRoot + blobPath
// firstBlobPath: /01CTZBM6BA090XZNRCWBXFA25R/01CTZBM6BBXXTNWM7YZBMFBC3W/01CTZBM6BBD3F8KZQ14Q79ZNKM/01CTZBM6BCEMJ4FXVZKHQ3FAJZ/01CTZBM6BD74MC5HVQ39PFGP0J
// Opening a readStream from the file system for an example to write with
const fs = require('fs');
const fullFilePath = path.join(options.blobStoreRoot, firstBlobPath);
const fsReadStream = await fs.createReadStream(fullFilePath);
// Writing data using writeStream
const result = await blobStore.createWriteStream();
console.log('Following is the object returned from blobStore.createWriteStream:');
console.dir(result, { depth: 0 });
// Logs the result object which contains the blobPath and writeStream.
// Use the writeStream to save your blob.
// Store the blobPath in your database.
//
// result object will be similar to this:
// {
// blobPath: "/01CTZBM6BA090XZNRCWBXFA25R/01CTZBM6BBXXTNWM7YZBMFBC3W/01CTZBM6BBD3F8KZQ14Q79ZNKM/01CTZBM6BCEMJ4FXVZKHQ3FAJZ/01CTZBM6BM53X3XBW7TJR2RBFQ",
// writeStream: [WriteStream]
// }
await new Promise((resolve, reject) => {
result.writeStream.on('finish', resolve);
result.writeStream.on('error', reject);
fsReadStream.pipe(result.writeStream);
});
console.log('As you can see it contains the writeStream and the blobPath.');
console.log('Save the blobPath for the file into your database:');
console.log(result.blobPath);
console.log('We can append data to an existing blob using blobStore.append:');
await blobStore.append(result.blobPath, ' - New content...');
const content = await blobStore.read(result.blobPath);
console.log(content);
console.log('We can copy a blob to a new blobPath:');
const newCopyBlobPath = await blobStore.copy(result.blobPath);
console.log('Here is the blobPath for the new copy:');
console.log(newCopyBlobPath);
console.log('And here is the content of the new file:');
const newCopyContent = await blobStore.read(newCopyBlobPath);
console.log(newCopyContent);
console.log(line);
}