Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uploading files from Laravel to azure blob storage

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!

like image 635
brpetrov Avatar asked Oct 15 '25 18:10

brpetrov


2 Answers

According to my test, we should use double quotes to expand the access key in .evn.

For example

  1. Add this in .env file
AZURE_STORAGE_NAME=<account name>
AZURE_STORAGE_KEY="<account key>"
AZURE_STORAGE_CONTAINER=
AZURE_STORAGE_URL=https://<account name>.blob.core.windows.net/
  1. Add this to the 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,
        ],
  1. Uplaod code
  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.')

        }
   }

enter image description here enter image description here

like image 194
Jim Xu Avatar answered Oct 18 '25 12:10

Jim Xu


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

like image 45
Dale Ryan Avatar answered Oct 18 '25 11:10

Dale Ryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!