Question

DigitalOcean Spaces and Laravel Unable to write file at location:

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

Show comments

Submit an answer


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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
February 9, 2024

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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel