Toko gumpalan sistem file yang dirancang untuk mencegah konflik saat digunakan dengan sistem file terdistribusi atau jaringan area penyimpanan.
Silakan membintangi GitHub / NPM dan menonton pembaruan.
Catatan: Membutuhkan Node.js v12 atau lebih baru.
npm install scalable-blob-store --save
const os = require ( 'os' ) ;
const ulid = require ( 'ulid' ) . ulid ; // You need a unique ID generator function
const BlobStore = require ( 'scalable-blob-store' ) ;
const options = {
blobStoreRoot : os . tmpdir ( ) + '/blobs' , // Change this!
idFunction : ulid ,
dirDepth : 4 ,
dirWidth : 1000 ,
} ;
// Creating the blobStore Object
const blobStore = new BlobStore ( options ) ;
const result = await blobStore . createWriteStream ( ) ;
console . dir ( result ) ;
// 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: "/01CTZRTWMAD153V20K26S4Y0BW/01CTZRTWMBZW4SPR4E5QGGJYSH/01CTZRTWMB3QXZK04SYFY8ZJVR/01CTZS3KJYFPRQ34S3T15Y798S",
// writeStream: [WriteStream]
// }
//
// In this example the full file path for the blob would be something like this:
// /tmp/blobs/01CTZRTWMAD153V20K26S4Y0BW/01CTZRTWMBZW4SPR4E5QGGJYSH/01CTZRTWMB3QXZK04SYFY8ZJVR/01CTZS3KJYFPRQ34S3T15Y798S
//
// This is based on the blobStoreRoot + blobPath.Lihat file contoh start cepat untuk lebih detail:
Setelah meneliti penyimpanan file pengguna, atau penyimpanan gumpalan, untuk aplikasi web yang saya kerjakan, saya menemukan solusi paling umum yang digunakan oleh pengembang web adalah menyimpan file menggunakan penyedia layanan cloud. Setelah membuat akun dengan penyedia seperti Amazon S3, Google Cloud Storage, atau Azure Storage, mereka hanya menyimpan semua file aplikasi dan gumpalan mereka di sana.
Saya meneliti harga penyimpanan cloud dan memutuskan saya menginginkan versi lokal gratis yang akan skala jika diperlukan.
Saya melihat sejumlah solusi yang ada seperti filestorage tetapi tidak senang dengan skalabilitas solusi ini. Sebagian besar hanya dirancang untuk satu server dan akan menyebabkan konflik menulis jika sistem file terdistribusi, sistem file cluster seperti GLUSTERFS, atau jaringan area penyimpanan digunakan sebagai sistem file backend.
Dalam perjalanan mobil yang panjang, saya memikirkan solusi untuk penyimpanan gumpalan saya dan menghasilkan scalable-blob-store .
Untuk mencapai skalabilitas pada sistem file yang didistribusikan atau direplikasi, scalable-blob-store tidak menggunakan file indeks atau database lain untuk mengelola file pada disk atau sistem penyimpanan. Sebagai gantinya, sistem file itu sendiri digunakan untuk menemukan jalur penyimpanan terbaru berdasarkan atribut birthtime sistem file (tanggal pembuatan direktori).
Setelah jalur terbaru ditentukan, jumlah file dalam direktori dihitung untuk memastikan tetap di bawah nilai yang dikonfigurasi. Ini untuk mencegah masalah kinerja disk ketika sejumlah besar file disimpan dalam satu direktori. Jika jumlah item dalam direktori menjadi terlalu besar, jalur penyimpanan baru ditentukan.
Karena tidak ada database yang digunakan untuk mengelola file di jalur root, terserah Anda untuk mempertahankan nilai blobPath yang dikembalikan dan metadata tentang file yang disimpan dalam database Anda sendiri.
Alasan scalable-blob-store dapat diskalakan adalah karena penamaan direktori dan file dalam sistem file Anda. Setiap direktori dan file yang disimpan ke disk dinamai oleh ID unik yang dihasilkan berdasarkan fungsi yang ditentukan pengguna. Anda dapat menggunakan generator ID unik seperti ULID, CUID, UUID V4, atau MongoDBS ObjectIDS hanya untuk beberapa nama. Lihat repositori ID unik saya yang luar biasa untuk lebih banyak contoh. Menggabungkan direktori antara server atau disk tidak boleh menyebabkan tabrakan nama file.
Jika sistem file yang direplikasi atau cluster digunakan, satu -satunya konflik yang dapat terjadi adalah ketika satu server membaca file sementara yang lain menghapus file yang sama. scalable-blob-store tidak mencoba mengelola konflik ini, namun itu akan meningkatkan pengecualian.
Di bawah ini adalah contoh-contoh struktur direktori yang dibuat oleh scalable-blob-store .
Contoh dengan Direktori CUID dan nama file:
b lobs c ij50xia200pzzph3we9r62bi // ← Directory File ↓
b lobs c ij50xia300q1zph3m4df4ypz . . c ij50xiae00qgzph3i0ms0l2wContoh dengan Direktori UUID dan Nama File:
b lobs 8 46a291f-9864-40bb-aefe-f29bdc73a761 // ← Directory File ↓
b lobs 8 46a291f-9864-40bb-aefe-f29bdc73a761 . . 8 b86b6fe-6166-424c-aed9-8faf1e62689e scalable-blob-store mendukung opsi konfigurasi untuk memberi Anda kontrol atas direktori dan file ID yang digunakan, kedalaman struktur direktori, dan lebar direktori. Opsi default memberikan 3 direktori yang berisi 1000 item yang memberikan total penyimpanan satu miliar file dalam struktur direktori.
Poin Operasional Lainnya:
dirWidth , direktori berikutnya dibuat.dirWidth , direktori induk berikutnya dibuat.dirWidth , nilai dirWidth diabaikan. Menulis
Di laptop saya dengan disk M.2 SSD, menjalankan skrip test-fs.js menghasilkan hasil berikut:
====================================================================================================
Testing scalable-blob-store with the following options:
blobStoreRoot: /tmp/blobs/test-fs
idFunction: ulid
dirDepth: 3
dirWidth: 1000
repeat: 10000
Beginning test...
====================================================================================================
Test complete.
====================================================================================================
{
blobStoreRoot: '/tmp/blobs/test-fs',
dirDepth: 3,
dirWidth: 1000,
runTimeMilliseconds: 83730,
totalDirectories: 12,
totalFiles: 10000,
totalBytes: 430000,
lastBlobPath: '/ckxwcwgwz0001lk9hgq8t9iup/ckxwcwgx00002lk9h6tbpdmq1/ckxwcy36m06yclk9hb0g92dwg/ckxwcy9ip07q4lk9h5uyl10k6'
}
====================================================================================================
Please remove /tmp/blobs/test-fs manually.
====================================================================================================
Membaca
Baca kinerja akan dekat dengan, jika tidak sama, seperti kecepatan disk.
Semua metode blobstore dalam scalable-blob-store mengembalikan janji. Ini sempurna untuk digunakan dengan fitur bahasa Async/Await.
| API | Jenis | Kembali |
|---|---|---|
| Blobstore baru (opsi) | Konstruktor | Instance Blobstore |
| Blobstore.blobstoreroot | Baca hanya properti | String |
| Blobstore.idfunction | Baca hanya properti | Function |
| Blobstore.dirdepth | Baca hanya properti | Number |
| Blobstore.dirwidth | Baca hanya properti | Number |
| Blobstore.getCurrentBlobdir () | Metode | Promise<String> |
| Blobstore.setCurrentBlobdir (Blobdir) | Metode | Promise<undefined> |
| Blobstore.createWriteStream () | Metode | Promise<Object> |
| Blobstore.write (data, writeoptions) | Metode | Promise<String> |
| Blobstore.Append (Blobpath, Data, AppendOptions) | Metode | Promise<undefined> |
| Blobstore.Copy (Blobpath, Flags) | Metode | Promise<String> |
| Blobstore.createreadstream (Blobpath) | Metode | Promise<ReadStream> |
| Blobstore.read (Blobpath, ReadOptions) | Metode | Promise<data> |
| Blobstore.open (blobpath, bendera, mode) | Metode | Promise<FileHandle> |
| Blobstore.realpath (Blobpath, Realpathoptions) | Metode | Promise<String> |
| Blobstore.stat (Blobpath) | Metode | Promise<Stats> |
| Blobstore.exists (Blobpath) | Metode | Promise<Boolean> |
| Blobstore.remove (Blobpath) | Metode | Promise<undefined> |
new BlobStore(options)Jenis: Fungsi Konstruktor.
Parameter: options sebagai Object .
Pengembalian : Objek BlobStore baru yang akan digunakan untuk menyimpan data.
Keterangan:
Anda dapat menghubungi new BlobStore(options) beberapa kali untuk membuat lebih dari satu toko gumpalan.
Opsi diteruskan ke fungsi konstruktor sebagai object JavaScript.
| Kunci | Keterangan | Default |
|---|---|---|
blobStoreRoot | Root Directory untuk menyimpan gumpalan | Diperlukan |
idFunction | Fungsi ID apa pun yang mengembalikan string ID yang unik | Diperlukan |
dirDepth | Seberapa dalam Anda menginginkan direktori di bawah akar | 3 |
dirWidth | Jumlah maksimum file atau direktori dalam direktori | 1000 |
Contoh:
// Start by requiring the `scalable-blob-store` constructor function:
const BlobStore = require ( 'scalable-blob-store' ) ;
// You will need a unique ID function
const uuid = require ( 'uuid' ) ;
// Create the options object
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 4 ,
dirWidth : 2000 ,
} ;
// Create a blob store using the options `object`:
const blobStore = new BlobStore ( options ) ;Membuat beberapa toko gumpalan:
const userOptions = {
blobStoreRoot : '/app/blobs/user' ,
idFunction : uuid . v4 ,
dirDepth : 4 ,
dirWidth : 2000 ,
} ;
const pdfOptions = {
blobStoreRoot : '/app/blobs/pdf' ,
idFunction : uuid . v4 ,
dirDepth : 2 ,
dirWidth : 300 ,
} ;
const userFileStore = new BlobStore ( userOptions ) ;
const pdfDocumentStore = new BlobStore ( pdfOptions ) ;blobStoreRootJenis: Baca hanya properti.
Pengembalian: String yang cocok dengan options.blobStoreRoot Anda.
Keterangan:
Ini adalah properti kenyamanan untuk memungkinkan Anda meneruskan objek Blobstore ke sub modul dan masih memiliki akses ke properti yang dikonfigurasi.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 4 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
console . log ( blobStore . blobStoreRoot ) ;
// Outputs '/app/blobs' which you configured in the optionsidFunctionJenis: Baca hanya properti.
Pengembalian: Fungsi ID unik yang Anda konfigurasi dalam nilai options.idFunction .
Keterangan:
Ini adalah properti kenyamanan untuk memungkinkan Anda meneruskan objek Blobstore ke sub modul dan masih memiliki akses ke properti yang dikonfigurasi.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 4 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
console . log ( blobStore . idFunction ( ) ) ;
// Outputs 'bac00ab2-5e6d-4b77-bfa4-e9befc3e4279' which is a generated UUID from the idFunction.dirDepthJenis: Baca hanya properti.
Pengembalian: Number yang cocok dengan options.dirDepth Anda. Nilai Dirdepth.
Keterangan:
Ini adalah properti kenyamanan untuk memungkinkan Anda meneruskan objek Blobstore ke sub modul dan masih memiliki akses ke properti yang dikonfigurasi.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 4 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
console . log ( blobStore . dirDepth ) ;
// Outputs '4' which you configured in the optionsdirWidthJenis: Baca hanya properti.
Pengembalian: Number yang cocok dengan options.dirWidth Anda. Nilai Dirwidth.
Keterangan:
Ini adalah properti kenyamanan untuk memungkinkan Anda meneruskan objek Blobstore ke sub modul dan masih memiliki akses ke properti yang dikonfigurasi.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 4 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
console . log ( blobStore . dirWidth ) ;
// Outputs '2000' which you configured in the optionsgetCurrentBlobDir()Jenis: Metode.
Pengembalian: Promise yang diselesaikan menjadi String yang merupakan direktori pembuatan gumpalan aktif saat ini.
Keterangan:
Fungsi ini digunakan secara internal oleh BlobStore untuk menentukan direktori di mana file gumpalan berikutnya akan disimpan ke disk.
Jika Anda perlu menyimpan file gumpalan di luar BlobStore , Anda dapat menggunakan metode ini untuk menemukan tempat yang tepat untuk meletakkan file Anda.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 3 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
async function main ( ) {
try {
console . log ( await blobStore . getCurrentBlobDir ( ) ) ;
// The 'dirDepth' option above is set to 3 so the output will be similar to the following:
// '/e44d3b0d-b552-4257-8b64-a53331184c38/443061b9-bfa7-40fc-a5a9-d848bc52155e/4d818f4c-88b3-45fd-a104-a2fc3700e9de'
} catch ( err ) {
console . error ( err ) ;
}
}
main ( ) ;setCurrentBlobDir(blobDir)Jenis: Metode.
Parameter: blobDir sebagai String .
blobStoreRoot . Pengembalian: Promise yang diselesaikan untuk undefined .
Keterangan:
Fungsi ini dapat digunakan untuk memandu BlobStore untuk menyimpan file gumpalan baru ke dalam blobPath yang diinginkan.
Salah satu masalah dengan scalable-blob-store adalah bahwa jika Anda menghapus banyak file gumpalan, direktori file tidak akan dihapus. Anda dapat menghapus direktori sendiri, atau mengisi kembali mereka dengan file gumpalan baru dengan mengatur direktori gumpalan aktif saat ini.
Fungsi ini ditambahkan untuk memungkinkan konsumen dari modul ini untuk mengatasi direktori gumpalan kosong.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 3 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
async function main ( ) {
try {
console . log ( await blobStore . getCurrentBlobDir ( ) ) ;
// The 'dirDepth' option above is set to 3 so the output will be similar to the following:
// '/e44d3b0d-b552-4257-8b64-a53331184c38/443061b9-bfa7-40fc-a5a9-d848bc52155e/4d818f4c-88b3-45fd-a104-a2fc3700e9de'
await blobStore . setCurrentBlobDir ( '/some/blob/path' ) ;
console . log ( await blobStore . getCurrentBlobDir ( ) ) ;
// Outputs '/some/blob/path' to the console.
// Any new blob files added to the blob store will go into this path until there are `dirWidth` or 2000 files within it.
} catch ( err ) {
console . error ( err ) ;
}
}
main ( ) ;createWriteStream()Jenis: Metode.
Pengembalian : Promise yang diselesaikan ke Object yang berisi jalur anak ke file dalam akar gumpalan dan writestream.
Keterangan:
Berikut ini adalah eksampe objek yang dikembalikan menggunakan UUID sebagai IDFunction:
{
blobPath : "/e6b7815a-c818-465d-8511-5a53c8276b86/aea4be6a-9e7f-4511-b394-049e68f59b02/fea722d1-001a-4765-8408-eb8e0fe7dbc6/183a6b7b-2fd6-4f80-8c6a-2647beb7bb19" ,
writeStream : stream . Writable
} Gunakan writeStream untuk menyimpan gumpalan atau file Anda. blobPath perlu disimpan ke database Anda untuk akses di masa mendatang.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 3 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
// The below readStream is simply to make this a complete example
const fs = require ( 'fs' ) ;
const readStream = fs . createReadStream ( '/path/to/file' ) ;
async function main ( ) {
let result ;
try {
result = await blobStore . createWriteStream ( ) ;
} catch ( err ) {
console . error ( err ) ;
}
console . dir ( result ) ;
// result object will be similar to this:
// {
// blobPath: "/e6b7815a-c818-465d-8511-5a53c8276b86/aea4be6a-9e7f-4511-b394-049e68f59b02/fea722d1-001a-4765-8408-eb8e0fe7dbc6/183a6b7b-2fd6-4f80-8c6a-2647beb7bb19",
// writeStream: [WriteStream]
// }
// Using a Promise to encapsulate the write asynchronous events.
await new Promise ( ( resolve , reject ) => {
result . writeStream . on ( 'finish' , ( ) => {
resolve ( ) ;
} ) ;
result . writeStream . on ( 'error' , reject ) ;
readStream . pipe ( result . writeStream ) ;
} ) ;
console . log ( blobPath ) ;
// Logs the blobPath. Save this in your database.
}
main ( ) ;write(data, writeOptions)Jenis: Metode.
Parameter: data sebagai String , Buffer , TypedArray , atau DataView .
Parameter: writeOptions sebagai Object .
writeOptions mendukung properti encoding, mode, dan bendera. Pengembalian: Promise yang diselesaikan menjadi String .
blobPath yang perlu dilakukan untuk database Anda.Keterangan:
Jika Anda memiliki data sederhana dalam memori daripada aliran data, Anda dapat menggunakan metode ini untuk menyimpan data ke dalam file gumpalan.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 3 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
async function main ( ) {
const data = 'The quick brown fox jumps over the lazy dog.' ;
try {
const blobPath = await blobStore . write ( data ) ;
// The returned blobPath will look something like this:
// '/e44d3b0d-b552-4257-8b64-a53331184c38/443061b9-bfa7-40fc-a5a9-d848bc52155e/4d818f4c-88b3-45fd-a104-a2fc3700e9de'
// Save it to your database.
} catch ( err ) {
console . error ( err ) ;
}
}
main ( ) ;append(blobPath, data, appendOptions)Jenis: Metode.
Parameter: blobPath sebagai String .
blobPath dari database aplikasi Anda. Parameter: data sebagai String atau Buffer .
Parameter: appendOptions sebagai Object .
appendOptions mendukung properti encoding, mode, dan bendera. Pengembalian: Promise yang diselesaikan menjadi undefined .
Keterangan:
Gunakan metode ini untuk menambahkan data memori yang sederhana ke akhir file Blob.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 3 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
async function main ( ) {
const data = 'The quick brown fox jumps over the lazy dog.' ;
try {
await blobStore . append ( data ) ;
} catch ( err ) {
console . error ( err ) ;
}
}
main ( ) ;copy(blobPath, flags)Jenis: Metode.
Parameter: blobPath sebagai String .
blobPath dari database aplikasi Anda. Parameter: flags sebagai Number .
Pengembalian: Promise yang diselesaikan menjadi String .
blobPath baru untuk file Blob yang disalin.Keterangan:
Gunakan metode ini untuk membuat salinan file gumpalan yang ada.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 3 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
async function main ( ) {
try {
const blobPathSource =
'/e6b7815a-c818-465d-8511-5a53c8276b86/aea4be6a-9e7f-4511-b394-049e68f59b02/fea722d1-001a-4765-8408-eb8e0fe7dbc6/183a6b7b-2fd6-4f80-8c6a-2647beb7bb19' ;
const blobPathDest = await blobStore . copy ( blobPathSource ) ;
// Store your new blobPath into your application database
} catch ( err ) {
console . error ( err ) ;
}
}
main ( ) ;createReadStream(blobPath)Jenis: Metode.
Parameter: blobPath sebagai String .
blobPath dari database aplikasi Anda. Pengembalian : Promise yang diselesaikan menjadi ReadStream .
Keterangan:
Membuat aliran yang dapat dibaca ke file gumpalan yang terletak di blobPath .
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 3 ,
dirWidth : 2000 ,
} ;
async function main ( ) {
// Get the blobPath value from your database.
const blobPath =
'/e6b7815a-c818-465d-8511-5a53c8276b86/aea4be6a-9e7f-4511-b394-049e68f59b02/fea722d1-001a-4765-8408-eb8e0fe7dbc6/183a6b7b-2fd6-4f80-8c6a-2647beb7bb19h' ;
let readStream ;
try {
readStream = await blobStore . createReadStream ( blobPath ) ;
} catch ( err ) {
console . error ( err ) ;
}
readStream . on ( 'error' , ( err ) => {
console . error ( err ) ;
} ) ;
// Blob contents is piped to the console.
readStream . pipe ( process . stdout ) ;
}
main ( ) ;read(blobPath, readOptions)Jenis: Metode.
Parameter: blobPath sebagai String .
blobPath dari database aplikasi Anda. Parameter: readOptions sebagai Object .
Pengembalian: Promise yang diselesaikan menjadi isi file gumpalan.
scalable-blob-store menetapkan nilai readOptions.encoding ke 'UTF8' secara default.Keterangan:
Gunakan metode ini untuk membaca konten file gumpalan kecil ke dalam memori.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 3 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
async function main ( ) {
try {
// Retrieve the blobPath value from your database
const blobPath =
'/e6b7815a-c818-465d-8511-5a53c8276b86/aea4be6a-9e7f-4511-b394-049e68f59b02/fea722d1-001a-4765-8408-eb8e0fe7dbc6/183a6b7b-2fd6-4f80-8c6a-2647beb7bb19' ;
const content = await blobStore . read ( blobPath ) ;
// Do something with the content
} catch ( err ) {
console . error ( err ) ;
}
}
main ( ) ;open(blobPath, flags, mode)Jenis: Metode.
Parameter: blobPath sebagai String .
blobPath dari database aplikasi Anda. Parameter: flags sebagai String atau Number .
Pengembalian: Promise yang diselesaikan menjadi objek FileHandle.
Keterangan:
Ini adalah metode yang lebih canggih yang memungkinkan Anda melakukan banyak operasi file terhadap file Blob.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 3 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
async function main ( ) {
try {
// Retrieve the blobPath value from your database
const blobPath =
'/e6b7815a-c818-465d-8511-5a53c8276b86/aea4be6a-9e7f-4511-b394-049e68f59b02/fea722d1-001a-4765-8408-eb8e0fe7dbc6/183a6b7b-2fd6-4f80-8c6a-2647beb7bb19' ;
const fileHandle = await blobStore . open ( blobPath ) ;
// Do something with the file handle object
// See the documentation for more detail
// The documentation link is in the description above
} catch ( err ) {
console . error ( err ) ;
}
}
main ( ) ;realPath(blobPath, realPathOptions)Jenis: Metode.
Parameter: blobPath sebagai String .
blobPath dari database aplikasi Anda. Parameter: realPathOptions sebagai String atau Object .
Pengembalian: Promise yang diselesaikan menjadi String .
Keterangan:
Gunakan metode ini untuk menemukan file gumpalan pada sistem file. Metode ini seharusnya tidak benar -benar dibutuhkan karena Anda dapat menentukan jalur file gumpalan penuh. Cukup kembangkan nilai Blobstoreroot dan Blobpath.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 3 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
async function main ( ) {
try {
// Retrieve the blobPath value from your database
const blobPath =
'/e6b7815a-c818-465d-8511-5a53c8276b86/aea4be6a-9e7f-4511-b394-049e68f59b02/fea722d1-001a-4765-8408-eb8e0fe7dbc6/183a6b7b-2fd6-4f80-8c6a-2647beb7bb19' ;
const fsPath = await blobStore . realPath ( blobPath ) ;
// With the above options the result will be similar to this:
// '/app/blobs/e6b7815a-c818-465d-8511-5a53c8276b86/aea4be6a-9e7f-4511-b394-049e68f59b02/fea722d1-001a-4765-8408-eb8e0fe7dbc6/183a6b7b-2fd6-4f80-8c6a-2647beb7bb19
} catch ( err ) {
console . error ( err ) ;
}
}
main ( ) ;stat(blobPath)Jenis: Metode.
Parameter: blobPath sebagai String .
Pengembalian: Object Statistik.
Keterangan:
Alih-alih menguraikan objek stats sistem file, scalable-blob-store mengembalikan objek stats mentah.
Rincian kelas stat lebih banyak dapat ditemukan di Wikipedia.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 3 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
async function main ( ) {
try {
// Retrieve the blobPath value from your database
const blobPath =
'/e6b7815a-c818-465d-8511-5a53c8276b86/aea4be6a-9e7f-4511-b394-049e68f59b02/fea722d1-001a-4765-8408-eb8e0fe7dbc6/183a6b7b-2fd6-4f80-8c6a-2647beb7bb19' ;
const stats = await blobStore . stat ( blobPath ) ;
console . dir ( stats ) ;
// Console output will be similar to the following.
// { dev: 2050,
// mode: 33188,
// nlink: 1,
// uid: 1000,
// gid: 1000,
// rdev: 0,
// blksize: 4096,
// ino: 6707277,
// size: 44,
// blocks: 8,
// atime: Mon Oct 12 2015 08:51:29 GMT+1000 (AEST),
// mtime: Mon Oct 12 2015 08:51:29 GMT+1000 (AEST),
// ctime: Mon Oct 12 2015 08:51:29 GMT+1000 (AEST),
// birthtime: Mon Oct 12 2015 08:51:29 GMT+1000 (AEST) }
} catch ( err ) {
console . error ( err ) ;
}
}
main ( ) ;exists(blobPath)Jenis: Metode.
Parameter: blobPath sebagai String .
Pengembalian: Boolean
true jika file ada, jika tidak false .Keterangan:
Gunakan metode ini untuk tes keberadaan file gumpalan sederhana.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 3 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
async function main ( ) {
try {
// Retrieve the blobPath value from your database
const blobPath =
'/e6b7815a-c818-465d-8511-5a53c8276b86/aea4be6a-9e7f-4511-b394-049e68f59b02/fea722d1-001a-4765-8408-eb8e0fe7dbc6/183a6b7b-2fd6-4f80-8c6a-2647beb7bb19' ;
const exists = await blobStore . exists ( blobPath ) ;
// The result will be either true or false depending if the blob file exists.
} catch ( err ) {
console . error ( err ) ;
}
}
main ( ) ;remove(blobPath)Jenis: Metode.
Parameter: blobPath sebagai String .
Pengembalian : undefined jika tidak ada yang salah atau file tidak ada.
Keterangan:
Gunakan metode ini untuk menghapus file gumpalan. Metode ini tidak dapat digunakan untuk menghapus direktori.
Contoh:
const BlobStore = require ( 'scalable-blob-store' ) ;
const uuid = require ( 'uuid' ) ;
const options = {
blobStoreRoot : '/app/blobs' ,
idFunction : uuid . v4 ,
dirDepth : 3 ,
dirWidth : 2000 ,
} ;
const blobStore = new BlobStore ( options ) ;
async function main ( ) {
try {
// Retrieve the blobPath value from your database
const blobPath =
'/e6b7815a-c818-465d-8511-5a53c8276b86/aea4be6a-9e7f-4511-b394-049e68f59b02/fea722d1-001a-4765-8408-eb8e0fe7dbc6/183a6b7b-2fd6-4f80-8c6a-2647beb7bb19' ;
await blobStore . remove ( blobPath ) ;
// The blob file will no longer exist
} catch ( err ) {
console . error ( err ) ;
}
}
main ( ) ; Ada masalah kecil di scalable-blob-store . Jika ada sejumlah besar file gumpalan yang ditambahkan dan kemudian dihapus dari toko gumpalan, Anda mungkin memiliki direktori atau direktori kosong dengan sejumlah kecil file di dalamnya. Direktori ini tidak akan pernah dihapus dan tidak akan diisi.
Jika Anda ingin mencegah direktori kosong atau berpenduduk jarang, Anda perlu menjalankan tugas pemeliharaan terhadap direktori blobStoreRoot . Tugas pemeliharaan ini perlu mencari direktori kosong atau tidak lengkap dan memanggil metode SetCurrentBlobdir yang lewat di blobPath kosong.
Untuk aplikasi Anda, Anda mungkin menemukan Anda jarang menghapus sejumlah besar file gumpalan. Jika ini masalahnya maka masalah ini dapat diabaikan.
Ada dua metode untuk menguji scalable-blob-store :
os.tmpdir() lokal. Setelah mengkloning scalable-blob-store , ketik hal-hal berikut ke dalam konsol Anda:
npm install
npm test
Menjalankan file test-fs.js akan membuat direktori ~/blobs di direktori sementara Anda dan kemudian secara rekursif mengisinya dengan banyak gumpalan.
Opsi default yang dikonfigurasi dalam file test-fs.js adalah:
const opts = {
blobStoreRoot : os . tmpdir ( ) + '/blobs' ,
idFunction : cuid ,
dirDepth : 3 ,
dirWidth : 1000 ,
} ;
const repeat = 10000 ;Ubah opsi jika Anda ingin melihat hasil yang berbeda.
Setelah mengkloning scalable-blob-store , ketik hal-hal berikut ke dalam konsol Anda:
npm install
node ./tests/test-fs.js
Setelah selesai, periksa direktori /tmp/blobs . Saya sarankan menggunakan perintah pohon yang memberi Anda ringkasan direktori dan file dalam direktori target.
tree ~ /blobs
tree -d ~ /blobs
Saya, Grant Carthew, saya seorang teknolog dari Queensland, Australia. Saya mengerjakan kode di sejumlah proyek pribadi dan ketika dibutuhkan saya membangun paket saya sendiri.
Proyek ini ada karena saya membutuhkan toko gumpalan lokal yang dapat skala.
Semua yang saya lakukan di open source dilakukan pada waktu saya sendiri dan sebagai kontribusi untuk komunitas open source.
Jika Anda menggunakan proyek saya dan ingin mengucapkan terima kasih atau mendukung saya, silakan klik tautan Patreon di bawah ini.
Lihat proyek saya yang lain di NPM.
git checkout -b my-new-featuregit commit -am 'Add some feature'git push origin my-new-featurenode-uuid dengan uuid .ES5 . Persyaratan mesin NodeJS yang dihapus.return null .return null setelah panggilan resol/tolak untuk mencegah peringatan bluebird.es5dist untuk versi node yang lebih lama. Paket diperbarui.Mit