Skip to content

Commit

Permalink
Update AWS SDK for PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Oct 23, 2014
1 parent e7c4bed commit 8e75f8e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
43 changes: 26 additions & 17 deletions Lib/Log/Engine/S3Log.php
Expand Up @@ -3,6 +3,12 @@
App::uses('FileLog', 'Log/Engine');
require_once(dirname(__FILE__) . '/../../../vendor/autoload.php');

use Aws\S3\S3Client;
use Aws\Common\Enum\Region;
use Aws\S3\Enum\CannedAcl;
use Aws\S3\Exception\S3Exception;
use Guzzle\Http\EntityBody;

/**
* File Storage stream for Logging with log rotate
*
Expand Down Expand Up @@ -88,38 +94,41 @@ public function write($type, $message) {
* @param $filePath
*/
private function _moveLogS3($filePath){
if (!class_exists('AmazonS3')
if (!class_exists('Aws\S3\S3Client')
|| !Configure::read('Yalog.S3Log.key')
|| !Configure::read('Yalog.S3Log.secret')
|| !Configure::read('Yalog.S3Log.bucket')) {
return false;
}
$fileName = preg_replace('/' . $this->_bufferPrefix . '/', '', basename($filePath));
$options = array('key' => Configure::read('Yalog.S3Log.key'),
'secret' => Configure::read('Yalog.S3Log.secret'),
);
$options = array(
'key' => Configure::read('Yalog.S3Log.key'),
'secret' => Configure::read('Yalog.S3Log.secret'),
);
$bucket = Configure::read('Yalog.S3Log.bucket');
$s3 = new AmazonS3($options);
$s3 = S3Client::factory($options);
$region = Configure::read('Yalog.S3Log.region');
if (!empty($region)) {
$s3->set_region($region);
$s3->setRegion($region);
}
$acl = Configure::read('Yalog.S3Log.acl');
if (empty($acl)) {
$acl = AmazonS3::ACL_PRIVATE;
$acl = CannedAcl::PUBLIC_READ;
}
$urlPrefix = Configure::read('Yalog.S3Log.urlPrefix');
$responce = $s3->create_object($bucket,
$urlPrefix . $fileName,
array(
'fileUpload' => $filePath,
'acl' => $acl,
));
if (!$responce->isOK()) {
//__('Validation Error: S3 Upload Error');

try {
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $urlPrefix . $fileName,
'Body' => EntityBody::factory(fopen($filePath, 'r')),
'ACL' => $acl,
));
$deleteLog = new File($filePath, true);
return $deleteLog->delete();
} catch (S3Exception $e) {
throw new S3Exception($e->getMessage());
return false;
}
$deleteLog = new File($filePath, true);
return $deleteLog->delete();
}
}
4 changes: 2 additions & 2 deletions Test/Case/Lib/Log/Engine/S3LogTest.php
Expand Up @@ -60,7 +60,7 @@ public function testS3Log(){
Configure::write('Yalog.S3Log.key', AWS_ACCESS_KEY);
Configure::write('Yalog.S3Log.secret', AWS_SECRET_ACCESS_KEY);
Configure::write('Yalog.S3Log.bucket', AWS_S3_BUCKET);
Configure::write('Yalog.S3Log.region', AmazonS3::REGION_TOKYO);
Configure::write('Yalog.S3Log.region', Aws\Common\Enum\Region::TOKYO);
Configure::write('Yalog.S3Log.urlPrefix', 'test_logs/');

$hash = sha1(time() . 'testS3Log');
Expand Down Expand Up @@ -88,4 +88,4 @@ public function testS3Log(){
@unlink($log);
}
}
}
}

0 comments on commit 8e75f8e

Please sign in to comment.