I was given a task to maintain a Laravel project with Vue.js (inertia) (not very experienced with both). Now we need to switch the host to Azure and I am stuck on the file storage. Specifically, I am not sure how to upload files from Laravel to the Blob. I am already using https://github.com/matthewbdaly/laravel-azure-storage which is a convenient way to connect the driver.
My driver in filesystem.php (details are correct):
'azure' => [
'driver' => 'azure',
'name' => env('AZURE_STORAGE_NAME'),
'key' => env('AZURE_STORAGE_KEY'),
'container' => env('AZURE_STORAGE_CONTAINER'),
'url' => env('AZURE_STORAGE_URL'),
'prefix' => null,
],
my controller:
$url = request()->file->store('azure');
Please help!
According to my test, we should use double quotes to expand the access key in .evn
.
For example
.env
fileAZURE_STORAGE_NAME=<account name>
AZURE_STORAGE_KEY="<account key>"
AZURE_STORAGE_CONTAINER=
AZURE_STORAGE_URL=https://<account name>.blob.core.windows.net/
disks
section of config/filesystems.php
:'azure' => [
'driver' => 'azure',
'name' => env('AZURE_STORAGE_NAME'),
'key' => env('AZURE_STORAGE_KEY'),
'container' => env('AZURE_STORAGE_CONTAINER'),
'url' => env('AZURE_STORAGE_URL'),
'prefix' => null,
],
public function fileUpload(Request $req){
$req->validate([
'file' => 'required|mimes:csv,txt,xlx,xls,pdf|max:2048'
]);
if($req->file()) {
$fileName = time().'_'.$req->file->getClientOriginalName();
// save file to azure blob virtual directory uplaods in your container
$filePath = $req->file('file')->storeAs('uploads/', $fileName, 'azure');
return back()
->with('success','File has been uploaded.')
}
}
Thanks @Jim Xu, tested out the logic from your example and it works great. If someone stumbled upon this, by default azure blob containers are private. To serve file or images publicly for your Laravel app, don't forget to change the container's access level to either Blob or Container
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With