Alvin Bakker
Alvin Bakker

Reputation: 1526

Copy S3 file from one folder to another

I am wondering if it is possible in Laravel 5 to copy a stored file from one folder to another within AWS.

Thus with something like:

$s3 = Storage::disk('s3');
$s3->copy('/old/s3/location/123.jpg', '/new/s3/location/123.jpg');

Is this possible? Or do I need to download the image first and then upload it again?

Upvotes: 1

Views: 10443

Answers (2)

Pawan Verma
Pawan Verma

Reputation: 1269

Yes you can move or copy files from one folder to another easily. For moving file you can use below code:-

$s3 = Storage::disk('s3')->move('folder1/1587195169.mp4', 'folder1/1587195169.mp4');

For copying a file you can use:-

$s=Storage::disk('s3')->copy('folder1/1587195169.mp4', 'folder1/1587195169.mp4');

Upvotes: 0

Alvin Bakker
Alvin Bakker

Reputation: 1526

With a little workaround I got it to work and like this:

$s3 = Storage::disk('s3');
$images = Storage::disk('s3')->allFiles('oldfolder/image/');
foreach($images as $image) {
    $new_loc = str_replace('oldfolder/image/', 'newfolder/image/', $image);
    $s3->copy($image, $new_loc);
}

The crux was in the fact that the $image has the full path already

Upvotes: 3

Related Questions