Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up for modern known #8

Merged
merged 2 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
89 changes: 34 additions & 55 deletions Main.php
Original file line number Diff line number Diff line change
@@ -1,67 +1,46 @@
<?php

namespace IdnoPlugins\S3 {
namespace IdnoPlugins\S3 {

class Main extends \Idno\Common\Plugin {
use \Idno\Common\Plugin;
use \Aws\S3\S3Client;

function registerPages() {
\Idno\Core\site()->addPageHandler('/file/([A-Za-z0-9]+)(/.*)?', '\IdnoPlugins\S3\Pages\File\View', true);
\Idno\Core\site()->hijackPageHandler('/file/([A-Za-z0-9]+)(/.*)?', '\IdnoPlugins\S3\Pages\File\View', true);
}

function registerEventHooks() {

// All of this only makes sense if we have an aws_key and aws_secret

if (!empty(\Idno\Core\site()->config()->aws_key)
&& !empty(\Idno\Core\site()->config()->aws_secret)
&& !empty(\Idno\Core\site()->config()->aws_bucket)) {

// Load AWS SDK and dependencies
$classLoader = new \Symfony\Component\ClassLoader\UniversalClassLoader();
$classLoader->registerNamespaces(array(
'Aws' => dirname(__FILE__) . '/external/aws-sdk/src',
'Guzzle' => dirname(__FILE__) . '/external/guzzle/src',
'Doctrine' => dirname(__FILE__) . '/external/doctrine/lib/Doctrine',
'Psr' => dirname(__FILE__) . '/external/psrlog',
'Monolog' => dirname(__FILE__) . '/external/monolog/src'
));

$classLoader->register();
class Main extends Plugin {
public $_s3client;

if (empty(\Idno\Core\site()->config()->aws_region)) {
$region = 'us-east-1';
} else {
$region = \Idno\Core\site()->config()->aws_region;
}

$params = array(
'key' => \Idno\Core\site()->config()->aws_key,
'secret' => \Idno\Core\site()->config()->aws_secret,
'region' => $region,
);

if (!empty(\Idno\Core\site()->config()->aws_base_url)) {
$params['base_url'] = \Idno\Core\site()->config()->aws_base_url;
}

if (!empty(\Idno\Core\site()->config()->aws_suppress_region)) {
unset($params['region']);
}
function registerEventHooks() {
$config = \Idno\Core\Idno::site()->config();
// All of this only makes sense if we have an aws_key and aws_secret
if (!empty($config->aws_key)
&& !empty($config->aws_secret)
&& !empty($config->aws_bucket)) {

$aws = \Aws\Common\Aws::factory($params);

// Impose the S3 filesystem
\Idno\Core\site()->filesystem = new \IdnoPlugins\S3\S3FileSystem();
$s3client = $aws->get('S3');
$s3client->registerStreamWrapper();
if (empty($config->aws_region)) {
$region = 'us-east-1';
} else {
$region = $config->aws_region;
}

\Idno\Core\site()->filesystem->attachAWSClient($s3client);
$params = array(
'credentials' => [
'key' => $config->aws_key,
'secret' => $config->aws_secret,
],
'region' => $region,
'version' => 'latest',
);

if (!empty($config->aws_base_url)) {
$params['base_url'] = $config->aws_base_url;
}

if (!empty($config->aws_suppress_region)) {
unset($params['region']);
}

$this->_s3client = S3Client::factory($params);
$this->_s3client->registerStreamWrapper();
}

}

}
}
}
79 changes: 0 additions & 79 deletions Pages/File/View.php

This file was deleted.

10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
Amazon S3 file handler for Known
================================

Uses the Amazon Simple Storage Service (S3) as a file back-end for Known.
Uses the Amazon Simple Storage Service (S3) to allow S3 file storage for the local back-end for Known.

Installation
------------

1. Modify your known `composer.json` to include the below; then update your lockfile
* `"idno/S3": "^1.0.0"`
2. `composer update --lock`
* Enable the plugin
* Set the following values in your site config.ini:
* aws_key: Your AWS access key
* aws_secret: Your AWS access secret
* aws_bucket: The S3 bucket you want to store data inside
* aws_region: The region you wish to connect to if not 'us-east-1'
3. modify your config.ini so that `s3://your-bucket/Uploads` is the `uploadpath`
4. Enjoy!

Once these are set, S3 will be set as the back-end for all files.
It is worthwhile using s3 sync to retrieve existing files and upload.
This has only been manually tested on greenfield deploys.

License & libraries
-------------------
Expand Down
118 changes: 0 additions & 118 deletions S3File.php

This file was deleted.