From d1a8dc4a8892d2db97c355a77f97090e72a5e604 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Tue, 4 Mar 2014 14:47:10 +1100 Subject: [PATCH] Support for setting a custom hostname for the bucket This allows one to easily use a CDN such as CloudFront to serve the assets from the S3 Bucket --- inc/class-s3-uploads.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/inc/class-s3-uploads.php b/inc/class-s3-uploads.php index d7aa01bc..8ea5c6eb 100644 --- a/inc/class-s3-uploads.php +++ b/inc/class-s3-uploads.php @@ -4,6 +4,7 @@ class S3_Uploads { private static $instance; private $bucket; + private $bucket_hostname; private $key; private $secret; @@ -15,17 +16,19 @@ class S3_Uploads { */ public static function get_instance() { - if ( ! self::$instance ) - self::$instance = new S3_Uploads( S3_UPLOADS_BUCKET, S3_UPLOADS_KEY, S3_UPLOADS_SECRET ); + if ( ! self::$instance ) { + self::$instance = new S3_Uploads( S3_UPLOADS_BUCKET, S3_UPLOADS_KEY, S3_UPLOADS_SECRET, defined( 'S3_UPLOADS_BUCKET_HOSTNAME' ) ? S3_UPLOADS_BUCKET_HOSTNAME : null ); + } return self::$instance; } - public function __construct( $bucket, $key, $secret ) { + public function __construct( $bucket, $key, $secret, $bucket_hostname = null ) { $this->bucket = $bucket; $this->key = $key; $this->secret = $secret; + $this->bucket_hostname = $bucket_hostname ? $bucket_hostname : 'https://' . strtok( $this->bucket, '/' ) . '.s3.amazonaws.com'; $this->s3()->registerStreamWrapper(); stream_context_set_option( stream_context_get_default(), 's3', 'ACL', Aws\S3\Enum\CannedAcl::PUBLIC_READ ); @@ -45,7 +48,7 @@ public function filter_upload_dir( $dirs ) { } public function get_s3_url() { - return 'https://' . strtok( $this->bucket, '/' ) . '.s3.amazonaws.com' . substr( $this->bucket, strlen( strtok( $this->bucket, '/' ) ) ); + return $this->bucket_hostname . substr( $this->bucket, strlen( strtok( $this->bucket, '/' ) ) ); } public function get_original_upload_dir() {