royvoetman/flysystem-gitlab-storage
Flysystem adapter for GitLab storage using GitLab’s Repository Files API v4. Store and retrieve files from a GitLab project/branch via a simple client + adapter setup. Supports optional path prefixes and integrates with Laravel via a companion package.
Installation
composer require royvoetman/flysystem-gitlab-storage
Basic Usage
use League\Flysystem\Filesystem;
use Royvoetman\Flysystem\GitlabStorage\GitlabStorageAdapter;
$adapter = new GitlabStorageAdapter(
'https://gitlab.example.com',
'your_access_token',
'project_id_or_path'
);
$filesystem = new Filesystem($adapter);
First Use Case: Uploading a File
$filesystem->write('path/to/file.txt', 'file content');
Where to Look First
$filesystem->write('remote/path/file.txt', file_get_contents('local/file.txt'));
$content = $filesystem->read('remote/path/file.txt');
file_put_contents('local/file.txt', $content);
$filesystem->delete('remote/path/file.txt');
$filesystem->createDirectory('remote/path/dir');
$files = $filesystem->listContents('remote/path');
// config/filesystems.php
'disks' => [
'gitlab' => [
'driver' => 'gitlab',
'url' => env('GITLAB_URL'),
'token' => env('GITLAB_TOKEN'),
'project' => env('GITLAB_PROJECT'),
],
],
use Illuminate\Support\Facades\Storage;
Storage::disk('gitlab')->put('file.txt', 'content');
+. Ensure paths are URL-encoded properly:
$filesystem->write('remote/path/with spaces/file.txt', 'content');
The adapter now uses rawurlencode() internally for proper handling.try {
$filesystem->write('file.txt', 'content');
} catch (\League\Flysystem\FilesystemException $e) {
Log::error('GitLab upload failed: ' . $e->getMessage());
}
Authentication Issues
read_repository, write_repository).Project Path vs. ID
project_id and project_path (e.g., group/project).project_path for nested projects/groups.Rate Limiting
429 Too Many Requests gracefully:
try {
$filesystem->read('file.txt');
} catch (\League\Flysystem\FilesystemException $e) {
if ($e->getCode() === 429) {
sleep(1); // Retry after delay
}
}
File Size Limits
URL Encoding in Paths
+. The package now uses rawurlencode() instead of urlencode() for proper encoding.// Avoid this (manual encoding may cause issues):
$filesystem->write(rawurlencode('path/with spaces'), 'content');
// Let the adapter handle it:
$filesystem->write('path/with spaces', 'content');
Case Sensitivity
$filesystem->write('File.txt', 'content'); // May fail if 'file.txt' exists.
Enable Debugging
$client = new \GuzzleHttp\Client([
'debug' => fopen('guzzle.log', 'w'),
]);
$adapter = new GitlabStorageAdapter($client, ...);
Check API Responses
$adapter->getClient()->getConfig('debug', true);
Validate Tokens
curl --header "PRIVATE-TOKEN: <your_token>" https://gitlab.example.com/api/v4/user
Custom Headers
$adapter = new GitlabStorageAdapter(
'https://gitlab.example.com',
'token',
'project',
['headers' => ['X-Custom-Header' => 'value']]
);
Proxy Support
$client = new \GuzzleHttp\Client([
'proxy' => 'http://proxy.example.com:8080',
]);
Event Listeners
$adapter->setEventDispatcher(new CustomEventDispatcher());
Flysystem Events
preWrite, postRead):
$filesystem->addListener('preWrite', function ($event) {
Log::info('Writing file: ' . $event->getPath());
});
SSL Verification
$client = new \GuzzleHttp\Client(['verify' => false]);
Timeouts
$client = new \GuzzleHttp\Client(['timeout' => 30]);
Environment Variables
.env for sensitive data:
GITLAB_URL=https://gitlab.example.com
GITLAB_TOKEN=your_token_here
GITLAB_PROJECT=group/project
How can I help you explore Laravel packages today?