旨在防止与分布式文件系统或存储区域网络一起使用的文件系统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 。包装已更新。麻省理工学院