PHP code example of medz / oss-stream-wrapper

1. Go to this page and download the library: Download medz/oss-stream-wrapper 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/ */

    

medz / oss-stream-wrapper example snippets


use Medz\Component\StreamWrapper\AliyunOSS\AliyunOSS;

#  如果使用的是medz/aliyun-oss这个包
# use Medz\Component\AliyunOSS\AliyunOSS;

$accessKeyId = "<您从OSS获得的AccessKeyId>"; ;
$accessKeySecret = "<您从OSS获得的AccessKeySecret>";
$endpoint = "<您选定的OSS数据中心访问域名,例如oss-cn-hangzhou.aliyuncs.com>";
$bucket = "<您使用的Bucket名字,注意命名规范>";
$oss = new AliyunOSS($$accessKeyId, $$accessKeySecret, $$endpoint);
$oss->setBucket($bucket);

// 重要步骤,注册流协议(可以注册多个)
$oss->registerStreamWrapper('oss'); // 默认流协议注册的是 oss:// 这个是可以自定义的,就在这里,定义你希望的流协议前缀。

file_put_contents('oss://demo/test.txt', 'This is a test content.');

// 上面我们已经注册了流协议,所以这里直接写重点代码。
use Symfony\Component\Finder\Finder;

$finder = new Finder();

$path = 'oss://demo'; //拟定一个已经存在的目录,如果不存在也没关系,流协议中已经支持了目录存在与否的判断,是本地文件一样,没有任何区别。
$finder->files()->in($path);

foreach ($finder as $file) {
  var_dump($file); // Symfony\Component\Finder\SplFileInfo
}