Ricardinho
Ricardinho

Reputation: 609

Laravel cant delete file in remote url using Storage::('s3')->delete($files); but can delete when using aws command

I want to delete these files in my laravel like this. But it doesnt work!!

use Storage;

class SomeController extends Controller
{
    public function delete()
    {
        $filesToDelete = [
           'http://minio:9100/minio/myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg',
           'http://minio:9100/minio/myapp/1/YFrdE0sarsAcpfFyrifd2.jpeg'
        ];

        // cant delete using this
        Storage::disk('s3')->delete($filesToDelete); 
    }
}

But when I try to delete using this command in my bash it works perfectly

aws --endpoint-url http://minio:9100 s3 rm s3://myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg
delete: s3://myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg

my aws configure list is exactly the same in my project config! I can use Storage to upload the files but when I try to delete the files it doesnt work:

// Working
Storage::disk('s3')->put('1/ImNkE0wwrstgSpzFyruw8.jpeg', $file);

Am I doing something wrong? Is there anyway to delete these files using laravel??

https://docs.min.io/docs/aws-cli-with-minio https://readouble.com/laravel/5.4/en/filesystem.html

Upvotes: 2

Views: 9735

Answers (4)

arifshabbir
arifshabbir

Reputation: 21

You are adding wrong file path.In order to remove image form s3 bucket you are not need to give full path of image, Just add root path of image.

To delete multiple images

        $files = [
           'myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg',
           'myapp/1/YFrdE0sarsAcpfFyrifd2.jpeg'
        ];

        Storage::disk('s3')->delete($files); 

Delete single image

        $file = 'myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg';
        Storage::disk('s3')->delete($file); 

Upvotes: 1

I solving this parsing url, like this:

$file_path = parse_url($file_url);
        //return $file_path['path'];
        Storage::disk('s3')->delete($file_path);

it works for me. In your bucket, the files has a key, thath be need to delete file from your php function. to get this the object url be parsed.

Upvotes: 6

albus_severus
albus_severus

Reputation: 3702

You can do it by this way

$file_name='your_file_path';
unlink(storage_path($file_name));

Upvotes: -1

Hashan
Hashan

Reputation: 180

I think the problem is file path. Try to run as mentioned below.

Storage::disk('s3')->delete('myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg'); 

Upvotes: 8

Related Questions