1
votes

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!

2
Welcome to SO .. is it base64 image?Kamlesh Paul
Please try to use code: store('<path>','<file_name>','azure') : laravel.com/docs/8.x/filesystem#specifying-a-diskJim Xu
No, it's not a base64 img. I am not trying to store it in the database but the Storage explorer in Azure. I tried specifying the disk. It didn't work for me. I tested giving the URL for the blob as <path> and also the root ( / ). Both options did not work.brpetrov
Now I noticed that I get a Status:500 on network saying: "The provided account key '' is not a valid base64 string. It has to pass the check 'base64_decode(<user_account_key>, true)'.". Any Ideas?brpetrov
@brpetrov Could you please tell me how you get an account key?Jim Xu

2 Answers

3
votes

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

1
votes

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