helmich/gridfs

MongoDB PHP 扩展的 GridFS 实现

v1.0.0 2016-02-21 16:25 UTC

This package is auto-updated.

Last update: 2024-08-28 04:36:01 UTC


README

Build Status Code Climate Issue Count Test Coverage

本包为 PHP 的新 mongodb 扩展(不要与同名 mongo 扩展混淆)提供了 GridFS 规范的用户空间实现。

此库需要 PHP 7!

作者和许可证

Martin Helmich
此库采用 MIT 许可证

安装

使用 Composer

$ composer require helmich/gridfs

使用方法

初始化

GridFS 围绕 进行操作。对于名为 $name 的桶,此库将在其中创建两个集合 $name.files$name.chunks,用于存储文件元数据和内容块。通过实例化 Helmich\GridFS\Bucket 类来创建一个新的桶。您需要一个 MongoDB\Database 实例作为依赖项,并可选择传递 Helmich\GridFS\Options\BucketOptions 类的实例来配置您的桶

$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
$database = new \MongoDB\Database($manager, 'yourdatabase');

$bucketOptions = (new \Helmich\GridFS\Options\BucketOptions)
  ->withBucketName('yourbucket');
$bucket = new \Helmich\GridFS\Bucket($database, $bucketOptions);

将文件上传到桶中

文件上传是通过流完成的。您可以使用 openUploadStream 函数打开一个新的上传流

$uploadOptions = (new \Helmich\GridFS\Options\UploadOptions)
  ->withChunkSizeBytes(4 << 10)
  ->withMetadata(['foo' => 'bar']);

$uploadStream = $bucket->openUploadStream("helloworld.txt", $uploadOptions);
$uploadStream->write("Hello World!");
$uploadStream->close();

echo "File ID is: " . $uploadStream->fileId();

或者,使用 uploadFromStream 方法,该方法接受一个 PHP 流作为参数

$fileStream = fopen('humungousfile.blob', 'r');
$fileId = $bucket->uploadFromStream('humungousfile.blob', $fileStream, $uploadOptions);

查找已上传的文件

使用 find 方法在您的桶中查找文件。find 方法将 MongoDB 查询对象作为第一个参数,并将其应用于 $bucketName.files 集合。第二个参数是 Helmich\GridFS\Options\FindOptions 类的实例,在其中您可以指定搜索的高级选项

$options = (new \Helmich\GridFS\Options\FindOptions)
  ->withBatchSize(32)
  ->withLimit(128)
  ->withSkip(13)
  ->withSort(['filename' => 1])
  ->withNoCursorTimeout();

$query = [
  'filename' => [
    '$in': ['foo.txt', 'bar.txt']
  ],
  'uploadDate' => [
    '$gt' => new \MongoDB\BSON\UTCDatetime(time() - 86400)
  ]
];

$files = $bucket->find($query, $options);

从桶中下载文件

下载文件也是以流为导向的。给定已存在的文件的 ID,使用 openDownloadStream 函数打开一个新的下载流

$file = $bucket->find(['filename' => 'foo.txt'], (new \Helmich\GridFS\Options\FindOptions)->withLimit(1))[0];

$downloadStream = $bucket->openDownloadStream($file['_id']);
echo $downloadStream->readAll();

// alternatively:
while (!$downloadStream->eof()) {
  echo $downloadStream->read(4096);
}

给定一个已存在的(可写的)PHP 流,您还可以使用 downloadToStream 方法将文件直接管道到流中

$fileStream = fopen('humungousfile.blob', 'w');
$bucket->downloadToStream($id, $fileStream);

还有 openDownloadStream (openDownloadStreamByName()) 和 downloadToStream (downloadToStreamByName) 的 byName 变体。这两个函数都接受一个文件名和 Helmich\GridFS\Options\DownloadOptions 实例作为参数。$options 参数允许您指定应下载给定文件名的哪个版本

$options = (new \Helmich\GridFS\Options\DownloadByNameOptions)
  ->withRevision(-1); // also the default; will download the latest revision of the file

$stream = $bucket->openDownloadStreamByName('yourfile.txt', $options);

删除文件

使用 delete 方法从桶中删除文件

$bucket->delete($fileId);

特性

PSR-7 适配器

我为一个符合 PSR-7 的 Web 应用实现了此包。PSR-7 也严重依赖于流,因此我添加了一个适配器类,将 Helmich\GridFS\Stream\DownloadStreamInterface 映射到 Psr\Http\Message\StreamInterface。这对于您想要将 GridFS 文件作为响应体流返回特别有用。以下示例使用 Slim 框架,但应容易适应其他符合 PSR-7 的框架

$app->get(
  '/documents/{name}',
  function(RequestInterface $req, ResponseInterface $res, array $args) use ($bucket): ResponseInterface
  {
    $stream = $bucket->openDownloadStreamByName($args['name']);
    return $res
      ->withHeader('content-type', $stream->file()['metadata']['contenttype'])
      ->withBody(new \Helmich\GridFS\Stream\Psr7\DownloadStreamAdapter($stream));
  }
);