recommerce/asset

此包已被弃用且不再维护。未建议替代包。
此包最新版本(5.1.0)没有可用的许可证信息。

5.1.0 2018-07-23 12:33 UTC

README

Build Status Code Climate Test Coverage

Recommerce asset

此库提供了一个接口和一些实现来处理资产上的文件。

当前实现包括

  • AWS S3 : 使用AWS S3 SDK库的亚马逊S3服务;
  • FileSystem : 使用您本地的文件系统作为资产;
  • FTP : FTP服务器(不是SFTP)。

使用Composer安装

composer require recommerce/asset:^3.0
composer update

使用示例

AWS S3客户端创建

直接配置

    use Recommerce\Asset\Adapter\Factory\S3ClientFactory;
    use Recommerce\Asset\AssetFactory;

    $config = [
        'factory' => S3ClientFactory::class,
        'params' => [
            'key'    => 'YOUR_S3_KEY',
            'secret' => 'YOUR_S3_SECRET',
            'region' => 'YOUR_AWS_REGION',
            'bucket' => 'YOUR_S3_BUCKET_NAME'
        ]
    ];

    $assetClient = (new AssetFactory())->createServiceFromConfig($config);

服务管理器

    use Recommerce\Asset\Adapter\Factory\S3ClientFactory;
    use Recommerce\Asset\AssetFactory;
    use Zend\ServiceManager\Config;
    use Zend\ServiceManager\ServiceManager;

    $config = [
        'asset' => [
            'factory' => S3ClientFactory::class,
            'params' => [
                'key'    => 'YOUR_S3_KEY',
                'secret' => 'YOUR_S3_SECRET',
                'region' => 'YOUR_AWS_REGION',
                'bucket' => 'YOUR_S3_BUCKET_NAME'
            ]
        ],
        'service_manager' => [
            'factories' => [
                'recommerce.asset.asset-client' => AssetFactory::class
            ]
        ]
    ];

    $serviceManager = new ServiceManager(new Config($config['service_manager']));
    $serviceManager->setService('Config', $config);

    $assetClient = $serviceManager->get('recommerce.asset.asset-client');

可选参数

    use Recommerce\Asset\Adapter\Factory\S3ClientFactory;
    use Recommerce\Asset\AssetFactory;

    $config = [
        'factory' => S3ClientFactory::class,
        'params' => [
            'key'    => 'YOUR_S3_KEY',
            'secret' => 'YOUR_S3_SECRET',
            'region' => 'YOUR_AWS_REGION',
            'bucket' => 'YOUR_S3_BUCKET_NAME',
            'tmpDir' => 'YOUR_LOCAL_TMP_DIR',
            'rootUrl' => 'BASE_URI_TO_S3_REPO'
            'isPrivate' => true|false
        ]
    ];

    $assetClient = (new AssetFactory())->createServiceFromConfig($config);

以下添加的可选参数允许

  • 'tmpDir' : (默认为 /tmp) 指定本地临时目录的绝对路径
  • 'rootUrl' : S3存储桶的根URL,当从文件名生成完整URL时很有用 $assetClient->getUrl('relative_path/to/file');
  • 'isPrivate' : 通过此S3Client实例创建的文件将具有私有还是公共读访问权限

文件系统客户端创建

直接配置

    use Recommerce\Asset\AssetFactory;
    use Recommerce\Asset\Adapter\Factory\FileSystemClientFactory;

    $config = [
        'factory' => FileSystemClientFactory::class,
        'params' => [
            'repository' => 'YOUR_LOCAL_ASSET_REPOSITORY'
        ]
    ];

    $assetClient = (new AssetFactory())->createServiceFromConfig($config);

FTP客户端创建

直接配置

    use Recommerce\Asset\AssetFactory;
    use Recommerce\Asset\Adapter\Factory\FtpClientFactory;

    $config = [
        'factory' => FtpClientFactory::class,
        'params' => [
            'hostname' => 'YOUR_HOST',
            'username' => 'YOUR_USERNAME',
            'password' => 'YOUR_PASSWORD',
            'port' => 'YOUR_PORT' // 21 by default,
        ]
    ];

    $assetClient = (new AssetFactory())->createServiceFromConfig($config);

基本

// Get asset file on local file system
$assetClient->get('path/to/remote_file.png', '/tmp/my_local_file.png');

// Put local file on asset
$assetClient->put('/tmp/my_local_file.png', 'path/to/remote_file.png');

// Check file existence on asset
$assetClient->exists('path/to/remote_file.png');

// Remove asset file
$assetClient->remove('path/to/remote_file.png');