diff --git a/readme.md b/readme.md index 284f185..312efc2 100644 --- a/readme.md +++ b/readme.md @@ -218,12 +218,16 @@ Upload assets to CDN ```bash php artisan cdn:push ``` + +You can specify a folder upload prefix in the cdn.php config file. Your assets will be uploaded into that folder on S3. + #### Empty Delete assets from CDN ```bash php artisan cdn:empty ``` +CAUTION: This will erase your entire bucket. This may not be what you want if you are specifying an upload folder when you push your assets. #### Load Assets diff --git a/src/Publiux/laravelcdn/Providers/AwsS3Provider.php b/src/Publiux/laravelcdn/Providers/AwsS3Provider.php index 6ec54dc..7101a7a 100644 --- a/src/Publiux/laravelcdn/Providers/AwsS3Provider.php +++ b/src/Publiux/laravelcdn/Providers/AwsS3Provider.php @@ -49,6 +49,7 @@ class AwsS3Provider extends Provider implements ProviderInterface 'version' => null, 'region' => null, 'buckets' => null, + 'upload_folder' => '', 'http' => null, 'acl' => 'public-read', 'cloudfront' => [ @@ -138,6 +139,7 @@ public function init($configurations) 'cloudfront' => $this->default['providers']['aws']['s3']['cloudfront']['use'], 'cloudfront_url' => $this->default['providers']['aws']['s3']['cloudfront']['cdn_url'], 'http' => $this->default['providers']['aws']['s3']['http'], + 'upload_folder' => $this->default['providers']['aws']['s3']['upload_folder'] ]; // check if any required configuration is missed @@ -180,7 +182,7 @@ public function upload($assets) // the bucket name 'Bucket' => $this->getBucket(), // the path of the file on the server (CDN) - 'Key' => str_replace('\\', '/', $file->getPathName()), + 'Key' => $this->supplier['upload_folder'] . str_replace('\\', '/', $file->getPathName()), // the path of the path locally 'Body' => fopen($file->getRealPath(), 'r'), // the permission of the file @@ -413,4 +415,4 @@ public function __get($attr) { return isset($this->supplier[$attr]) ? $this->supplier[$attr] : null; } -} \ No newline at end of file +} diff --git a/src/config/cdn.php b/src/config/cdn.php index f80d115..8de77ee 100644 --- a/src/config/cdn.php +++ b/src/config/cdn.php @@ -117,6 +117,25 @@ // 'your-js-bucket-name-here' => ['public/js'], // 'your-css-bucket-name-here' => ['public/css'], ], + + /* + |-------------------------------------------------------------------------- + | CDN Bucket Upload Folder Prefix + |-------------------------------------------------------------------------- + | + | You can specify a prefix for files when they are pushed/uploaded to S3. + | For example, if you want to have your files uploaded into a folder in the + | called 'webassets', you would specify 'webassets/'. + | + | When the command 'php artisan cdn:push' is run, your assets will be + | uploaded to S3, but will be placed inside this folder. + | + | If you do not wish to set this prefix, leave an empty string as the value. + | + | NOTE: YOUR FOLDER NAME PREFIX MUST END WITH A TRAILING SLASH, e.g. 'folder/' + | + */ + 'upload_folder' => env('AWS_CDN_ASSET_UPLOAD_FOLDER', ''), /* |--------------------------------------------------------------------------