Working example Example :
<?php
set_include_path(".:/home/whyceffy/public_html/fam/lib");
require 'aws/autoloader.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
echo '<form action="https://whyceeyes.com/fam/test.php" method="post" enctype="multipart/form-data"> <input type="FILE" name="filetoupload"><input type="submit" name="submit" value="Upload Now"></form>';
function handle_file_upload($params, $userID = NULL) {
try{
//Create a S3Client
$s3 = new S3Client(array(
//'profile' => 'default', donot use this if credential user here below
'region' => 'ap-southeast-1',
'version' => '2006-03-01',
'credentials' => array(
'key' => 'Access key ID of IAM',
'secret' => 'this password from users secret'
)
));
if (empty($_FILES)) {
return false;
}
if (!is_uploaded_file($_FILES[$params]['tmp_name'])) {
return false;
}
$sourcePath = $_FILES[$params]['tmp_name'];
$bucket = 'mefm';
$keyname = 'resource/' . $userID . '-' . date('y-m-d-H-i-s') . '-' . $_FILES[$params]['name'];
$result = $s3->putObject([
'Bucket' => $bucket,
'Key' => $keyname,
'ACL' => 'public-read',
'//Body' => 'Hello, world!',
'SourceFile' =>$sourcePath,
]);
if($result["@metadata"]["statusCode"] === 200){
return $result['ObjectURL'];
}else{
return false;
}
} catch (S3Exception $e) {
return false;
}
}
$s3url = handle_file_upload("filetoupload",'me');
if($s3url){
echo $s3url;
}
===============================
if key is not properly defined
in
$result = $s3->putObject([
'Bucket' => $bucket,
'Key' => $keyname,
'ACL' => 'public-read',
'//Body' => 'Hello, world!',
'SourceFile' =>$sourcePath,
]);
then
PHP Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Found 1 error while validating the input provided for the PutObject operation:
[Key] is missing and is a required parameter' in /home/public_html/fam/lib/aws/Aws/Api/Validator.php:65
Stack trace:
===================================
No comments:
Post a Comment