PHP code example of reactphp-x / filesystem-s3
1. Go to this page and download the library: Download reactphp-x/filesystem-s3 library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
reactphp-x / filesystem-s3 example snippets
eactphpX\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";
});