Provides common AWS functionality in a low-level and therefore lightweight library (less than 3% of the size of the official PHP SDK!)
use com\amazon\aws\{Credentials, ServiceEndpoint};
use util\Secret;
use util\cmd\Console;
use util\log\Logging;
$credentials= new Credentials($accessKey, new Secret($secretKey));
$api= (new ServiceEndpoint('lambda', $credentials))->in('eu-central-1')->version('2015-03-31');
$api->setTrace(Logging::all()->toConsole());
$r= $api->resource('/functions/greet/invocations')->transmit(['name' => getenv('USER')]);
Console::writeLine($r);
Console::writeLine($r->value());
AWS credentials are stored in various places, depending on the runtime environment. The CredentialProvider class supports the following:
AWS_ACCESS_KEY_ID
, AWS_SECRET_ACCESS_KEY
and (if present) AWS_SESSION_TOKEN
~/.aws/config
and (if present) ~/.aws/credentials
(honoring alternative locations set via environment variables)See https://docs.aws.amazon.com/sdkref/latest/guide/standardized-credentials.html
use com\amazon\aws\{ServiceEndpoint, CredentialProvider};
use util\cmd\Console;
$s3= (new ServiceEndpoint('s3', CredentialProvider::default()))
->in('eu-central-1')
->using('my-bucket')
;
$link= $s3->sign('/path/to/resource.png', timeout: 180);
Console::writeLine($link);
use com\amazon\aws\api\SignatureV4;
use com\amazon\aws\{ServiceEndpoint, CredentialProvider};
use io\File;
use util\cmd\Console;
$s3= (new ServiceEndpoint('s3', CredentialProvider::default()))
->in('eu-central-1')
->using('my-bucket')
;
$file= new File('large.txt');
$file->open(File::READ);
try {
$transfer= $s3->open('PUT', 'target/'.$file->filename, [
'x-amz-content-sha256' => SignatureV4::UNSIGNED, // Or calculate from file
'Content-Type' => 'text/plain',
'Content-Length' => $file->size(),
]);
while (!$file->eof()) {
$transfer->write($file->read());
}
$response= $transfer->finish();
Console::writeLine($response);
} finally {
$file->close();
}
See https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html:
use com\amazon\aws\{ServiceEndpoint, CredentialProvider};
use util\cmd\Console;
$model= 'anthropic.claude-3-5-sonnet-20240620-v1:0';
$runtime= (new ServiceEndpoint('bedrock', CredentialProvider::default()))
->using('bedrock-runtime.')
->in('eu-central-1')
;
$response= $runtime->resource('/model/{0}/converse-stream', [$model])->transmit([
'system' => [['text' => 'Use informal language']],
'messages' => [
['role' => 'user', 'content' => [['text' => $argv[1]]]],
],
'inferenceConfig' => [
'maxTokens' => 1000,
'temperature' => 0.5,
],
]);
foreach ($response->events() as $event) {
Console::writeLine($event->header(':event-type'), ': ', $event->value());
}