Commit 1660d0b9 authored by Joe Hoyle's avatar Joe Hoyle
Browse files

Merge pull request #29 from humanmade/instance-profile-support

Support for using the ec2 instance profile
parents e18fe275 be6fa1ee
Loading
Loading
Loading
Loading
+15 −2
Original line number Diff line number Diff line
@@ -17,7 +17,13 @@ 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, defined( 'S3_UPLOADS_BUCKET_URL' ) ? S3_UPLOADS_BUCKET_URL : null, defined( 'S3_UPLOADS_REGION' ) ? S3_UPLOADS_REGION : null );

			$key    = defined( 'S3_UPLOADS_KEY' ) ? S3_UPLOADS_KEY : null;
			$secret = defined( 'S3_UPLOADS_SECRET' ) ? S3_UPLOADS_SECRET : null;
			$url    = defined( 'S3_UPLOADS_BUCKET_URL' ) ? S3_UPLOADS_BUCKET_URL : null;
			$region = defined( 'S3_UPLOADS_REGION' ) ? S3_UPLOADS_REGION : null;

			self::$instance = new S3_Uploads( S3_UPLOADS_BUCKET, $key, $secret, $url, $region );
		}

		return self::$instance;
@@ -124,13 +130,20 @@ class S3_Uploads {
		if ( ! empty( $this->s3 ) )
			return $this->s3;

		$params = array( 'key' => $this->key, 'secret' => $this->secret );
		$params = array();

		if ( $this->key && $this->secret ) {
			$params['key'] = $this->key;
			$params['secret'] = $this->secret;
		}

		if ( $this->region ) {
			$params['signature'] = 'v4';
			$params['region'] = $this->region;
		}

		$params = apply_filters( 's3_uploads_s3_client_params', $params );

		$this->s3 = Aws\Common\Aws::factory( $params )->get( 's3' );

		return $this->s3;
+5 −1
Original line number Diff line number Diff line
@@ -17,7 +17,11 @@ if ( defined( 'WP_CLI' ) && WP_CLI ) {
add_action( 'plugins_loaded', 's3_uploads_init' );

function s3_uploads_init() {
	if ( ! defined( 'S3_UPLOADS_BUCKET' ) || ! defined( 'S3_UPLOADS_KEY' ) || ! defined( 'S3_UPLOADS_SECRET' ) ) {
	if ( ! defined( 'S3_UPLOADS_BUCKET' ) ) {
		return;
	}

	if ( ( ! defined( 'S3_UPLOADS_KEY' ) || ! defined( 'S3_UPLOADS_SECRET' ) ) && ! defined( 'S3_UPLOADS_USE_INSTANCE_PROFILE' ) ) {
		return;
	}

+13 −6
Original line number Diff line number Diff line
@@ -45,19 +45,26 @@ class Test_S3_Uploads extends WP_UnitTestCase {
		$this->assertFalse( in_array( 's3', stream_get_wrappers() ) );
	}

	public function s3_uploads_enabled() {
	public function test_s3_uploads_enabled() {

		$this->assertTrue( s3_uploads_is_enabled() );
		$this->assertTrue( s3_uploads_enabled() );

		update_option( 's3_uploads_enabled', 'enabled' );
		$this->assertTrue( s3_uploads_is_enabled() );
		$this->assertTrue( s3_uploads_enabled() );

		delete_option( 's3_uploads_enabled' );
		define( 'S3_UPLOADS_AUTOENABLE', false );

		$this->assertFalse( s3_uploads_is_enabled() );
		$this->assertFalse( s3_uploads_enabled() );

		delete_option( 's3_uploads_enabled', 'enabled' );
		$this->assertTrue( s3_uploads_is_enabled() );
		update_option( 's3_uploads_enabled', 'enabled' );
		$this->assertTrue( s3_uploads_enabled() );
	}

	public function test_get_s3_client() {

		$s3 = S3_Uploads::get_instance()->s3();

		$this->assertInstanceOf( 'Aws\\S3\\S3Client', $s3 );
	}
}