Report this

What is the reason for this report?

DigitalOcean Spaces and Laravel Unable to write file at location:

Posted on February 7, 2024

I’ve been searching and following documentations & videos but can’t seem to make DigitalOcean Spaces work with Laravel 10

DO_SPACES_KEY=key
DO_SPACES_SECRET=secret
DO_SPACES_ENDPOINT=https://sgp1.digitaloceanspaces.com
DO_SPACES_REGION=sgp1
DO_SPACES_BUCKET=db-sys-attachments
DO_SPACES_URL=https://db-sys-attachments.sgp1.digitaloceanspaces.com
'spaces' => [
            'driver' => 's3',
            'key' => env('DO_SPACES_KEY'),
            'secret' => env('DO_SPACES_SECRET'),
            'region' => env('DO_SPACES_REGION'),
            'bucket' => env('DO_SPACES_BUCKET'),
            'endpoint' => env('DO_SPACES_ENDPOINT'),
            'url' => env('DO_SPACES_URL'),
            'throw' => true,
        ],
if (!Storage::disk('spaces')->put($folder . '/' . $filename, file_get_contents($attachment), 'public')) {
            return false;
        }

Response

Unable to write file at location: profiles/1-cor-sample.jpg.

the file comes from the request



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hey!

There are a few things that I could suggest checking, starting by ensuring your .env configuration matches your DigitalOcean Spaces details accurately. It’s good to double-check for any typos or incorrect values, especially in DO_SPACES_KEY, DO_SPACES_SECRET, DO_SPACES_REGION, and DO_SPACES_BUCKET. Your endpoint and URL seem correctly configured for the Singapore region (sgp1).

After that out of the way, I could suggest adding 'visibility' => 'public' setting to your filesystem as it will be needed to ensure that the images uploaded via the media manager are visible to the public.

Then to further diagnose the issue, you can try catching the exception to get more detailed error messages:

try {
    Storage::disk('spaces')->put($folder . '/' . $filename, file_get_contents($attachment), 'public');
} catch (\Exception $e) {
    dd($e->getMessage());
    // Alternatively, log the error message
    // Log::error($e->getMessage());
}

This can provide more insight into what’s going wrong, whether it’s a permissions issue, a problem with the file itself, or something else.

Also, what I could suggest is to try writing a simple text file to Spaces to rule out issues with the $attachment variable or its contents:

Storage::disk('spaces')->put('test.txt', 'Hello, Spaces!', 'public');

If this works, the issue might be with how you’re handling or referencing $attachment.

Let me know how it goes!

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.