2
I am developing an application that sends an image to a Bucket on Amazon and I am using the 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);
}
}
Turns out my API
has more than one function in the same file, type change photo, change basic information, unsubscribe, change password and so on.
That’s why I chose to keep the require
to be executed only inside the photo change function, but was directly receiving the error:
Unexpected 'use' error (T_USE)
After researching about I discovered that the namespace
use
can only be declared no escopo mais externo de um arquivo
.
Source: Unexpected 'use' (T_USE) error when using autoload
Okay, I put the use
in the outermost scope, more or less so:
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, only I wanted to know if it affects my other functions? In charging time mainly, since the Amazon sdk has 6MB.
Which version of php vc uses?
– rray
PHP Version 7.0.14-1
Running on an Ubuntu server– MarceloBoni