reactphp-x / filesystem-s3
v1.0.0
2025-03-22 02:10 UTC
Requires
- aws/aws-sdk-php: ^3.0
- react/http: ^1.11
- wpjscc/filesystem: ^0.2.0
This package is auto-updated.
Last update: 2025-03-22 02:11:37 UTC
README
install
composer require reactphp-x/filesystem-s3 -vvv
usage
<?php require __DIR__.'/../vendor/autoload.php'; use ReactphpX\FilesystemS3\Adapter; use React\EventLoop\Loop; use React\Filesystem\Node\FileInterface; use React\Filesystem\Node\DirectoryInterface; // 初始化 S3 适配器 $bucket = 'xxxx'; $adapter = new Adapter([ 'endpoint' => 'xxxx', 'version' => 'latest', 'region' => 'us-east-1', 'use_path_style_endpoint' => true, 'credentials' => [ 'key' => 'xxx', 'secret' => 'xxxx', ], ], $bucket); // 示例:上传文件 $destinationPath = 'uploads/example.txt'; $adapter->file($destinationPath)->putContents('Hello World!')->then(function () use ($adapter, $destinationPath) { echo "File uploaded to S3: $destinationPath\n"; }, function ($error) { echo "Error uploading file: " . $error->getMessage() . "\n"; }); // 示例:读取文件 $adapter->file($destinationPath)->getContents()->then(function ($content) { echo "File content: $content\n"; }, function ($error) { echo "Error reading file: " . $error->getMessage() . "\n"; }); // 示例:删除文件 // $adapter->file($destinationPath)->unlink()->then(function () use ($adapter, $destinationPath) { // echo "File deleted from S3: $destinationPath\n"; // }, function ($error) { // echo "Error deleting file: " . $error->getMessage() . "\n"; // }); // 示例:列出目录 $adapter->directory('')->ls()->then(function ($nodes) { foreach ($nodes as $node) { if ($node instanceof FileInterface) { echo "File: " . $node->path() . "\n"; } elseif ($node instanceof DirectoryInterface) { echo "Directory: " . $node->path() . "\n"; } } }, function ($error) { echo "Error listing directory: " . $error->getMessage() . "\n"; });