PHP code example of kaibatech / viettel-cloud-s3
1. Go to this page and download the library: Download kaibatech/viettel-cloud-s3 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/ */
kaibatech / viettel-cloud-s3 example snippets
'providers' => [
// ...
Kaibatech\ViettelCloudS3\ViettelCloudS3ServiceProvider::class,
],
'disks' => [
// ... other disks
'viettel-s3' => [
'driver' => 'viettel-s3',
'key' => env('VIETTEL_S3_ACCESS_KEY_ID'),
'secret' => env('VIETTEL_S3_SECRET_ACCESS_KEY'),
'region' => env('VIETTEL_S3_REGION', 'us-east-1'),
'bucket' => env('VIETTEL_S3_BUCKET'),
'url' => env('VIETTEL_S3_URL'),
'endpoint' => env('VIETTEL_S3_ENDPOINT'),
'throw' => false,
],
],
'viettel-s3' => [
'driver' => 'viettel-s3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'throw' => false,
],
use Illuminate\Support\Facades\Storage;
// Upload a file
$content = 'Hello, Viettel Cloud!';
$path = 'documents/hello.txt';
Storage::disk('viettel-s3')->put($path, $content, [
'visibility' => 'public',
'mimetype' => 'text/plain'
]);
// Check if file exists
if (Storage::disk('viettel-s3')->exists($path)) {
echo "File exists!";
}
// Download file content
$content = Storage::disk('viettel-s3')->get($path);
// Get file size
$size = Storage::disk('viettel-s3')->size($path);
// Get file URL
$url = Storage::disk('viettel-s3')->url($path);
// Delete file
Storage::disk('viettel-s3')->delete($path);
public function uploadFile(Request $request)
{
$request->validate([
'file' => 'ntOriginalName();
// Upload using Viettel S3 driver
$path = Storage::disk('viettel-s3')->putFileAs(
'uploads',
$file,
$filename,
['visibility' => 'public']
);
return response()->json([
'success' => true,
'path' => $path,
'url' => Storage::disk('viettel-s3')->url($path),
'size' => $file->getSize(),
]);
}
// Upload multiple files
$files = [
'file1.txt' => 'Content 1',
'file2.txt' => 'Content 2',
'file3.txt' => 'Content 3',
];
foreach ($files as $filename => $content) {
Storage::disk('viettel-s3')->put("batch/{$filename}", $content, [
'visibility' => 'public'
]);
}
// Delete multiple files
$filesToDelete = ['batch/file1.txt', 'batch/file2.txt', 'batch/file3.txt'];
Storage::disk('viettel-s3')->delete($filesToDelete);
// Upload from stream
$stream = fopen('/path/to/large-file.zip', 'r');
Storage::disk('viettel-s3')->putStream('large-files/archive.zip', $stream);
fclose($stream);
// Read as stream
$stream = Storage::disk('viettel-s3')->readStream('large-files/archive.zip');
// Process stream...
$path = 'documents/example.pdf';
// Get file information
$exists = Storage::disk('viettel-s3')->exists($path);
$size = Storage::disk('viettel-s3')->size($path);
$lastModified = Storage::disk('viettel-s3')->lastModified($path);
$mimeType = Storage::disk('viettel-s3')->mimeType($path);
$url = Storage::disk('viettel-s3')->url($path);
echo "File: {$path}\n";
echo "Exists: " . ($exists ? 'Yes' : 'No') . "\n";
echo "Size: {$size} bytes\n";
echo "Last Modified: " . date('Y-m-d H:i:s', $lastModified) . "\n";
echo "MIME Type: {$mimeType}\n";
echo "URL: {$url}\n";
// Upload with public visibility (adds x-amz-acl: public-read header)
Storage::disk('viettel-s3')->put($path, $content, [
'visibility' => 'public'
]);
// Upload as private (default)
Storage::disk('viettel-s3')->put($path, $content);
// or explicitly
Storage::disk('viettel-s3')->put($path, $content, [
'visibility' => 'private'
]);
try {
Storage::disk('viettel-s3')->put($path, $content);
echo "Upload successful!";
} catch (\League\Flysystem\UnableToWriteFile $e) {
echo "Upload failed: " . $e->getMessage();
} catch (\Exception $e) {
echo "General error: " . $e->getMessage();
}
// Test basic functionality
$disk = Storage::disk('viettel-s3');
// Upload test
$testFile = 'test-' . time() . '.txt';
$testContent = 'Hello from Viettel Cloud S3!';
$disk->put($testFile, $testContent, ['visibility' => 'public']);
// Verify upload
if ($disk->exists($testFile)) {
echo "✅ Upload successful\n";
// Test download
$downloadedContent = $disk->get($testFile);
if ($downloadedContent === $testContent) {
echo "✅ Download successful\n";
}
// Test URL generation
$url = $disk->url($testFile);
echo "📁 File URL: {$url}\n";
// Cleanup
$disk->delete($testFile);
echo "🗑️ Cleanup completed\n";
}
// In config/logging.php, set the default log level to 'debug'
'level' => 'debug',
bash
php artisan vendor:publish --tag=viettel-cloud-s3-config