codeinc/object-storage

此包已被放弃,不再维护。没有建议的替代包。

对象存储

1.4.0 2018-03-13 19:04 UTC

This package is auto-updated.

Last update: 2020-01-24 20:48:54 UTC


README

此库是用PHP 7编写的,为各种云和本地对象存储平台提供抽象层,包括

  • OpenStack Swift
  • BackBlaze B2
  • SFTP
  • 本地文件系统

使用

初始化容器

use CodeInc\ObjectStorage;

// SFTP container with private key authentication
$sftpDirectory = ObjectStorage\Sftp\SftpDirectory::factoryPubKey(
    "/remote/path/to/files",
    "hostname.local",
    "remote-user",
    "path/to/public-key.pub",
    "path/to/private-key",
    "optional-key-passphrase"
    22 // optional port number
);

// SFTP container with user/password authentication
$sftpDirectory = ObjectStorage\Sftp\SftpDirectory::factoryPassword(
    "hostname.local",
    "remote-user",
    "remote-password",
    22 // optional port number
);

// Local file system container
$localDirectory = ObjectStorage\Local\LocalDirectory::factory(
    "/path/to/files"
);

// Swift container
$swiftContainer = ObjectStorage\Swift\SwiftContainer::factory(
    "container-name",
    "container-swift-region",
    "https://open-stack-auth-url.com",
    "open-stack-user",
    "open-stack-password",
    "open-stack-tenant-id",
    "open-stack-tenant-name"
);

// B2 container 
$b2Bucket = ObjectStorage\BackBlazeB2\B2Bucket::factory(
    "container-or-bucket-name",
    "b2-account-id",
    "b2-application-key"
);

创建文件

use CodeInc\ObjectStorage;

// from an existing file
$object = new ObjectStorage\Utils\InlineObject("test.jpg");
$object->setFileContent("/path/to/test.jpg");

// from a string
$object = new ObjectStorage\Utils\InlineObject("test.txt");
$object->setStringContent("C'est un test au format texte !");

上传对象

// uploading an object
$container->uploadObject($object, 'optional-new-object-name.txt');

// transfering an object from a container to another
$destinationContainer->uploadObject(
    $sourceContainer->getObject('test.jpg')
);

列出对象

foreach ($container as $file) {
    var_dump($file->getName());
}

$file 是实现 StoreObjectInterface 接口的对象。

获取对象

header('Content-Type: image/jpeg');
echo $container->getObject('test.jpg')->getContent();

getObject() 返回实现 StoreObjectInterface 接口的对象。

删除对象

// from a container
$container->deleteObject("test.jpg");

// from an object
$object = $container->getObject('test.jpg');
if ($object instanceof StoreObjectDeleteInterface) {
    $object->delete();
}

对于实现 StoreObjectDeleteInterface 的对象,可以直接在对象上调用 delete() 方法。

安装

此库通过 Packagist 提供,并可以使用 Composer 安装。

composer require codeinc/object-storage

许可协议

该库在 MIT 许可协议下发布(请参阅 LICENSE 文件)。