Commit c00c5da4 authored by Joe Hoyle's avatar Joe Hoyle
Browse files

Merge pull request #17 from TheCodeCompany/filter-put-object

Add filter for modifying S3 Object Paramaters
parents f2c2742b dd689bd2
Loading
Loading
Loading
Loading
+22 −2
Original line number Diff line number Diff line
@@ -61,10 +61,30 @@ wp s3-uploads upload-directory <from> <to> [--sync] [--dry-run]

Passing `--sync` will only upload files that are newer in `<from>` or that don't exist on S3 already. Use `--dry-run` to test.

There is also an all purpose `cp` command for arbitraty copying to and from S3. 
There is also an all purpose `cp` command for arbitrary copying to and from S3.

```
wp s3-uploads cp <from> <to>
```

Note: as either `<from>` or `<to>` can be S3 or local locations, you must speficy the full S3 location via `s3://mybucket/mydirectory` for example `cp ./test.txt s3://mybucket/test.txt`.

Cache Control
==========

You can define the default HTTP `Cache-Control` header for uploaded media using the
following constant:

```PHP
define( 'S3_UPLOADS_CACHE_CONTROL', 30 * 24 * 60 * 60 );
	// will expire in 30 days time
```

You can also configure the `Expires` header using the `S3_UPLOADS_EXPIRES` constant
For instance if you wanted to set an asset to effectively not expire, you could
set the Expires header way off in the future.  For example:

```PHP
define( 'S3_UPLOADS_EXPIRES', gmdate( 'D, d M Y H:i:s', time() + (10 * 365 * 24 * 60 * 60) ) .' GMT' );
	// will expire in 10 years time
```
+31 −1
Original line number Diff line number Diff line
@@ -17,6 +17,36 @@ class S3_Uploads_Stream_Wrapper extends Aws\S3\StreamWrapper {
		static::$client = $client;
	}

	// Override
	public function stream_flush() {

		/// Expires:
		if ( defined( 'S3_UPLOADS_HTTP_EXPIRES' ) ) {
			$this->params[ 'Expires' ] = S3_UPLOADS_HTTP_EXPIRES;
		}

		// Cache-Control:
		if ( defined( 'S3_UPLOADS_HTTP_CACHE_CONTROL' ) ) {
			if ( is_numeric( S3_UPLOADS_HTTP_CACHE_CONTROL ) ) {
				$this->params[ 'CacheControl' ] = 'max-age='. S3_UPLOADS_HTTP_CACHE_CONTROL;
			} else {
				$this->params[ 'CacheControl' ] = S3_UPLOADS_HTTP_CACHE_CONTROL;
			}
		}

		/**
		 * Filter the parameters passed to S3
		 * Theses are the parameters passed to S3Client::putObject()
		 * See; http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_putObject
		 *
		 * @param array $params S3Client::putObject paramteres.
		 */
		$this->params = apply_filters( 's3_uploads_putObject_params', $this->params );

		return parent::stream_flush();

	}

	public function stream_metadata( $path, $option, $value ) {
		// not implemented
	}