How to move all files and folder from one folder to another of S3 bucket in php with some short way?

How to move all files and folder from one folder to another of S3 bucket in php with some short way?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Moving S3 Folders in PHP: Beyond the Copy-Paste Loop

Moving every file and folder from one S3 prefix to another typically follows a three-step lifecycle: list objects in the source, copy them to the destination, and finally delete the originals from the source. While this approach is functionally correct, it often feels clunky, especially when dealing with large datasets or strict concurrency requirements. The core challenge lies in managing object metadata and ensuring atomicity across these distinct steps without triggering versioning conflicts or leaving orphaned files behind.

The standard AWS SDK implementation for PHP mirrors the logic described in your scenario. You iterate through a ListObjectsV2 call to gather keys, then loop through that list to invoke CopyObject, and finish with a cleanup pass using DeleteObject. This works fine for small migrations, but it introduces latency because the SDK must make three separate API calls per object. More critically, if you copy an object while simultaneously deleting its previous version (or if versioning is enabled), you risk creating duplicates or losing specific versions of files before the operation completes.

The mmove Command: A Simpler Alternative

If your goal is simply to relocate data without strict real-time consistency guarantees during the migration window, AWS provides a dedicated CLI command that abstracts this complexity entirely: aws s3 mv. This command handles the listing, copying, and deleting in a single logical operation. It supports parallel execution for large datasets, automatically manages versioning by overwriting old versions (or deleting them if you specify --delete), and even allows moving between different buckets or regions.

While this is primarily a CLI tool, its existence proves that the three-step manual loop isn't the only architectural pattern available. However, if your PHP application must orchestrate this movement programmatically to integrate with existing logic, relying solely on mmove isn't feasible. You need to optimize the native SDK calls rather than abandon them.

Optimizing the Three-Step Process in PHP

To make the standard approach feel shorter and more efficient, you should focus on reducing latency and improving concurrency. The most significant bottleneck is usually the synchronous iteration over a list of keys. Instead of copying one by one, you can batch your copy operations. Most AWS SDK implementations allow you to queue multiple copy requests and execute them concurrently, significantly reducing the total execution time compared to a serial loop.

Here is how you might structure this in PHP using the AWS SDK for PHP v3:

use Aws\S3\S3Client;

$client = new S3Client([
    'region' => 'us-east-1',
    'version' => 'latest'
]);

$sourceBucket = 'your-source-bucket';
$destinationBucket = 'your-dest-bucket';
$prefix = 'folder-to-move/';

// 1. List objects with pagination handled efficiently
$paginator = $client->getPaginator('ListObjectsV2', [
    'Bucket' => $sourceBucket,
    'Prefix' => $prefix,
]);

$copyPromises = []; // Collect promises for parallel execution

foreach ($paginator as $result) {
    foreach ($result['Contents'] as $object) {
        $key = $object['Key'];
        $targetKey = str_replace($prefix, '', $key); // Preserve relative path structure
        
        // Create a copy promise to execute in parallel
        $copyPromises[] = $client->copyObject([
            'SourceBucket' => $sourceBucket,
            'SourceKey'    => $key,
            'CopySource'   => "{$sourceBucket}.{$key}",
            'Bucket'       => $destinationBucket,
            'Key'          => $targetKey,
            // Metadata preservation if needed:
            // 'MetadataDirective' => 'COPY', 
        ]);
    }
}

// 2. Execute all copies concurrently
$copyPromises = array_map(function ($p) { return $p->wait(); }, $copyPromises);

// 3. Delete objects from source (optional: do this after successful copy if needed)
foreach ($paginator as $result) {
    foreach ($result['Contents'] as $object) {
        $client->deleteObject([
            'Bucket' => $sourceBucket,
            'Key'    => $object['Key'],
        ]);
    }
}

Critical Trade-offs and Pitfalls

The primary trade-off in this optimized approach is concurrency control. If you copy too many objects simultaneously without throttling, you might hit the RateLimitExceeded error or saturate your application's memory. You must be mindful of S3's request limits (typically 5,000 requests per second for list operations and specific concurrency caps for other actions).

Additionally, consider whether you need to preserve metadata like custom headers or encryption settings. The CopyObject operation defaults to copying ACLs and metadata, but this behavior can change depending on bucket policies. Always verify that the destination bucket accepts the incoming object types (e.g., if source is an S3 Glacier archive, it must be restored first).

If you are building a reusable service for Laravel, encapsulating this logic in a helper class prevents duplication and makes error handling consistent. For complex migration scenarios where downtime is unacceptable, consider staging the move to a temporary prefix before finalizing the deletion of originals.

For developers looking to streamline these operations within their stack, resources like https://laravelcompany.com often provide insights into integrating third-party services and managing cloud infrastructure efficiently within modern PHP frameworks.

Our network