本SDK提供RestoreObject
类和HeadObjectCommand
类用于解冻归档文件。具体内容详见 官方文档 。
参数说明
Bucket
: 文件所在的存储空间Key
: 文件在存储空间内的名称RestoreRequest
: 文件恢复后可访问的天数
示例
执行该示例前请确保配置文件的正确性
以下代码段需要在上下文中运行
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 初始化客户端
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());
}
// 发送对象恢复请求
function restoreS3Object($s3Client, $bucketName, $keyName)
{
try {
$params = [
'Bucket' => $bucketName,
'Key' => $keyName,
'RestoreRequest' => [
'Days' => 3, // 解冻天数
],
];
$s3Client->restoreObject($params);
echo "Restore request sent successfully." . PHP_EOL;
checkRestorationStatus($s3Client, $bucketName, $keyName);
} catch (AwsException $e) {
echo "Error restoring object: " . $e->getMessage() . PHP_EOL;
}
}
// 检查恢复状态
function checkRestorationStatus($s3Client, $bucketName, $keyName)
{
try {
$headParams = [
'Bucket' => $bucketName,
'Key' => $keyName,
];
$result = $s3Client->headObject($headParams);
$restoreStatus = isset($result['Restore']) && strpos($result['Restore'], 'ongoing-request="true"') !== false
? "in-progress"
: "finished or failed";
echo "Restoration status: {$restoreStatus}" . PHP_EOL;
} catch (AwsException $e) {
echo "Error checking restoration status: " . $e->getMessage() . PHP_EOL;
}
}
// 执行对象解冻
restoreS3Object($s3Client, $bucketName, $keyName);
PREVIOUS拷贝文件
NEXTAWS S3 协议支持说明