Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
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)
{
putenv("AWS_ACCESS_KEY_ID=$accessKey");
putenv("AWS_SECRET_ACCESS_KEY=$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;
}
}
@pymander

This comment has been minimized.

Copy link
Owner Author

commented Oct 26, 2016

Here's the blog post where I talk about using this file: https://arnesonium.com/2016/10/using-amazon-s3-as-a-nette-service/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.