caseyamcl/wosclient 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

caseyamcl/wosclient

Composer 安装命令:

composer require caseyamcl/wosclient

包简介

A portable PHP client library to access data in a DDN (WOS) Web Object Scalar API

README 文档

README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

This library is a portable HTTP client for the DDN Web Object Scalar storage system HTTP API.

Unlike the official DDN PHP client, this library communicates with the WOS device over HTTP, and does not require the installation of any PHP C extensions.

Install

This library requires PHP v5.5 or newer. It has been tested with PHP7. If you wish to run tests, you must run v5.6 or newer.

I also recommend installing the ext-curl PHP extension.

Via Composer

$ composer require caseyamcl/wosclient guzzlehttp/guzzle

Note: By default, this library uses Guzzle v6.0. However, if you do not wish to use Guzzle 6, you can create your own implementation by implementing the WosClientInterface yourself (details below).

Usage

This library uses PSR-4 autoloading. If you are not using Composer or another PSR-4 autoloader, you will need to manually include all of the files in the src/ directory (but I highly discourage this; use an autoloader!).

If you know the URL to your WOS API and your WOS Policy name or ID, you can create a WosClient instance by calling the WosClient::build() constructor. Pass in the URL to your WOS API and your WOS Policy name or ID:

use WosClient\WosClient;

$wosClient = WosClient::build('http://mywos.example.org/', 'my-policy-id');

The WosClient contains several public methods for interacting with the object storage API:

// Get an object by its Object ID (OID)
$wosObject = $wosClient->getObject('abcdef-ghijkl-mnopqr-12345');

// Get a range of data for a large object (get bytes 50000-100000)
$partialWosObject = $wosClient->getObject('abcdef-ghijkl-mnopqr-12345', '50000-100000'); 

// Get metadata for an Object ID (OID)
$wosMetadata = $wosClient->getMetadata('abcdef-ghijkl-mnopqr-12345');

// Put an object
$wosObjectId = $wosClient->putObject('some-serializable-or-streamable-data', ['some' => 'metadata']);

// Reserve an Object ID, without putting any data in it yet
$wosObjectId = $wosClient->reserveObject();

// Put an object having reserved its ID ahead of time with reserveObject()
$wosObjectId = $wosClient->putObject('some-serializable-or-streamable-data', [], $reservedObjectId);

// Delete an object
$wosClient->deleteObject('abcdef-ghijkl-mnopqr-12345');

All of these methods optionally accept an array of Guzzle HTTP request options as the last method parameter. If you pass any options in this way, they will override all default and computed request options. If you pass in HTTP headers in this way, they will be merged with the default headers (see Guzzle Docs).

Using responses

The WosClient::getObject() method returns an instance of WosClient\WosObject:

// Get the object
$wosObject = $wosClient->getObject('abcdef-ghijkl-mnopqr-12345');

// Get the data from the response as a string
$wosObject->getData()->__toString();

// or, as a shortcut..
$wosObject->__toString();

// Get the Object ID
$wosObject->getId();

The WosClient::getMetadata() and the WosObject::getMetadata() methods return an instance of WosClient\WosObjectMetadata:

// Get the meta-data (instance of WosObjectMetadata; see below)
$metadata = $wosObject->getMetadata();

// Get access to the HTTP response
$wosObject->getHttpResponse();

// Get the metadata from the object
$wosObject = $wosClient->getObject('abcdef-ghijkl-mnopqr-12345');
$metadata = $wosObject->getMetadata();

// ..or just get the metadata for the object from the WOS server without
// getting the content
$metadata = $wosClient->getMetadata('abcdef-ghijkl-mnopqr-12345');

// Get the object ID
$objectId = $metadata->getObjectId();

// Get the object size in bytes - This returns NULL if not known
$numBytes = $metadata->getObjectSize();

// Access custom metadata (having been added with `WosObject::putMetadata()`
$foo = $metadata->get('foo');

// Conditionally get metadat if it exists
if  ($metadata->has('bar')) {
    $bar = $metadata->get('bar');
}

// Metadata implements \Countable, \ArrayAccess, and \Traversable
$foo = $metadata['foo'];
$bar = $metadata['bar'];
$num = count($metadata);

for ($metadata as $key => $val) {
    echo "$key: $val";
}

The WosClient::putObject() and WosClient::reserveObject() methods return an instance of WosClient\WosObjectId:

// Put an object with auto-generated ID
$wosObjectId = $wosClient->putObject('some object data');

// Reserve an object ID
$wosObjectId = $wosClient->reserveObject();

// Get the ID as a string
$idString = $wosObjectId->getId();

// ..or cast as string..
$idString = (string) $wosObjectId;

Streaming large objects

The WOS supports objects up to 5 terabytes in size!

If the object you are retrieving from the WOS server is very large, it is not a good idea to read the entire thing into memory at once.

Fortunately, by default, this library will stream data from the WOS server, instead of downloading it into memory. The library uses the PSR-7 StreamableInterface to accomplish this.

To stream a large file, simply seek through it rather than converting it to a string:

$veryLargeWosObject = $wosClient->getObject('abcdef-ghijkl-mnopqr-12345');
$dataStream = $veryLargeWosObject->getData();

// Read the data stream 1024 bytes at a time, and 
// echo the data out..  You could do anything with the chunked data if
// you wish...
while ( ! $dataStream->eof()) {
    echo $dataStream->read(1024);
}

Another way to stream data from your WOS server is to specify the $range parameter of the WosClient::getObject() to retrieve only chunks of the large object at a time:

$metadata  = $wosClient->getMetadata('abcdef-ghijkl-mnopqr-12345');
$chunkSize = 1024; // read this many bytes at a time

for ($i = 0; $i < $metadata->getLength(); $i+= $chunkSize) {

    $from = $i;
    $to   = $i + $chunkSize;

    // WosClient::getObject second parameter accepts range in the format '####-####' (e.g. '1024-2048')
    echo $wosClient->getObject('abcdef-ghijkl-mnopqr-12345', $from . '- . $to)->__toString();
}

Handling errors

This library converts all application-layer runtime errors into instances of WosClient\Exception\WosException. There are three sub-classes:

  • WosClient\Exception\WosServerException - This exception is thrown when the WOS server rejects the request or encounters an error and returns a response with a x-ddn-status header other than success (0). The exception code will correspond to the WOS DDN Status code, and the message will be a detailed description of the code that was returned from the server.
  • WosClient\Exception\InvalidResponseException - This exception is thrown when a HTTP response from the server is missing a HTTP header that is expected to be present. E.g. when a getObject() sever response does not include a x-ddn-oid header.
  • WosClient\Exception\InvalidParameterException - This exception is thrown when a provided header value is not in the expected format that the WOS requires. For example, if the Range header is not in ###-### format. It is thrown before the request is sent to the server.

Example:

use WosClient\Exception\WosServerException;

try {
   $wosClient->getObject('does-not-exist');
} catch (WosServerException $e) {

    // WOS User-friendly message, e.g., 'Object cannot be located'
    echo $e->getMessage();
    
    // WOS Code, e.g., 207
    echo $e->getCode();
    
    // WOS Error Name (machine-friendly, uses CamelCase), e.g., 'ObjNotFound'
    echo $e->getErrorName();
}

Note that WosServerException is ONLY thrown when the server emits a response with the x-ddn-status header present, and the header is a non-zero value.

It is NOT thrown in the event of any other HTTP transmission error (such as network timeout, or an internal WOS server error). You can catch these types of HTTP errors separately, by catching Guzzle exceptions:

use WosClient\Exception\WosServerException;
use WosClient\Exception\WosRequestException;
use GuzzleHttp\Exception\GuzzleException;

try {
   $wosClient->getObject('does-not-exist');
}
catch (WosServerException $e) {

    // WOS Exception thrown by the WOS Client
    echo 'WOS Error: ' . $e->getMessage();
    
}
catch (WosRequestException $e) {

    // Some other application exception occurred, such as
    // the WOS server returned a response that is missing an
    // expected header (this really should never happen)
    echo 'Something strange happened: ' . $e->getMessage();
}
catch (GuzzleException $e) {

   // HTTP Exception thrown by Guzzle
   echo 'HTTP Error: ' . $e->getMessage();
}

The Guzzle library contains a number of different exception classes for specific error cases, in case you wish to be more specific about your exception handling.

Instantiating with a custom Guzzle 6 client instance

You may wish to use your own Guzzle 6 Client instance to make requests to the WOS server. Some examples of why you may wish to do this include:

  • you have setup your own request/response middleware, or
  • you wish to use custom HTTP request default values (such as connect_timeout), or
  • you wish to gain access to HTTP Response objects during the request/response cycle.

To use a custom Guzzle client instance, simply use the main constructor for the WosClient\WosClient class instead of the build() constructor.

The base_uri parameter MUST be set in your Guzzle client class, or the library will throw a \RuntimeException during object construction. This value must be the URL for one of your WOS nodes.

You also may wish to set the x-ddn-policy header, so that you do not need to specify it in each request:

use WosClient\WosClient;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;

// Setup custom Guzzle client
$guzzleClient = new GuzzleClient([
    'base_uri'        => 'http://mywos.example.org/',
    'connect_timeout' => 60,
    'handler'         => HandlerStack::create(new CurlHandler()),
    'headers'         => [
        'x-ddn-policy' => 'my-policy'
    ],
    /** ..other guzzle options here.. */
]);

// Instantiate a new WOS Client
$wosClient = new WosClient($guzzleClient);

Creating a different WOS Client implementation

You may wish to write your own implementation for the interfaces included in this library. The only dependency in this case is that you must include the "psr/http-message": "~1.0" package.

If your implementation uses a PSR-7 compliant HTTP library, you only need to implement the WosClient\WosClientInterface. You can use the built-in implementations of all other classes.

If, however, your implementation does NOT implement PSR-7, you will need to implement the following interfaces:

  • WosClient\WosClientInterface
  • WosObjectInterface
  • WosObjectIdInterface
  • WosObjectMetadataInterface
  • Psr\Http\Message\StreamInterface (hint: Guzzle Streams does this pretty well)

Each interface file contains pretty good documentation for how its methods should behave.

Note that you should only throw exceptions in specific cases:

  • WosClient\Exception\WosServerException - Throw this if the WOS server emits an error code (refer to the WOS API documentation).
  • WosClient\Exception\InvalidParameterException - Throw this if you validate parameters or HTTP headers on the client-side, before sending the request to the server.
  • WosClient\Exception\InvalidResponseException - Throw this if the server generates a response that the client does not know how to process. For example, the server does not include a HTTP header that is expected to exist.

Change log

Please see CHANGELOG for more information about what has changed recently.

Testing

To run tests, be sure that you have installed all of the dependencies in the require-dev portion of the composer.json file.

Run unit tests:

$ composer test

This library also includes a simple console utility to test the client against your own WOS device. The test suite writes two tiny objects to the WOS and then deletes them:

$ composer livetest http://your-wos.example.org your-policy-id

Run the PHP CodeSniffer to detect style errors:

$ composer sniff

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email caseyamcl@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

caseyamcl/wosclient 适用场景与选型建议

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

它主要适用于以下技术方向: 「storage」 「caseyamcl」 「wosclient」 「wos」 「ddn」 「web object scalar」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 85
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

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