By fatmabensoy
Hello,
I am using Storage class for getting file url like this;
Storage::disk('doSpaces')->url('uploads/file/'.$this->exampleAttribute);
//This gives : mybucketname.ams3.digitaloceanspaces.com/uploads/file/1546429522.jpg
But I dont want orginal file url, I wanna CDN file url like this ;
mybucketname.ams3.cdn.digitaloceanspaces.com/uploads/file/1546429522.jpg
I prefer clean code instead of string manipulation
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!
I know you said no string manipulation but i currently have the same problem and the only clean way I solved it for my self is to put it in a method:
public function getExampleAttributeStoragePath(){
if($this->exampleAttribute){
return str_replace("digitaloceanspaces","cdn.digitaloceanspaces",Storage::url($this->exampleAttribute));
}else{
return null;
}
}
But I also hope for a cleaner code, hope this helps as a quick fix for now
or you can also edit the url attribute in the filesystem config like this
'spaces' => [
'driver' => 's3',
'key' => env('DO_SPACES_KEY'),
'secret' => env('DO_SPACES_SECRET'),
'endpoint' => env('DO_SPACES_ENDPOINT'),
'region' => env('DO_SPACES_REGION'),
'bucket' => env('DO_SPACES_BUCKET'),
'url' => str_replace(env('DO_SPACES_REGION'),env('DO_SPACES_BUCKET').'.'.env('DO_SPACES_REGION'),str_replace("digitaloceanspaces","cdn.digitaloceanspaces",env('DO_SPACES_ENDPOINT'))),
],
its a bit messy but does the job also this way its stored in the cache
@fatmabensoy @omartuffaha so as you have the spaces registered in the config/filesystem.php, where you specify your endpoint? (if it allows CDN. Currently SFO2 and NYC3 I think, I’d verify)
I just changed my endpoint in my .env file to the following and it serves it all as CDN without any manipulation
DO_SPACES_ENDPOINT=https://sfo2.cdn.digitalocean.com
that made them all automatically serve up as {spacename].sfo2.cdn.digitalocean.com/filename.jpg
Hope this helps!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.