PHP code example of bingher / obs

1. Go to this page and download the library: Download bingher/obs 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/ */

    

bingher / obs example snippets




use bingher\obs\OBS;
use bingher\obs\DriverType;

// 配置参数
$config = [
    'type'      => DriverType::MIN_IO, // 使用DriverType枚举
    'endpoint'  => 'http://localhost:9000',
    'key'       => 'minioadmin',
    'secret'    => 'minioadmin',
    'bucket'    => 'test-bucket',
    'region'    => 'us-east-1',
    'ssl_verify' => false, // 开发环境可设置为false
];

// 创建OBS客户端实例
$obs = new OBS($config);

// 上传文件
$localFilePath = '/path/to/local/file.txt';
$objectKey = 'remote/file.txt';
$acl = 'public-read'; // 可选: private, public-read
$result = $obs->put($objectKey, $localFilePath, $acl);
var_dump($result); // bool

// 获取文件
$downloadPath = '/path/to/download/file.txt';
$result = $obs->get($objectKey, $downloadPath);
var_dump($result); // bool

// 获取文件URL
$url = $obs->url($objectKey, 3600); // 有效期3600秒
var_dump($url); // string

// 检查文件是否存在
$exists = $obs->exist($objectKey);
var_dump($exists); // bool

// 删除文件
$deleted = $obs->delete($objectKey);
var_dump($deleted); // bool

// 获取预授权上传链接
$putUrlInfo = $obs->putUrl($objectKey, 'text/plain', 3600);
var_dump($putUrlInfo); // array

// 创建后切换驱动类型
$obs->setDriver(DriverType::ALI_OSS);

// 也可以直接使用字符串(不推荐)
// $obs->setDriver('AliOSS');

use bingher\obs\DriverType;

// 枚举常量
DriverType::ALI_OSS    // 阿里云OSS
DriverType::COS        // 腾讯云COS
DriverType::HW_OBS     // 华为云OBS
DriverType::MIN_IO     // MinIO
DriverType::RUST_FS    // RustFS
DriverType::S3         // AWS S3
DriverType::SEAWEED_FS // SeaweedFS

// 枚举方法
DriverType::all()      // 获取所有驱动类型和标签的映射
DriverType::isValid()  // 验证驱动类型是否有效
DriverType::getValues() // 获取所有驱动类型值
DriverType::getLabel() // 获取驱动类型的中文标签
shell
# 使用阿里云OSS
echo '安装阿里云OSS驱动' && composer uire qcloud/cos-sdk-v5

# 使用华为云OBS
echo '安装华为云OBS驱动' && composer 
shell
   php run_test.php tests/DriverTypeTest.php  # 运行特定测试
   # 或
   vendor/bin/phpunit  # 运行所有测试