Skip to content

Commit

Permalink
Adding subdirectory option for S3 bucket upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnnnyw committed Nov 24, 2016
1 parent 2e85b5f commit 23ede03
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
10 changes: 8 additions & 2 deletions AWSS3AssetsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,14 @@ private function getS3Bucket()
* @access public
* @param \Craft\AssetFileModel $asset
* @return void
* @throws \Craft\Exception
*/
public function copyAsset(AssetFileModel $asset)
{
$settings = $this->getSettings();

try {
$this->getS3Bucket()->cp($this->getAssetPath($asset), $asset->filename);
$this->getS3Bucket()->cp($this->getAssetPath($asset), trim('/', $settings->bucketPath . '/' . $asset->filename));
} catch (\Exception $e) {
throw new CraftException($e->getMessage());
}
Expand All @@ -208,11 +211,14 @@ public function copyAsset(AssetFileModel $asset)
* @access public
* @param \Craft\AssetFileModel $asset
* @return void
* @throws \Craft\Exception
*/
public function deleteAsset(AssetFileModel $asset)
{
$settings = $this->getSettings();

try {
$this->getS3Bucket()->rm($asset->filename);
$this->getS3Bucket()->rm(trim('/', $settings->bucketPath . '/' . $asset->filename));
} catch (\Exception $e) {
throw new CraftException($e->getMessage());
}
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ The AWS region that you created your S3 bucket in e.g. us-east-1. Please ensure
##### Bucket Name
The name of your AWS S3 bucket. This is the name your provided when you created your bucket e.g. media.mywebsite.com.

##### Bucket Path (optional)
Path to upload assets to in your S3 bucket. If left blank it will upload assets to the root of your S3 bucket.

##### AWS Key (optional)
Your IAM user key. The IAM user associated with this key must have read/write access to your AWS S3 bucket.

Expand All @@ -62,4 +65,4 @@ Your IAM user secret. The IAM user associated with this key must have read/write

If you wish to serve your assets from an AWS CloudFront distribution, then you will need to create an asset source that points to CloudFront.

Navigate to `Settings > Assets` in your Craft control panel. Click `New asset source`. Configure your asset source as your normally would with the exception of the URL field. Here you will need to add the URL of your CloudFront distribution. More information can be found on setting up a CloudFront distribution, in the [documentation](http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/GettingStarted.html).
Navigate to `Settings > Assets` in your Craft control panel. Click `New asset source`. Configure your asset source as you normally would with the exception of the URL field. Here you will need to add the URL of your CloudFront distribution. More information can be found on setting up a CloudFront distribution, in the [documentation](http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/GettingStarted.html).
8 changes: 8 additions & 0 deletions release.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@
"notes": [
"[Added] Initial release"
]
},
{
"version": "1.1.0",
"downloadUrl": "https://github.com/jonnnnyw/craft-awss3assets/archive/v1.1.0.zip",
"date": "2016-11-24T10:30+00:00",
"notes": [
"[Added] Can now set S3 bucket upload path"
]
}
]
1 change: 1 addition & 0 deletions src/JonnyW/AWSS3Assets/Model/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected function defineAttributes()
return array(
'bucketRegion' => array(AttributeType::String, 'required' => true),
'bucketName' => array(AttributeType::String, 'required' => true),
'bucketPath' => array(AttributeType::String, 'required' => false),
'awsKey' => array(AttributeType::String, 'required' => false),
'awsSecret' => array(AttributeType::String, 'required' => false)
);
Expand Down
10 changes: 10 additions & 0 deletions templates/_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
errors: settings.getErrors('bucketName')
}) }}

{{ forms.textField({
label: "Bucket Path"|t,
required: false,
id: 'bucketPath',
name: 'bucketPath',
instructions: "The subdirectory to upload your assets to. Leave blank to upload to the root of your S3 bucket."|t,
value: settings.bucketPath,
errors: settings.getErrors('bucketPath')
}) }}

{{ forms.textField({
label: "AWS Key"|t,
id: 'awsKey',
Expand Down
19 changes: 19 additions & 0 deletions tests/JonnyW/AWSS3Assets/Integration/S3BucketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ public function testCanOverwriteExistingMediaInS3Bucket()
$this->assertTrue($bucket->cp($path, $name) && $bucket->cp($path, $name));
}

/**
* Test can copy new media
* to S3 bucket in subdirectory
*
* @return void
*/
public function testCanCopyNewMediaToS3BucketInSubDirectory()
{
$file = 'assets/' . $this->getFileName();

$bucket = S3Bucket::getInstance('us-east-1', 'tests.jonnyw.me');
$bucket->cp(
$this->getFilePath(),
$file
);

$this->assertInstanceOf('\Aws\Result', $bucket->get($file));
}

/**
* Test can delete media from
* S3 bucket
Expand Down

0 comments on commit 23ede03

Please sign in to comment.