Created
September 21, 2017 13:29
-
-
Save xMlex/99d1d3ac72c21c5ae37b4b9cf0231f9e to your computer and use it in GitHub Desktop.
S3Client client as Yii2 component for store files and text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by Maxim Novikov. | |
* Date: 21.09.17 - 15:09 | |
*/ | |
namespace prometheus\core\components; | |
use Aws\S3\S3Client; | |
use yii\base\Component; | |
use yii\base\UserException; | |
/** | |
* Class FileServer работа с общим хранилищем файлов | |
// $this->storeString('testkey', 'my data '); | |
// $this->storeFile('testfile', '/home/mlex/www-last.tar.xz'); | |
// $this->getAsFile('testfile', '/home/mlex/public_html/prometheus/public/uploads/test.tar.gz'); | |
// $this->delete(['testkey', 'testfile']); | |
* @package prometheus\core\components | |
*/ | |
class FileServer extends Component | |
{ | |
var $server = 'http://{host}:{port}'; | |
var $bucket = '{bucket}'; | |
var $key = '{key}'; | |
var $secret = '{secret}'; | |
/** @var S3Client */ | |
protected $service; | |
public function init() | |
{ | |
$this->service = new S3Client([ | |
'version' => 'latest', | |
'region' => 'us-west-2', | |
'endpoint' => $this->server, | |
'use_path_style_endpoint' => true, | |
'credentials' => [ | |
'key' => $this->key, | |
'secret' => $this->secret, | |
], | |
]); | |
} | |
/** | |
* @param string $key | |
* @param string $value | |
*/ | |
public function storeString($key, $value) | |
{ | |
$this->service->putObject([ | |
'Bucket' => $this->bucket, | |
'Key' => $key, | |
'Body' => $value | |
]); | |
} | |
/** | |
* @param string $key | |
* @param string $filePath путь до файла | |
* @param string $contentType | |
* @throws UserException | |
*/ | |
public function storeFile($key, $filePath, $contentType = 'text/plain') | |
{ | |
if (!is_file($filePath) || !is_readable($filePath)) { | |
throw new UserException('Попытка загрузить не существующий или не читаемый файл: ' . $filePath); | |
} | |
$this->service->putObject(array( | |
'Bucket' => $this->bucket, | |
'Key' => $key, | |
'SourceFile' => $filePath, | |
'ContentType' => 'text/plain', | |
'ACL' => 'public-read', | |
'StorageClass' => 'REDUCED_REDUNDANCY', | |
)); | |
} | |
/** | |
* @param string $key | |
* @return string | |
*/ | |
public function getAsString($key) | |
{ | |
$result = $this->service->getObject(array( | |
'Bucket' => $this->bucket, | |
'Key' => $key | |
)); | |
return $result['Body']; | |
} | |
/** | |
* @param string $key | |
* @param string $filePath путь до файла | |
*/ | |
public function getAsFile($key, $filePath) | |
{ | |
$this->service->getObject(array( | |
'Bucket' => $this->bucket, | |
'Key' => $key, | |
'SaveAs' => $filePath | |
)); | |
} | |
/** | |
* Получений публичной ссылки на файл, которая будет активна 1 час | |
* @param string $key | |
* @param string $time время активности ссылки | |
* @return string | |
*/ | |
public function getUrl($key, $time = '+60 minutes') | |
{ | |
$command = $this->service->getCommand('GetObject', [ | |
'Bucket' => $this->bucket, | |
'Key' => $key | |
]); | |
$request = $this->service->createPresignedRequest($command, $time); | |
return (string)$request->getUri(); | |
} | |
/** | |
* @param array|string $key | |
*/ | |
public function delete($key) | |
{ | |
if (is_array($key)) { | |
foreach ($key as $item) { | |
$this->service->deleteObject(array( | |
'Bucket' => $this->bucket, | |
'Key' => $item | |
)); | |
} | |
return; | |
} | |
$this->service->deleteObject(array( | |
'Bucket' => $this->bucket, | |
'Key' => $key | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment