Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
namespace App\Model;
use Nette;
use Aws\S3\S3Client;
class S3Uploader
/** @var \Aws\S3\S3Client */
private $s3client;
/** @var string */
private $bucket;
public function __construct($bucket, $accessKey, $secretKey)
$this->s3client = new \Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-west-2'
$this->bucket = $bucket;
* Upload a file to an S3 bucket
* @param string $key The key used for the uploaded object
* @param string $file The filename to be uploaded
* @param string $contentType The file's content type. This defaults to "application/octet-stream"
* @return string A URL to access the file publically.
public function uploadPublic ($key, $file, $contentType = 'application/octet-stream')
$result = $this->s3client->putObject([
'Bucket' => $this->bucket,
'Key' => $key,
'SourceFile' => $file,
'ContentType' => $contentType,
'ACL' => 'public-read'
$url = $this->s3client->getObjectUrl($this->bucket, $key);
return $url;
public function getClient () {
return $this->s3client;
public function getBucket () {
return $this->bucket;

This comment has been minimized.

Copy link
Owner Author

@pymander pymander commented Oct 26, 2016

Here's the blog post where I talk about using this file:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment