I'm trying to copy a folder from one S3 bucket to another using PHP but it is causing me problems. I can create the folder I want but I cannot copy from one folder to another. Here is my code. Any help would be greatly appreciated
$key = '123';
$secret = '123';
$bucket = 'bck-users';
$keyname = $username;
try {
//Create bucket for user
$s3 = Aws\S3\S3Client::factory(array(
'region' => 'eu-west-1',
'version' => '2006-03-01',
'credentials' => array(
'key' => $key,
'secret' => $secret,
)
));
$result = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
));
echo $result['ObjectURL'] . "\n";
}
catch (Exception $e){
echo $e->getMessage() . "\n";
}
//Copy default files to bucket
$sourceBucket = 'bck-users';
$sourceKeyname = 'default';
$targetBucket = $username;
$targetKeyname = 'default';
// Instantiate the client.
$s3 = S3Client::factory(array(
'region' => 'eu-west-1',
'version' => '2006-03-01',
'credentials' => array(
'key' => $key,
'secret' => $secret,);
// Perform a batch of CopyObject operations.
$batch = array();
$batch[] = $s3->getCommand('CopyObject', array(
'Bucket' => $targetBucket,
'Key' => $targetKeyname,
'CopySource' => $sourceBucket/$sourceKeyname,
));
}
try {
$successful = $s3->execute($batch);
}
catch (Exception $e){
echo $e->getMessage() . "\n";
}
$s3Client = new S3Client([
'region' => 'ap-southeast-1',
'version' => 'latest',
'credentials' => [
'key' => Yii::$app->params['key'],
'secret' => Yii::$app->params['secret'],
]
]);
$result = $s3Client->copyObject([
'Bucket' => Yii::$app->params['bucket'],
'CopySource' => 'sourcebucketname/objectkey',
'Key' => 'tragetbucketname/tragetkey',
]);
return $result;
Hi to everyone who comes here.
I suggest a copy
method that can copy to another bucket.
public function CopyFile(string $key, $to)
{
$s3Client = new S3Client([
'version' => 'latest',
'region' => self::AWS_REGION,
'credentials' => [
'key' => self::AWS_KEY,
'secret' => self::AWS_SECRET
]
]);
$s3Client->copy(self::BUCKET_NAME, $key, self::BUCKET_NAME, $to);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.