Skip to content

Instantly share code, notes, and snippets.

@pymander
Created October 26, 2016 21:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pymander/a027523a7b9152660fac8e7bb4801c91 to your computer and use it in GitHub Desktop.
Save pymander/a027523a7b9152660fac8e7bb4801c91 to your computer and use it in GitHub Desktop.
<?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
Copy link
Author

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