jiangwang / flysystem-tos
Flysystem adapter for the TOS (Volcengine Object Storage) storage service.
1.0.3
2024-01-15 13:28 UTC
Requires
- php: >=8.0.0
- ext-json: *
- guzzlehttp/guzzle: ^7.8
- league/flysystem: ^3.0
README
基于 League\Flysystem 的火山引擎 TOS (字节跳动对象存储) 适配器。
特性
- 完全兼容 Flysystem v3.0+ API
- 支持所有基本的文件操作(读取、写入、删除、复制、移动等)
- 支持文件和目录的可见性设置
- 支持流式上传和下载
- 支持文件元数据操作
- 内置错误处理和异常管理
安装
通过 Composer 安装:
composer require jiangwang/flysystem-tos
要求
- PHP >= 8.0
- League\Flysystem ^3.0
- ext-json
- guzzlehttp/guzzle ^7.8
使用方法
基本配置
<?php use League\Flysystem\Filesystem; use Jiangwang\FlysystemTos\TosAdapter; // 配置 TOS 客户端 $config = [ 'region' => 'your-region', 'endpoint' => 'https://tos-s3-cn-beijing.volces.com', 'accessKeyId' => 'your-access-key-id', 'accessKeySecret' => 'your-access-key-secret', 'bucket' => 'your-bucket-name', // 可选配置 'securityToken' => 'your-security-token', // 临时凭证 'connectionTimeout' => 30, 'socketTimeout' => 30, ]; // 创建适配器和文件系统 $adapter = new TosAdapter($config); $filesystem = new Filesystem($adapter);
文件操作
写入文件
// 写入字符串内容 $filesystem->write('path/to/file.txt', 'Hello, World!'); // 写入文件流 $stream = fopen('local/file.txt', 'r'); $filesystem->writeStream('path/to/file.txt', $stream); // 设置文件可见性 $filesystem->write('path/to/file.txt', 'content', [ 'visibility' => 'public' // 或 'private' ]);
读取文件
// 读取文件内容 $content = $filesystem->read('path/to/file.txt'); // 读取文件流 $stream = $filesystem->readStream('path/to/file.txt');
文件信息
// 检查文件是否存在 $exists = $filesystem->fileExists('path/to/file.txt'); // 获取文件大小 $size = $filesystem->fileSize('path/to/file.txt'); // 获取文件最后修改时间 $timestamp = $filesystem->lastModified('path/to/file.txt'); // 获取文件 MIME 类型 $mimeType = $filesystem->mimeType('path/to/file.txt'); // 获取文件可见性 $visibility = $filesystem->visibility('path/to/file.txt');
文件管理
// 复制文件 $filesystem->copy('source/file.txt', 'destination/file.txt'); // 移动文件 $filesystem->move('source/file.txt', 'destination/file.txt'); // 删除文件 $filesystem->delete('path/to/file.txt');
目录操作
// 创建目录 $filesystem->createDirectory('path/to/directory'); // 检查目录是否存在 $exists = $filesystem->directoryExists('path/to/directory'); // 列出目录内容 $contents = $filesystem->listContents('path/to/directory'); // 递归列出目录内容 $contents = $filesystem->listContents('path/to/directory', true); // 删除目录 $filesystem->deleteDirectory('path/to/directory');
可见性设置
// 设置文件为公开可读 $filesystem->setVisibility('path/to/file.txt', 'public'); // 设置文件为私有 $filesystem->setVisibility('path/to/file.txt', 'private');
配置选项
适配器支持以下配置选项:
配置项 | 类型 | 必需 | 描述 |
---|---|---|---|
region |
string | 是 | TOS 服务区域 |
endpoint |
string | 是 | TOS 服务端点 |
accessKeyId |
string | 是 | 访问密钥 ID |
accessKeySecret |
string | 是 | 访问密钥密码 |
bucket |
string | 是 | 存储桶名称 |
securityToken |
string | 否 | 临时访问凭证 |
connectionTimeout |
int | 否 | 连接超时时间(秒) |
socketTimeout |
int | 否 | 套接字超时时间(秒) |
许可证
本项目采用 MIT 许可证。详情请参阅 LICENSE 文件。
相关链接
作者
- jsogn - jbanxian@gmail.com
致谢
- League\Flysystem - 提供优秀的文件系统抽象
- 火山引擎 TOS - 提供可靠的对象存储服务