解冻归档文件

本SDK提供RestoreObjectCommand类和HeadObjectCommand类用于解冻归档文件。完整代码详见 GithubRestoreObjectCommand调用的 S3 API 为 RestoreObject, 具体参见RestoreObject API 文档HeadObjectCommand调用的 S3 API 为 HeadObject, 具体参见HeadObject API 文档

参数说明

  • 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
function RestoreObject() {
    const [bucketName, setBucketName] = useState("");
    const [keyName, setKeyName] = useState("");
    const [restoreStatus, setRestoreStatus] = useState("");

    const handleRestore = async () => {
        setRestoreStatus("发送解冻请求...");
        const restoreRequest = {
            Days: 3,
        };

        const params = {
            Bucket: bucketName,
            Key: keyName,
            RestoreRequest: restoreRequest,
        };

        try {
            const restoreCommand = new RestoreObjectCommand(params);
            const restoreResponse = await s3.send(restoreCommand);
            console.log("解冻请求发送成功:", restoreResponse);
            setRestoreStatus("解冻请求发送成功!");
            setTimeout(() => {
                checkRestorationStatus(bucketName, keyName);
            }, 3000);

        } catch (err) {
            console.error("解冻对象出错:", err);
            setRestoreStatus("解冻对象出错");
        }
    };
    const checkRestorationStatus = async (bucketName, keyName) => {
        try {
            const headParams = {
                Bucket: bucketName,
                Key: keyName,
            };

            const headCommand = new HeadObjectCommand(headParams);
            const headResponse = await s3.send(headCommand);

            if (headResponse.Restore) {
                const match = headResponse.Restore.match(/ongoing-request="(\w+)"/);
                const isInProgress = match && match[1] === "true";
                const status = isInProgress ? "in-progress" : "finished";
                console.log(`解冻状态: ${status}`);
                setRestoreStatus(`解冻状态: ${status}`);
            } else {
                setRestoreStatus("未找到解冻请求或解冻已完成");
            }
        } catch (err) {
            console.error("检查解冻状态出错:", err);
            setRestoreStatus("检查解冻状态出错");
        }