本SDK提供CopyObject
类用于拷贝文件,具体内容详见 官方文档 。
参数说明
Bucket
: 目标存储空间Key
: 目标存储空间中的文件名CopySource
: 源文件在存储空间中的路径
示例
执行该示例前请确保配置文件的正确性
以下代码段需要在上下文中运行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 初始化客户端
try {
$s3Client = new S3Client([
'endpoint' => $endpoint,
'region' => $region,
'signature_version' => $signatureVersion,
'force_path_style' => $forcePathStyle,
'credentials' => [
'key' => $accessKey,
'secret' => $secretKey,
],
]);
echo "Client initialized successfully!" . PHP_EOL;
} catch (InvalidArgumentException $e) {
die("Failed to initialize Client: " . $e->getMessage());
}
try {
// 执行复制操作
$result = $s3Client->copyObject([
'Bucket' => $destinationBucketName,
'Key' => $destinationKeyName,
'CopySource' => "{$sourceBucketName}/{$sourceKeyName}",
]);
echo "File copied successfully from {$sourceBucketName}/{$sourceKeyName} to {$destinationBucketName}/{$destinationKeyName}" . PHP_EOL;
} catch (AwsException $e) {
echo "Error copying file: " . $e->getMessage() . PHP_EOL;
}