· 6 years ago · Oct 31, 2019, 12:21 AM
1session_start();
2
3require 'vendor/autoload.php';
4
5use Aws\S3\S3Client;
6
7try {
8
9 // fires an exception if there is no data sent
10 if (!isset($_FILES['inputimg'])) {
11 throw new Exception("File not uploaded", 1);
12 }
13
14 // creates a client object, informing AWS credentials
15 $clientS3 = S3Client::factory(array(
16 'region' => 'us-east-2',
17 'version' => 'latest',
18 'key' => ACCESS_KEY,
19 'secret' => SECRET_KEY
20 ));
21
22 // putObject method sends data to the chosen bucket (in our case, teste-marcelo)
23 $response = $clientS3->putObject(array(
24 'Bucket' => "dms-midterm-raw",
25 'Key' => $_FILES['inputimg']['name'],
26 'SourceFile' => $_FILES['inputimg']['tmp_name'],
27 'ACL' => 'public-read',
28 ));
29
30 $_SESSION['msg'] = "Object successfully posted, address <a href='{$response['ObjectURL']}'>{$response['ObjectURL']}</a>";
31
32 header("location: index.php");
33
34 } catch(Exception $e) {
35 echo "Error > {$e->getMessage()}";
36 }