旨在防止與分佈式文件系統或存儲區域網絡一起使用的文件系統BLOB存儲。
請在github / npm上出演並注意更新。
注意:需要node.js v12或更高版本。
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.請參閱快速啟動示例文件以獲取更多詳細信息:
研究用戶文件存儲或BLOB存儲後,我正在使用的Web應用程序,我發現Web開發人員使用的最常見的解決方案是使用雲服務提供商存儲文件。在與Amazon S3,Google Cloud Storage或Azure存儲等提供商創建帳戶之後,他們只是將所有應用程序文件和斑點存放在那裡。
我研究了雲存儲的價格,並決定我想要一個免費的本地版本,該版本在需要時可以擴展。
我查看了許多現有解決方案,例如filestorage,但對這些解決方案的可擴展性不滿意。大多數僅為單個服務器設計,如果將分佈式文件系統,諸如GlusterFS之類的群集文件系統或存儲區域網絡用作後端文件系統時,會導致寫衝突。
在一次漫長的汽車旅行中,我正在考慮用於斑點存儲的解決方案,並提出了scalable-blob-store 。
為了在分佈式或複制的文件系統上實現可伸縮性, scalable-blob-store不使用索引文件或其他數據庫來管理磁盤或存儲系統上的文件。相反,文件系統本身用於根據文件系統birthtime屬性(目錄創建日期)找到最新的存儲路徑。
一旦確定了最新路徑,就會計數目錄中的文件數,以確保其保持在配置的值之下。這是為了防止磁盤性能問題,當將大量文件存儲在單個目錄中時。如果目錄中的項目數太大,則確定新的存儲路徑。
由於沒有用於管理根路徑中文件的數據庫,因此您要維護返回的blobPath值和有關您自己數據庫中存儲的文件的元數據。
scalable-blob-store原因是可擴展的,這是由於文件系統中目錄和文件的命名。每個保存到磁盤的目錄和文件都由基於用戶定義的Funciton生成的唯一ID命名。您可以使用任何唯一的ID生成器,例如ULID,CUID,UUID V4或Mongodbs Objectids僅舉幾例。查看我很棒的獨特ID存儲庫,以獲取更多示例。在服務器或磁盤之間合併目錄絕不應導致文件名碰撞。
如果正在使用複製或群集文件系統,可能發生的唯一沖突是當一個服務器正在讀取文件時,另一個服務器正在刪除同一文件時。 scalable-blob-store不會試圖管理這場衝突,但是會引起例外。
以下是scalable-blob-store創建的目錄結構的示例。
使用CUID目錄和文件名稱示例:
b lobs c ij50xia200pzzph3we9r62bi // ← Directory File ↓
b lobs c ij50xia300q1zph3m4df4ypz . . c ij50xiae00qgzph3i0ms0l2w帶有UUID目錄和文件名的示例:
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支持配置選項,使您可以控制使用的目錄和文件ID,目錄結構的深度以及目錄的寬度。默認選項給出了3個目錄,其中包含1000個項目,在目錄結構內總存儲了十億個文件。
其他興趣點:
dirWidth值,就會創建下一個目錄。dirWidth值,就會創建下一個父目錄。dirWidth值,則忽略了dirWidth值。 寫
在帶有M.2 SSD磁盤的筆記本電腦上,運行test-fs.js腳本會產生以下結果:
====================================================================================================
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.
====================================================================================================
讀
閱讀性能將接近(即使不是相同),即磁盤速度。
scalable-blob-store內的所有BlobStore方法都返回承諾。這非常適合使用異步/等待語言功能。
| API | 類型 | 返回 |
|---|---|---|
| 新的Blobstore(選項) | 構造函數 | BlobStore實例 |
| blobstore.blobstoreroot | 僅閱讀屬性 | String |
| blobstore.idfunction | 僅閱讀屬性 | Function |
| blobstore.dirdepth | 僅閱讀屬性 | Number |
| blobstore.dirwidth | 僅閱讀屬性 | Number |
| blobstore.getCurrentBlobDir() | 方法 | Promise<String> |
| blobstore.setcurrentblobdir(blobdir) | 方法 | Promise<undefined> |
| blobstore.createwritestream() | 方法 | Promise<Object> |
| blobstore.write(數據,寫入) | 方法 | Promise<String> |
| blobstore.append(blobpath,數據,附錄) | 方法 | Promise<undefined> |
| blobstore.copy(BlobPath,標誌) | 方法 | Promise<String> |
| Blobstore.CreateReadstream(BlobPath) | 方法 | Promise<ReadStream> |
| blobstore.read(blobpath,readoptions) | 方法 | Promise<data> |
| blobstore.open(blobpath,標誌,模式) | 方法 | Promise<FileHandle> |
| Blobstore.RealPath(BlobPath,realPathoptions) | 方法 | Promise<String> |
| BlobStore.Stat(BlobPath) | 方法 | Promise<Stats> |
| blobstore.exists(BlobPath) | 方法 | Promise<Boolean> |
| blobstore.remove(blobpath) | 方法 | Promise<undefined> |
new BlobStore(options)類型:構造函數函數。
參數:作為Object options 。
返回:用於存儲數據的新的BlobStore對象。
描述:
您可以多次調用new BlobStore(options)來創建多個Blob Store。
選項作為JavaScript object傳遞給構造函數函數。
| 鑰匙 | 描述 | 預設值 |
|---|---|---|
blobStoreRoot | 根目錄到存儲斑點 | 必需的 |
idFunction | 任何返回唯一ID字符串的ID功能 | 必需的 |
dirDepth | 您希望目錄在根源下有多深 | 3 |
dirWidth | 目錄中的最大文件或目錄數 | 1000 |
例子:
// 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 ) ;創建多個Blob商店:
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 ) ;blobStoreRoot類型:僅閱讀屬性。
返回:與您的options.blobStoreRoot值匹配的String 。
描述:
這是一個便利屬性,可讓您將BlobStore對像傳遞到子模塊,並且仍然可以訪問配置的屬性。
例子:
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 optionsidFunction類型:僅閱讀屬性。
返回:您在options.idFunction值中配置的唯一ID函數。
描述:
這是一個便利屬性,可讓您將BlobStore對像傳遞到子模塊,並且仍然可以訪問配置的屬性。
例子:
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.dirDepth類型:僅閱讀屬性。
返回:一個與您的options.dirDepth匹配的Number 。
描述:
這是一個便利屬性,可讓您將BlobStore對像傳遞到子模塊,並且仍然可以訪問配置的屬性。
例子:
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 optionsdirWidth類型:僅閱讀屬性。
返回:與您的options.dirWidth值匹配的Number 。
描述:
這是一個便利屬性,可讓您將BlobStore對像傳遞到子模塊,並且仍然可以訪問配置的屬性。
例子:
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()類型:方法。
返回:解決當前活動BLOB創建目錄的String的Promise 。
描述:
BlobStore在內部使用此功能來確定將將下一個BLOB文件保存到磁盤的目錄。
如果您需要在BlobStore之外存儲一個Blob文件,則可以使用此方法來定位正確的位置來放置文件。
例子:
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)類型:方法。
參數: blobDir作為String 。
blobStoreRoot路徑下的文件系統目錄路徑。回報:決心undefined的Promise 。
描述:
該功能可用於指導BlobStore將新的Blob文件保存到所需的blobPath中。
scalable-blob-store的一個問題是,如果您刪除了許多BLOB文件,則該文件所在的目錄將不會被刪除。您可以自己刪除目錄,也可以通過設置當前的活動blob目錄來重新填充它們。
添加了此功能,以使該模塊的消費者能夠圍繞空白斑點目錄工作。
例子:
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()類型:方法。
返回:解決的Promise可以解決到包含Blob Store根部和Writestream中文件路徑的Object 。
描述:
這是使用uuid作為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
}使用writeStream保存斑點或文件。 blobPath需要保存到您的數據庫中,以便將來訪問。
例子:
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)類型:方法。
參數: data作為String , Buffer , TypedArray或DataView 。
參數: writeOptions作為Object 。
writeOptions對象支持編碼,模式和標誌屬性。返回:解決String的Promise 。
blobPath值。描述:
如果您在內存中有簡單的數據,而不是數據流,則可以使用此方法將數據存儲到BLOB文件中。
例子:
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)類型:方法。
參數: blobPath作為String 。
blobPath 。參數: data作為String或Buffer 。
參數: appendOptions作為Object 。
appendOptions對象支持編碼,模式和標誌屬性。回報:解決undefined的Promise 。
描述:
使用此方法將簡單的存儲器數據添加到BLOB文件的末尾。
例子:
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)類型:方法。
參數: blobPath作為String 。
blobPath 。參數: flags為Number 。
返回:解決String的Promise 。
blobPath值。描述:
使用此方法創建現有斑點文件的副本。
例子:
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)類型:方法。
參數: blobPath作為String 。
blobPath 。返回: Promise能夠解決ReadStream 。
描述:
為位於blobPath的Blob文件創建可讀的流。
例子:
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)類型:方法。
參數: blobPath作為String 。
blobPath 。參數: readOptions作為Object 。
返回:解決斑點文件內容的Promise 。
scalable-blob-store將readOptions.encoding值設置為'utf8'。描述:
使用此方法將小斑點文件的內容讀取到內存中。
例子:
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)類型:方法。
參數: blobPath作為String 。
blobPath 。參數: flags為String或Number 。
返回:解決fileHandle對象的Promise 。
描述:
這是一種更高級的方法,使您可以針對Blob文件執行許多文件操作。
例子:
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)類型:方法。
參數: blobPath作為String 。
blobPath 。參數: realPathOptions作為String或Object 。
返回:解決String的Promise 。
描述:
使用此方法在文件系統上找到斑點文件。不應該真正需要此方法,因為您可以確定完整的BLOB文件路徑。只需將BlobStoreroot和BlobPath值串聯。
例子:
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)類型:方法。
參數: blobPath作為String 。
返回:統計Object 。
描述:
scalable-blob-store而不是解析文件系統stats對象,而是返回原始stats對象。
可以在Wikipedia上找到更多STAT類細節。
例子:
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)類型:方法。
參數: blobPath作為String 。
回報: Boolean
true false 。描述:
使用此方法進行簡單的BLOB文件存在測試。
例子:
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)類型:方法。
參數: blobPath作為String 。
返回:如果沒有任何問題或文件不存在,則undefined 。
描述:
使用此方法刪除斑點文件。此方法不能用於刪除目錄。
例子:
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 ( ) ; scalable-blob-store中存在一個小問題。如果添加了大量BLOB文件,然後從Blob Store中刪除,則可能有空的目錄或目錄,其中包含少數文件。這些目錄將永遠不會被刪除,也不會被人填充。
如果您希望防止空填充或稀疏的目錄,則需要針對blobStoreRoot目錄運行維護任務。此維護任務將需要查找空的目錄或不完整的目錄,並調用SetCurrentBlobDir方法傳遞到空blobPath中。
對於您的應用程序,您可能會發現您很少刪除大量的BLOB文件。如果是這種情況,則可以忽略此問題。
有兩種測試scalable-blob-store方法:
os.tmpdir()目錄的單元測試。克隆scalable-blob-store後,將以下內容鍵入您的控制台:
npm install
npm test
運行test-fs.js文件將在您的臨時目錄中創建一個~/blobs目錄,然後遞歸地填充很多斑點。
在test-fs.js文件中配置的默認選項是:
const opts = {
blobStoreRoot : os . tmpdir ( ) + '/blobs' ,
idFunction : cuid ,
dirDepth : 3 ,
dirWidth : 1000 ,
} ;
const repeat = 10000 ;如果您想查看不同的結果,請更改選項。
克隆scalable-blob-store後,將以下內容鍵入您的控制台:
npm install
node ./tests/test-fs.js
完成後,檢查/tmp/blobs目錄。我建議使用樹命令,該命令為您提供目標目錄中目錄和文件的摘要。
tree ~ /blobs
tree -d ~ /blobs
我是澳大利亞昆士蘭州的技術專家Grant Carthew。我在許多個人項目中處理代碼,當需要時,我會構建自己的軟件包。
這個項目之所以存在,是因為我需要一個可以擴展的本地Blob商店。
我在開源中所做的一切都是在我自己的時間內完成的,也是對開源社區的貢獻。
如果您正在使用我的項目,並想感謝我或支持我,請單擊下面的Patreon鏈接。
在NPM上查看我的其他項目。
git checkout -b my-new-featuregit commit -am 'Add some feature'git push origin my-new-featurenode-uuid uuid 。ES5構建代碼。刪除了Nodejs發動機要求。return null語句。return null ,以防止藍鳥警告。es5dist 。包裝已更新。麻省理工學院