Created
October 26, 2016 21:56
-
-
Save pymander/a027523a7b9152660fac8e7bb4801c91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the blog post where I talk about using this file: https://arnesonium.com/2016/10/using-amazon-s3-as-a-nette-service/