PHP code example of lonelywalkersource / flysystem-oss

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

    

lonelywalkersource / flysystem-oss example snippets


use Lonelywalksource\Flysystem\Oss\Config\OssConfig;
use Lonelywalksource\Flysystem\Oss\OssAdapter;

$config = OssConfig::fromArray([
    // ── Required ─────────────────────────────────────────

    // Alibaba Cloud AccessKey ID (alias: 'key')
    'access_key'     => 'your-access-key',

    // Alibaba Cloud AccessKey Secret (alias: 'secret')
    'secret_key'     => 'your-secret-key',

    // OSS endpoint, e.g. oss-cn-hangzhou.aliyuncs.com
    'endpoint'       => 'oss-cn-hangzhou.aliyuncs.com',

    // Bucket name
    'bucket'         => 'your-bucket',

    // ── Optional ─────────────────────────────────────────

    // Region (e.g. cn-hangzhou) — ───────

    // Whether to throw exceptions. Default: true
    'throw'          => true,

    // ── OssClient Extra Options ──────────────────────────
    // Any key not listed above is passed directly to the OssClient constructor.
    // Valid OssClient options 

$adapter = new OssAdapter($config);
$adapter->write('path/to/file.txt', 'Hello OSS', new \League\Flysystem\Config());

$client = new \OSS\OssClient([...]);
$adapter = new OssAdapter($config, $client);

$config = OssConfig::fromArray([
    'access_key' => 'your-access-key',
    'secret_key' => 'your-secret-key',
    'endpoint'   => 'oss-cn-hangzhou.aliyuncs.com',
    'bucket'     => 'default-bucket',
    'buckets'    => [
        'images' => [
            'bucket'   => 'images-bucket',
            'endpoint' => 'oss-cn-beijing.aliyuncs.com',
        ],
    ],
]);

$adapter = new OssAdapter($config);
$adapter->write('file.txt', 'goes to default bucket', new \League\Flysystem\Config());

$imagesAdapter = $adapter->bucket('images');
$imagesAdapter->write('photo.jpg', 'goes to images bucket', new \League\Flysystem\Config());

$adapter->setDomain('https://cdn.example.com');
echo $adapter->getUrl('file.txt'); // https://cdn.example.com/file.txt

$result = $adapter->generatePostPolicy([
    'expire' => 1800,
    'prefix' => 'uploads/',
]);
// $result['policy_token_json'] contains form fields for direct OSS upload

$url = $adapter->getTemporaryUrl('file.txt', 3600); // 1 hour

$result = $adapter->verifyCallback(
    authorizationBase64: $_SERVER['HTTP_AUTHORIZATION'] ?? '',
    pubKeyUrlBase64: $_SERVER['HTTP_X_OSS_PUB_KEY_URL'] ?? '',
    path: $_SERVER['REQUEST_URI'] ?? '',
    body: file_get_contents('php://input'),
);
[$ok, $data] = $result;

use Lonelywalksource\Flysystem\Oss\Signer\CallbackSigner;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;

$signer = new CallbackSigner($httpClient, $requestFactory);
$result = $signer->verify($authorization, $pubKeyUrl, $path, $body);