libratechie / flysystem-aliyun
Aliyun OSS Flysystem Adapter
v1.0.0
2024-08-07 06:58 UTC
Requires
- php: >=8.2
- aliyuncs/oss-sdk-php: ^2.7
- league/flysystem: ^3.28
Requires (Dev)
- mockery/mockery: ^1.6
- phpunit/phpunit: ^11.2
This package is auto-updated.
Last update: 2025-03-07 08:25:01 UTC
README
A Flysystem adapter for Aliyun OSS.
Requirements
PHP
>= 8.2league/flysystem
^3.28aliyuncs/oss-sdk-php
^2.7
Installation
You can install the package via Composer:
composer require libratechie/flysystem-aliyun
Configuration
To use the Aliyun adapter, you need to configure it with your Aliyun OSS credentials and settings.
use League\Flysystem\Filesystem; use Libratechie\Flysystem\Aliyun\AliyunAdapter; $accessKeyId = 'your-access-id'; $accessKeySecret = 'your-access-key'; $bucket = 'your-bucket-name'; $endpoint = 'your-endpoint'; // e.g., oss-cn-guangzhou.aliyuncs.com $adapter = new AliyunAdapter($accessKeyId, $accessKeySecret, $endpoint, $bucket); $config = [ 'public_url' => 'your-public-url', // e.g., https://your-bucket-name.oss-cn-guangzhou.aliyuncs.com ]; $filesystem = new Filesystem($adapter, $config);
Usage
Here are some examples of how to use the filesystem with the Aliyun adapter:
Checking
// Checking if a File Exists $filesystem->fileExists('path/to/file.txt'); // Checking if a Directory Exists $filesystem->directoryExists('path/to'); // Checking if a File or Folder Exists $filesystem->directoryExists('path/to/file.txt');
Write
// Writing a File $filesystem->write('path/to/file.txt', 'contents'); // Write Use writeStream $stream = fopen('local/path/to/file.txt', 'r+'); $filesystem->writeStream('path/to/file.txt', $stream); // Create a directory $filesystem->createDirectory('path/to/directory'); // Move a file $filesystem->rename('path/to/file.txt', 'new/path/to/file.txt'); // Copy a file $filesystem->copy('path/to/file.txt', 'new/path/to/file.txt');
Visibility
// Set the visibility of a file to 'public' $filesystem->setVisibility('path/to/file.txt', 'public'); // Get the visibility of a file // default: Inherits Bucket permissions. The read/write permissions of individual files are determined by the Bucket's read/write permissions. // private: Private. All access operations to the file require authentication. // public-read: Public read. Write operations require authentication, but the file can be read anonymously. // public-read-write: Public read/write. Anyone (including anonymous visitors) can read and write the file. $visibility = $filesystem->visibility('path/to/file.txt');
Read
// Listing Contents of a Directory $contents = $filesystem->listContents('path/to/directory', true); foreach ($contents as $object) { echo $object['type'] . ': ' . $object['path'] . PHP_EOL; } // Reading a File $contents = $filesystem->read('path/to/file.txt'); // Get the last modified time of a file $lastModified = $filesystem->lastModified('path/to/file.txt'); // Get the file size $fileSize = $filesystem->fileSize('path/to/file.txt'); // Get the mime type of file $mimeType = $filesystem->mimeType('path/to/file.txt'); // Assuming 'public_url' is configured in the $config array // Generate a public URL for a file $publicUrl = $this->filesystem->publicUrl($path); // Sign URL with specified expiration time in seconds and HTTP method. // The signed URL could be used to access the object directly. $expiresAt = new DateTimeImmutable('+1 hour'); $privateUrl = $this->filesystem->temporaryUrl($path, $expiresAt);
Delete
// Deleting a File $filesystem->delete('path/to/file.txt'); // Deleting a Directory $filesystem->deleteDirectory('path/to');