I'm developing an application that sends an image to a bucket in amazon and I'm using sdk pure
To send use the following code:
function especifica(){
require '..\..\lib\aws\aws-autoloader.php';
use Aws\S3\S3Client;
define("ACCESS_KEY_AMAZON", "");
define("SECRET_KEY_AMAZON", "");
try {
$clientS3 = S3Client::factory(array(
'region' => 'região',
'version' => 'latest',
'key' => ACCESS_KEY_AMAZON,
'secret' => SECRET_KEY_AMAZON
));
$response = $clientS3->putObject(array(
'Bucket' => "bucket",
'Key' => "profile_picture/".$_POST['idusuario'].".png",
'SourceFile' => $_POST['hidden_cropped'],
));
die(var_dump($response));
echo "Objeto postado com sucesso, endereco <a href='{$response['ObjectURL']}'>{$response['ObjectURL']}</a>";
} catch(Exception $e) {
log_errors( "Edit picture, upload to amazon S3".PHP_EOL."Exception: ".$e);
}
}
It happens that my API
has more than one function in the same file, type change photo, change basic information, cancel subscription, change password and so on.
So I preferred to keep require
to run only within the photo change function, but I was getting the error directly:
Unexpected error 'use' (T_USE)
After searching I found that namespace
use
can only be declared no escopo mais externo de um arquivo
.
Source: Unexpected 'use' error (T_USE) use autoload
Ok, I put use
in the outermost scope, more or less this way:
require '..\..\lib\aws\aws-autoloader.php';
use Aws\S3\S3Client;
function especifica(){
define("ACCESS_KEY_AMAZON", "");
define("SECRET_KEY_AMAZON", "");
try {
$clientS3 = S3Client::factory(array(
'region' => 'região',
'version' => 'latest',
'key' => ACCESS_KEY_AMAZON,
'secret' => SECRET_KEY_AMAZON
));
$response = $clientS3->putObject(array(
'Bucket' => "bucket",
'Key' => "profile_picture/".$_POST['idusuario'].".png",
'SourceFile' => $_POST['hidden_cropped'],
));
die(var_dump($response));
echo "Objeto postado com sucesso, endereco <a href='{$response['ObjectURL']}'>{$response['ObjectURL']}</a>";
} catch(Exception $e) {
log_errors( "Edit picture, upload to amazon S3".PHP_EOL."Exception: ".$e);
}
}
And it worked, but just wanted to know if it affects my other functions? In the loading time mainly, since the amazon sdk has 6MB.