Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add S3 temporary URLs #156

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Description:
* **Bucket**: The bucket where you wish to store your objects. Every object in Amazon S3 is stored in a bucket. If the specified bucket doesn't exist Stapler will attempt to create it. The bucket name will not be interpolated.
* **ACL**: This is a string/array that should be one of the canned access policies that S3 provides (private, public-read, public-read-write, authenticated-read, bucket-owner-read, bucket-owner-full-control). The default for Stapler is public-read. An associative array (style => permission) may be passed to specify permissions on a per style basis.
* **path**: This is the key under the bucket in which the file will be stored. The URL will be constructed from the bucket and the path. This is what you will want to interpolate. Keys should be unique, like filenames, and despite the fact that S3 (strictly speaking) does not support directories, you can still use a / to separate parts of your file name.
* **s3_object_url_expires**: This is a string representing a default expiration time for object URLs. This adheres to the AWS expiration format (eg. +1 minutes) as described [here](http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_getObjectUrl). This will almost always be used in conjunction with a 'private' ACL.

Default values:
* **s3_client_config**
Expand All @@ -79,4 +80,5 @@ Default values:
* **s3_object_config**
* **Bucket**: ''
* **ACL**: 'public-read'
* **path**: ':attachment/:id/:style/:filename'
* **path**: ':attachment/:id/:style/:filename'
* **s3_object_url_expires**: null
21 changes: 20 additions & 1 deletion src/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,26 @@ public function __call($method, $parameters)
public function url($styleName = '')
{
if ($this->originalFilename()) {
return $this->storageDriver->url($styleName, $this);
return $this->storageDriver->url($styleName, null, $this);
}

return $this->defaultUrl($styleName);
}

/**
* Generates a temporary url to an uploaded file (or a resized version of it).
* AWS Documentation for $expires format at
* http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_getObjectUrl
*
* @param string $expires
* @param string $styleName
*
* @return string
*/
public function tempUrl($expires, $styleName = '')
{
if ($this->originalFilename()) {
return $this->storageDriver->url($styleName, $expires, $this);
}

return $this->defaultUrl($styleName);
Expand Down
14 changes: 13 additions & 1 deletion src/Interfaces/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ public function setQueuedForDeletion($array);
*/
public function url($styleName = '');

/**
* Generates a temporary url to an uploaded file (or a resized version of it).
* AWS Documentation for $expires format at
* http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_getObjectUrl
*
* @param string $expires
* @param string $styleName
*
* @return string
*/
public function tempUrl($expires, $styleName = '');

/**
* Generates the file system path to an uploaded file (or a resized version of it).
* This is used for saving files, etc.
Expand Down Expand Up @@ -252,4 +264,4 @@ public function instanceWrite($property, $value);
* attributes.
*/
public function clearAttributes();
}
}
5 changes: 3 additions & 2 deletions src/Interfaces/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ interface Storage
* Return the url for a file upload.
*
* @param string $styleName
* @param string $expires
*
* @return string
*/
public function url($styleName);
public function url($styleName, $expires = null);

/**
* For filesystem storage this method returns the path (on disk) of a file upload.
Expand Down Expand Up @@ -39,4 +40,4 @@ public function remove(array $filePaths);
* @param string $filePath
*/
public function move($file, $filePath);
}
}
3 changes: 2 additions & 1 deletion src/Storage/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ public function __construct(Attachment $attachedFile)
* Return the url for a file upload.
*
* @param string $styleName
* @param string $expires
*
* @return string
*/
public function url($styleName)
public function url($styleName, $expires = null)
{
return $this->attachedFile->getInterpolator()->interpolate($this->attachedFile->url, $this->attachedFile, $styleName);
}
Expand Down
9 changes: 7 additions & 2 deletions src/Storage/S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ public function __construct(Attachment $attachedFile, S3Client $s3Client)
* Return the url for a file upload.
*
* @param string $styleName
* @param string $expires
*
* @return string
*/
public function url($styleName)
public function url($styleName, $expires = null)
{
return $this->s3Client->getObjectUrl($this->attachedFile->s3_object_config['Bucket'], $this->path($styleName), null, ['PathStyle' => true]);
if (!$expires) {
$expires = $this->attachedFile->s3_object_url_expires;
}

return $this->s3Client->getObjectUrl($this->attachedFile->s3_object_config['Bucket'], $this->path($styleName), $expires, ['PathStyle' => true]);
}

/**
Expand Down