Introduction of Amazon Glacier on BEAR.Saturday.


Install SDK by Composer

cd App
curl -s http://getcomposer.org/installer | php
vim composer.json
composer.json
{
    "config": {
        "vendor-dir": "Vendor"
    },
    "require": {
        "aws/aws-sdk-php": "*"
    }
}
php composer.phar install

Implement interface of Glacier.

vim App/app.yml
App/app.yml
+ App_Ro_AWS:
+   key: KEY
+   secret: SECRET
+   region: REGION
+ App_Ro_Backup:
+   vault_name: VAULT_NAME
vim App/Ro/AWS.php
App/Ro/AWS.php
<?php
require_once 'App/Vendor/autoload.php';
class App_Ro_AWS extends App_Ro
{
    public function onInject()
    {
        parent::onInject();
        $app = BEAR::get('app');
        if (!array_key_exists(__CLASS__, $app)) {
            throw new BEAR_Exception(sprintf('Setting not found. [%s]', __CLASS__));
        }
        $this->_config = array_merge($this->_config, $app[__CLASS__]);
    }
}
vim App/Ro/AWS/Glacier.php
App/Ro/AWS/Glacier.php
<?php
use Aws\Glacier\GlacierClient;
class App_Ro_AWS_Glacier extends App_Ro_AWS
{
    /**
     * Generate Glacier client.
     */
    public function onRead($values)
    {
        return GlacierClient::factory(
            array(
                'key' => $this->_config['key'],
                'secret' => $this->_config['secret'],
                'region' => $this->_config['region']
            )
        );
    }
}
vim App/Ro/Backup.php
App/Ro/Backup.php
<?php
class App_Ro_Backup extends App_Ro
{
    protected $_client;

    public function onInject()
    {
        parent::onInject();
        $this->_client = $this->_resource->read(array('uri' => 'AWS/Glacier'))->getBody();
    }

    /**
     * @required path
     */
    public function onUpdate($values)
    {
        if (!is_readable($values['path'])) {
            throw new BEAR_Exception(sprintf('File not found or not readable: [%s]', $values['path']));
        }
        return $this->_client->uploadArchive(
            array(
                'vaultName' => $this->_config['vault_name'],
                'body' => fopen($values['path'], 'r')
            )
        )->get('archiveId');
    }
}

Example

bear update “Backup?path=/path/to/query.sql.tgz”