laravel-filesystem-qiniu

repository·master·Indexed 19 days ago

https://github.com/overtrue/laravel-filesystem-qiniu

A Laravel filesystem driver for Qiniu cloud storage built on overtrue/flysystem-qiniu. It integrates Qiniu as a storage disk within the standard Laravel Storage abstraction for versions >= 9.0, supporting standard filesystem operations and Qiniu-specific features via the adapter.

Tokens
1.1K
Snippets
4
Records
4
Agent score
17%

What's inside laravel-filesystem-qiniu

  1. Configure the Qiniu storage driver

    master

    To use the Qiniu driver, follow these two steps:

    1. Register the service provider in config/app.php:

    2. Define a new disk named qiniu in config/filesystems.php using the qiniu driver. You must provide your Qiniu credentials and bucket information via environment variables.

    // 1. Register the provider in config/app.php
    'providers' => [
        Overtrue\LaravelFilesystem\Qiniu\QiniuStorageServiceProvider::class,
    ],
    
    // 2. Add the disk to config/filesystems.php
    'disks' => [
        'qiniu' => [
           'driver'     => 'qiniu',
           'access_key' => env('QINIU_ACCESS_KEY', 'xxxxxxxxxxxxxxxx'),
           'secret_key' => env('QINIU_SECRET_KEY', 'xxxxxxxxxxxxxxxx'),
           'bucket'     => env('QINIU_BUCKET', 'test'),
           'domain'     => env('QINIU_DOMAIN', 'xxx.clouddn.com'), // or host: https://xxxx.clouddn.com
        ],
    ]
  2. Use the Qiniu storage disk

    master

    Once configured, you can interact with Qiniu using Laravel's Storage facade. Standard filesystem operations like put, has, copy, and read work as expected. For Qiniu-specific features like generating upload tokens or private URLs, you must access the underlying adapter via $disk->getAdapter().

    $disk = Storage::disk('qiniu');
    
    // Standard filesystem operations
    $disk->put('avatars/filename.jpg', $fileContents);
    $exists = $disk->has('file.jpg');
    $time = $disk->lastModified('file1.jpg');
    $disk->copy('old/file1.jpg', 'new/file1.jpg');
    $disk->move('old/file1.jpg', 'new/file1.jpg');
    $contents = $disk->read('folder/my_file.txt');
    
    // Qiniu-specific operations via the adapter
    $file = $disk->getAdapter()->fetch('folder/save_as.txt', $fromUrl);
    $url = $disk->getAdapter()->getUrl('folder/my_file.txt');
    $token = $disk->getAdapter()->getUploadToken('folder/my_file.txt');
    $token = $disk->getAdapter()->getUploadToken('folder/my_file.txt', 3600);
    $url = $disk->getAdapter()->privateDownloadUrl('folder/my_file.txt');
  3. Reference: Qiniu disk configuration keys

    master

    When defining the qiniu disk in config/filesystems.php, use the following keys:

    'qiniu' => [
       'driver'     => 'qiniu',           // Must be 'qiniu'
       'access_key' => '...',            // Qiniu Access Key
       'secret_key' => '...',            // Qiniu Secret Key
       'bucket'     => '...',            // Qiniu Bucket name
       'domain'     => '...',            // Qiniu Domain (e.g., 'xxx.clouddn.com' or 'https://xxxx.clouddn.com')
    ]