Commit 1f9abd30 authored by Joe Hoyle's avatar Joe Hoyle
Browse files

Merge pull request #16 from TheCodeCompany/verify-command

Adding a WP-CLI Verify command
parents 84bac6d4 6cc32f3f
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -20,6 +20,13 @@ define( 'S3_UPLOADS_KEY', '' );
define( 'S3_UPLOADS_SECRET', '' );
```

The next thing that you should do is to verify your setup. You can do this using the `verify` command
like so:

```
wp s3-uploads verify
```

You'll want to create a new IAM user for the S3-Uploads plugin, so you are not using admin level access keys on your site. S3-Uploads can create the IAM user for you and asign the correct permissions.

```
+58 −11
Original line number Diff line number Diff line
@@ -2,6 +2,53 @@

class S3_Uploads_WP_CLI_Command extends WP_CLI_Command {

	/**
	 * Verifies the API keys entered will work for writing and deleting from S3.
	 *
	 * @subcommand verify
	 */
	public function verify_api_keys() {

		S3_Uploads::get_instance(); // Boot

		$upload_dir = wp_upload_dir();

		// The upload file location on the local filesystem
		$s3_path = $upload_dir['basedir'] . '/' . mt_rand() . '.jpg';

		// Attempt to copy the file to S3
		WP_CLI::print_value( 'Attempting to upload file '. $s3_path );
		
		// Copy canola from the test dir, upto S3
		$copy = copy(
			dirname( dirname(__FILE__) ) . '/tests/data/canola.jpg',
			$s3_path
		);
        
		// Check that copy worked
		if ( ! $copy ) {
			WP_CLI::error( 'Failed to copy / write to S3 - check your policy?' );
			return;
		}

		WP_CLI::print_value( 'File uploaded to S3 successfully' );

		// Delete off S3
		WP_CLI::print_value( 'Attempting to delete file '. $s3_path );
		$delete = unlink( $s3_path );

		// Check that delete worked
		if ( ! $delete ) {
			WP_CLI::error( 'Failed to delete '. $s3_path );
			return;
		}

		WP_CLI::print_value( 'File deleted from S3 successfully' );

		WP_CLI::success( 'Looks like your configuration is correct.' );

	}

	/**
	 * @subcommand migrate-attachments
	 * @synopsis [--delete-local]