0

I'm using DigitalOcean Spaces (S3 compatible) for storing my Laravel's app file.

Now I'm trying to make a .txt file inserting in it a text, with this code:

$file = Storage::disk('digitalocean')->put('generated-file/'.$title.'.txt', $content); 
return $file;

But instead of the path of the file, I get a boolean, so when I try to get the complete url with this method:

Storage::disk('digitalocean')->url($file);

I'm getting a wrong URL. How I can achieve what I want to do? (Make a new txt file, don't upload an existing one, and getting his public url on the object storage).

  • `->put()` simply returns `true` or `false`, if you want to use `url()`, you'll need to return something like `'generated-file/'.$title.'.txt'` instead of `$file`. – Tim Lewis Oct 27 '21 at 15:59

1 Answers1

0

put(...) (or CRUD operations in general) will return the result of the operation in the form of a boolean most of the time - true indicating success and false indicating failure.

In this case, $file will contain a boolean value.

Return the file path and then pass that to url(...) to obtain the URL.

$filePath = 'generated-file/'.$title.'.txt';
$result = Storage::disk('digitalocean')->put($filePath, $content); 
return $filePath;
Storage::disk('digitalocean')->url($filePath);
Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44