解冻归档文件

本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
const s3 = require('./s3client');
const { RestoreObjectCommand, HeadObjectCommand } = require("@aws-sdk/client-s3");

async function restoreS3Object(bucketName, keyName) {
    try {
        const restoreRequest = {
            Days: 3,
        };

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

        const command = new RestoreObjectCommand(params);
        const response = await s3.send(command);
        console.log("Restore request sent successfully:", response);


        await checkRestorationStatus(bucketName, keyName);
    } catch (err) {
        console.error("Error restoring object:", err);
    }
}

async function checkRestorationStatus(bucketName, keyName) {
    try {
        const params = {
            Bucket: bucketName,
            Key: keyName
        };

        const command = new HeadObjectCommand(params);
        const response = await s3.send(command);

        const restStatus = response.Restore ? "in-progress" : "finished or failed";
        console.log(`Restoration status: ${restStatus}`);
    } catch (err) {
        console.error("Error checking restoration status:", err);
    }
}

Example/ 目录中运行以下命令执行该示例

1
$ node RestoreObject.js <bucketName> <keyName>