Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TheForce00's avatar

Access to Storage Folder

I have a docs file in the application storage folder /storage/docs that stores files I need to load during the life of the application.

How can I manage this using the storage facade on AWS using vapor? I have tried the storage_path function and not having any luck.

Thanks!

0 likes
2 replies
LaryAI's avatar
Level 58

When using Laravel Vapor, your application is typically running in a serverless environment, which means you don't have direct access to the local filesystem like you would on a traditional server. Instead, you should use AWS S3 to store and access your files. Here's how you can manage files in the /app/docs directory using the Storage facade with AWS S3:

  1. Configure S3 in Laravel:

    First, ensure that your filesystems.php configuration file is set up to use S3. You should have a disk configuration like this:

    'disks' => [
        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
        ],
    ],
    

    Make sure your .env file contains the correct AWS credentials and bucket information.

  2. Upload Files to S3:

    If you have files that you need to upload to S3, you can use the Storage facade to do so. For example:

    use Illuminate\Support\Facades\Storage;
    
    // Upload a file to S3
    Storage::disk('s3')->put('docs/yourfile.txt', file_get_contents(storage_path('app/docs/yourfile.txt')));
    
  3. Access Files from S3:

    To access files stored in S3, you can use the get method or generate a URL to the file:

    // Get the file's contents
    $contents = Storage::disk('s3')->get('docs/yourfile.txt');
    
    // Generate a URL to the file
    $url = Storage::disk('s3')->url('docs/yourfile.txt');
    
  4. Use Vapor's Asset Management:

    If you are deploying with Laravel Vapor, you can also use Vapor's asset management to handle your files. This involves using the vapor:asset command to upload assets to S3 during deployment.

By using S3, you can ensure that your files are accessible in a serverless environment like AWS Lambda, which is what Laravel Vapor uses under the hood. This approach also provides scalability and reliability for file storage.

jlrdw's avatar

If secure files, I'd suggest a read through the AWS Docs and they have examples as well. I store these things out of any web folders and you can use correct headers if a user needs to download one. I store an image (just example) like:

ann_14578_127.jpg

Original name was ann.jpg. The 127 is the user id. So using string functions if the number after the last underscore doesn't match the authorization then a redirect.

Just simplified example here. But there are good past post that go into detail of securing files.

Please or to participate in this conversation.