I want to upload image files directly to S3 without storing them on my server (for security). How can I do that with the PHP SDK from AWS S3? here is an example code:
<?php
require_once '/var/www/site/vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = '<your bucket name>';
$keyname = 'sample';
$filepath = '/path/to/image.jpg';
// Instantiate the client.
$s3 = S3Client::factory(array(
'key' => 'your AWS access key',
'secret' => 'your AWS secret access key'
));
try {
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ACL' => 'public-read'
));
echo 'Success';
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}
Here is the upload form:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="image">
<input type="submit" value="Upload Image" name="submit">
</form>
What would I put for $filepath in the PHP code since I don't want to store it on my server for security reason (I don't want it to execute malicious code and stuff like that)? Any help would be appreciated thanks!