
copy() Copy an object from one location to another. You can pass options available to the CopyObject
operation into the stream context options to modify how the object is copied.
// Copy a file on Amazon S3 to another bucket
copy('s3://bucket/key', 's3://other_bucket/key');
Working with buckets
You can modify and browse Amazon S3 buckets similar to how PHP allows the modification and traversal of
directories on your filesystem.
Here's an example of creating a bucket:
mkdir('s3://bucket');
You can pass in stream context options to the mkdir() method to modify how the bucket is created using the
parameters available to the CreateBucket operation.
// Create a bucket in the EU region
mkdir('s3://bucket', stream_context_create(array(
's3' => array(
'LocationConstraint' => 'eu-west-1'
)
));
You can delete buckets using the rmdir() function.
// Delete a bucket
rmdir('s3://bucket');
Note
A bucket can only be deleted if it is empty.
Listing the contents of a bucket
The opendir(), readdir(), rewinddir(), and closedir() PHP functions can be used with the Amazon S3 stream wrapper
to traverse the contents of a bucket. You can pass in parameters available to the ListObjects operation as custom
stream context options to the opendir() function to modify how objects are listed.
$dir = "s3://bucket/";
if (is_dir($dir) && ($dh = opendir($dir))) {
while (($file = readdir($dh)) !== false) {
echo "filename: {$file} : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
You can recursively list each object and prefix in a bucket using PHP's RecursiveDirectoryIterator.
$dir = 's3://bucket';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $file) {
echo $file->getType() . ': ' . $file . "\n";
}
Amazon S3 Stream Wrapper
134
Comentários a estes Manuais