-
Notifications
You must be signed in to change notification settings - Fork 2
/
quick-start-read.js
executable file
·60 lines (49 loc) · 1.88 KB
/
quick-start-read.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
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
quickStartRead(data);
async function quickStartRead(writeData) {
console.log(line);
console.log('Quick Start Read Example');
console.log(line);
console.log('We need a blob file to read.');
const blobPath = await blobStore.write(writeData);
console.log('Following is the blobPath for the file we will read:');
console.log(blobPath);
// blobPath is not a full file system path
// The full path will be the blobStoreRoot + blobPath
// blobPath: /01CTZBM6BA090XZNRCWBXFA25R/01CTZBM6BBXXTNWM7YZBMFBC3W/01CTZBM6BBD3F8KZQ14Q79ZNKM/01CTZBM6BCEMJ4FXVZKHQ3FAJZ/01CTZBM6BD74MC5HVQ39PFGP0J
console.log('Here is the full path for the file:');
console.log(path.join(options.blobStoreRoot, blobPath));
// Reading Example
console.log('Lets read the file using blobStore.createReadStream.');
console.log('Following this log line we should see the data:');
const readStream = await blobStore.createReadStream(blobPath);
readStream.on('error', (err) => {
console.error(err);
});
// Pipe the file to the console.
readStream.pipe(process.stdout);
// A small delay...
await new Promise((resolve) => {
setTimeout(resolve, 400);
});
console.log(); // Adding a new line to the console
// We can also use the blobStore.read method
const content = await blobStore.read(blobPath);
console.log('Following is the data from the blobStore.read method:');
console.log(content);
console.log(line);
}