simonxiaoma / yii2-hejiang-storage
A file uploading extension for Yii2, saving files to object storage service.
Requires
- php: >=5.4.0
- aliyuncs/oss-sdk-php: ^2.3
- qcloud/cos-sdk-v5: ^1.0
- qiniu/php-sdk: ^7.2
- yiisoft/yii2: ~2.0.5
This package is auto-updated.
Last update: 2025-03-01 00:21:57 UTC
README
Thanks for your attention. This package is only used for my company projects, please do NOT use it in your product environment.
Usage
-
Firstly, add this lines of code to your Yii application config:
'components' => [ 'class' => 'Hejiang\Storage\Components\StorageComponent', 'basePath' => 'temp/', 'driver' => [ 'class' => 'Hejiang\Storage\Drivers\Local', 'accessKey' => '', 'secretKey' => '', 'bucket' => '', ] ]
-
Then after app bootstarpping, you would get the storage component instance like that:
$storage = \Yii::$app->storage;
Alternatively, you can also create a driver while app running:
$storage->setDriver('Hejiang\Storage\Drivers\Local', []);
-
Fetch uploaded file by field name:
$file = $storage->getUploadedFile('FILE-FIELD-NAME');
-
Save it.
$url = $file->saveAs('NEW-FILE-NAME.EXT'); // or $url = $file->saveWithOriginalExtension('NEW-FILE-BASE-NAME'); // or $url = $file->saveAsUniqueHash();
$url
will be a URL string which can access this file on success, orfalse
on failure.If there's any error occurred, these methods will throw a
Hejiang\Storage\Exceptions\StorageException
. Don't forget totry... catch ...
.
About
Working at: Zhejiang Hejiang Technology Co., Ltd.