-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsconnect.php
76 lines (60 loc) · 1.75 KB
/
awsconnect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/*
* S3 Upload
* Simple upload that connects to the S3 bucket and uploads a file
*
* @author akastl<anja.kastl@gmail.com>
*/
require(dirname(dirname(dirname(__FILE__))) . "/aws/aws-autoloader.php");
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
class AwsConnect
{
public $s3;
public $s3Client;
public $uploadDir;
public function __construct($args = array())
{
$this->s3 = S3Client::factory($this->build_aws_config());
$this->s3Client = $this->s3->get('s3');
$this->uploadDir = '/maps/icons';
$this->bucket = 'assets.projects.newsday.com';
}
public function build_aws_config()
{
return array(
'key' => getenv('S3_KEY'),
'secret' => getenv('S3_SECRET'),
'region' => 'us-east-1'
);
}
//upload a series of keys to S3
public function push_to_s3($file, $args)
{
set_time_limit(120);
$errors = 0;
$filename = 'dot_'.$args['color'].'_'.$args['text'].'.png';
$remoteFile = $this->uploadDir. '/' .$filename;
try
{
// Upload data.
$marker = array(
'Bucket' => $this->bucket,
'Key' => $remoteFile,
'Body' => $file,
'ACL' => 'public-read'
);
$result = $this->s3->putObject($marker);
// Print the URL to the object.
// echo $result['ObjectURL'];
} catch (MultipartUploadException $e)
{
$uploader->abort();
echo "Upload failed.";
echo "<pre>".$e->getMessage()."</pre>";
$errors++;
}
return ($errors == 0) ? 'http://'.$this->bucket.$remoteFile : false;
}
}
?>