承接 helmich/gridfs 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

helmich/gridfs

Composer 安装命令:

composer require helmich/gridfs

包简介

GridFS implementation for the MongoDB PHP extension

README 文档

README

Build Status Code Climate Issue Count Test Coverage

This package provides a userspace implementation of the GridFS specification for PHP's new mongodb extension (not to be confused with the similarly named mongo extension).

This library requires PHP 7!

Author and license

Martin Helmich
This library is MIT-licensed.

Installation

Use Composer:

$ composer require helmich/gridfs

Usage

Initialization

GridFS is oriented around Buckets. For a bucket named $name, this library will create two collections $name.files and $name.chunks in which file metadata and content blocks will be stored. Create a new bucket by instantiating the Helmich\GridFS\Bucket class. You will need a MongoDB\Database instance as dependency and can optionally pass an instance of the Helmich\GridFS\Options\BucketOptions class to configure your bucket:

$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);

Uploading files into a bucket

Uploading of files is done via streams. You can open a new upload stream using the openUploadStream function:

$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();

Alternatively, use the uploadFromStream method, which takes a PHP stream as a parameter:

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

Finding uploaded files

Use the find method to find files in your bucket. The find method takes a MongoDB query object as first parameter that is applied to the $bucketName.files collection. The second parameter is an instance of the Helmich\GridFS\Options\FindOptions class in which you can specify advanced options for the search:

$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);

Downloading files from a bucket

Downloading files is also stream-oriented. Given the ID of an already existing file, open a new download stream using the openDownloadStream function:

$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);
}

Given an already existing (and writeable) PHP stream, you can also use the downloadToStream method to pipe the file directly into the stream:

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

There is also a byName variant of both openDownloadStream (openDownloadStreamByName()) and downloadToStream (downloadToStreamByName). Both of these functions take a file name and a Helmich\GridFS\Options\DownloadOptions instance as parameter. The $options parameter allows you to specify which revision of the given filename should be downloaded:

$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);

Deleting files

Delete files from the bucket using the delete method:

$bucket->delete($fileId);

Gimmicks

PSR-7 adapters

I've implemented this package for a PSR-7 compliant web application. PSR-7 also heavily relies on streams, so I've found it useful to add an adapter class to map a Helmich\GridFS\Stream\DownloadStreamInterface to a Psr\Http\Message\StreamInterface. This is especially useful if you want to return GridFS files as a response body stream. The following example uses the Slim framework, but should be easily adaptable to other PSR-7 compliant frameworks:

$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));
  }
);

helmich/gridfs 适用场景与选型建议

helmich/gridfs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.35k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2016 年 06 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 helmich/gridfs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 helmich/gridfs 我们能提供哪些服务?
定制开发 / 二次开发

基于 helmich/gridfs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 12.35k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-07