Skip to content

Instantly share code, notes, and snippets.

@dudekm
Created November 8, 2022 20:29
Show Gist options
  • Save dudekm/ccb06aafb7e2309497fe63cd571c08c3 to your computer and use it in GitHub Desktop.
Save dudekm/ccb06aafb7e2309497fe63cd571c08c3 to your computer and use it in GitHub Desktop.
ASYNC-AWS
<?php
declare(strict_types=1);
require_once __DIR__.'/vendor/autoload.php';
use AsyncAws\S3\S3Client;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
$logger = new ConsoleLogger(new ConsoleOutput(OutputInterface::VERBOSITY_VERY_VERBOSE));
$s3 = new S3Client([
'accessKeyId' => 'access-key',
'accessKeySecret' => 'secret-key',
'region' => 'region-key',
'pathStyleEndpoint' => true,
'endpoint' => 'http://127.0.0.1:4566',
], null, null, $logger);
$file = fopen('new.txt', 'r');
//$file = 'new.txt';
buckets($s3);
files($s3);
upload($s3, $file);
function upload(S3Client $s3, $file)
{
echo "<h1>Uploaded file</h1>";
try {
$responses[] = $s3->putObject([
'Bucket' => 'demo-bucket',
'Key' => 'new-key-file',
'Body' => $file,
])->resolve(5);
} catch (\Throwable $throwable) {
echo $throwable->getMessage();
}
}
function buckets(S3Client $s3)
{
echo "<h1>Buckets</h1>";
try {
$buckets = $s3->listBuckets();
echo "<pre>";
foreach ($buckets->getIterator() as $bucket) {
print_r($bucket);
}
echo "</pre>";
} catch (Throwable $throwable) {
echo $throwable->getMessage();
}
}
function files(S3Client $s3)
{
echo "<h1>Files</h1>";
try {
$objects = $s3->listObjectsV2([
'Bucket' => 'demo-bucket',
]);
echo "<pre>";
foreach ($objects->getIterator() as $object) {
print_r($object);
}
echo "</pre>";
} catch (Throwable $throwable) {
echo $throwable->getMessage();
}
}
php async.php
<h1>Buckets</h1><pre>AsyncAws\S3\ValueObject\Bucket Object
(
[name:AsyncAws\S3\ValueObject\Bucket:private] => demo-bucket
[creationDate:AsyncAws\S3\ValueObject\Bucket:private] => DateTimeImmutable Object
(
[date] => 2022-11-08 20:16:53.000000
[timezone_type] => 2
[timezone] => Z
)
)
AsyncAws\S3\ValueObject\Bucket Object
(
[name:AsyncAws\S3\ValueObject\Bucket:private] => demo-bucket2
[creationDate:AsyncAws\S3\ValueObject\Bucket:private] => DateTimeImmutable Object
(
[date] => 2022-11-08 20:16:55.000000
[timezone_type] => 2
[timezone] => Z
)
)
</pre><h1>Files</h1><pre></pre><h1>Uploaded file</h1>[info] Try #1 after 1091ms: Idle timeout reached for "http://127.0.0.1:4566/demo-bucket/new-key-file".
[info] Try #2 after 2130ms: Idle timeout reached for "http://127.0.0.1:4566/demo-bucket/new-key-file".
[info] Try #3 after 3859ms: Idle timeout reached for "http://127.0.0.1:4566/demo-bucket/new-key-file".
[error] Could not contact remote server.
Could not contact remote server.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment