Skip to content

Commit

Permalink
Merge pull request #3 from humanmade/bucket-hostname
Browse files Browse the repository at this point in the history
Support for setting a custom hostname for the bucket
  • Loading branch information
joehoyle committed Mar 4, 2014
2 parents 3fab3d4 + d1a8dc4 commit b076df3
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions inc/class-s3-uploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class S3_Uploads {

private static $instance;
private $bucket;
private $bucket_hostname;
private $key;
private $secret;

Expand All @@ -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 );
Expand All @@ -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() {
Expand Down

0 comments on commit b076df3

Please sign in to comment.