superbalist/laravel4-storage

此包已被弃用且不再维护。未建议替代包。

为 Laravel 4 提供文件系统抽象库

1.1.1 2022-01-11 07:15 UTC

README

为 Laravel 4 提供文件系统抽象库

Author StyleCI Software License Packagist Version Total Downloads

此包为 Laravel 4 带来了文件系统抽象层。这是 Laravel 5 的一个有机特性,它底层使用 flysystem 包。请注意,这并非与 Laravel 5 包完全相同的版本,配置和方法名可能会有细微差别。我们正在努力使此包在未来主要版本中与 Laravel 4 -> 5 升级更无缝。

支持的适配器

  • 本地
  • Rackspace (云文件)
  • 亚马逊网络服务 (S3)
  • 谷歌云

安装

composer require superbalist/laravel4-storage

在 app.php 中注册服务提供者

'providers' => array(
    'Superbalist\Storage\StorageServiceProvider',
)

在 app.php 中注册外观

'aliases' => array(
    'Storage' => 'Superbalist\Storage\StorageFacade',
)

创建 storage.php 配置文件。

<?php

return array(

    /*
    |--------------------------------------------------------------------------
    | Default Connection
    |--------------------------------------------------------------------------
    |
    | The default file system connection to use.
    |
    | A non-default connection can also be specified by using \Storage::connection('name')->put(...)
    |
    */

    'default' => 'local',

    /*
    |--------------------------------------------------------------------------
    | Connections
    |--------------------------------------------------------------------------
    |
    | The various connection configs.
    |
    */

    'connections' => array(
        'local' => array(
            'adapter' => 'local',
            'root_path' => storage_path(),
            'public_url_base' => '[[http://a.public.url.to.your.service/storage]]',
        ),

        'rackspace' => array(
            'adapter' => 'rackspace',
            'store' => 'cloudFiles',
            'region' => 'LON',
            'container' => '[[insert your cdn container name]]',
        ),

        'gcloud' => array(
            'adapter' => 'gcloud',
            'bucket' => '[[insert your bucket name]]',
        ),
    ),

);

创建 services.php 配置文件。

<?php

return array(

    /*
    |--------------------------------------------------------------------------
    | AWS
    |--------------------------------------------------------------------------
    |
    */

    'aws' => array(
        'access_key' => '[[your aws key]]',
        'secret_key' => '[[your aws secret]]',
        'region' => '[[your aws region]]',
    ),

    /*
    |--------------------------------------------------------------------------
    | Google Cloud
    |--------------------------------------------------------------------------
    |
    */

    'google_cloud' => array(
        'service_account' => '[[your service account]',
        'key_file' => '[[path to the p12 key file]]',
        'secret' => '[[your secret]]',
        'developer_key' => '[[your developer key]]',
    ),

    /*
    |--------------------------------------------------------------------------
    | Rackspace
    |--------------------------------------------------------------------------
    |
    */

    'rackspace' => array(
        'username' => '[[your username]]',
        'tenant_name' => '[[your tenant name]]',
        'api_key' => '[[your api key]]',
        'api_endpoint' => \OpenCloud\Rackspace::UK_IDENTITY_ENDPOINT,
    ),

);

使用方法

有关核心 API 的完整文档,请参阅 http://flysystem.thephpleague.com/api/

核心 API 所提供的所有功能均可在 Laravel 中的 Storage 外观后面使用。

use Storage;

// write to a file
Storage::write('path/to/file.txt', 'contents');

// update a file
Storage::update('path/to/file.txt', 'new contents');

// read a file
$contents = Storage::read('path/to/file.txt');

// check if a file exists
$exists = Storage::has('path/to/file.txt');

// delete a file
Storage::delete('path/to/file.txt');

// rename a file
Storage::rename('filename.txt', 'newname.txt');

// copy a file
Storage::copy('filename.txt', 'duplicate.txt');

// specify the storage connection to use
$contents = Storage::connection('rackspace')->listContents();

// get a public url to a file
// this is currently only supported by the local and google cloud adapters
$url = Storage::getPublicUrl('path/to/file.txt');