存储类型转换

本SDK提供CopyObject类实现存储类型转换,允许在不移动或重新上传文件的情况下更新对象的元数据。具体内容详见 官方文档

参数说明

  • Bucket: 文件所在的存储空间
  • CopySource: 存储空间内的文件路径
  • Key: 文件在存储空间内的名称
  • StorageClass: S3 存储类型
  • MetadataDirective: 复制原有元数据("COPY")或替换为新的元数据("REPLACE")

存储类型转换规则

US3存储类型 S3存储类型 US3对应S3默认存储类型
STANDARD STANDARD
STANDARD_IA
STANDARD
IA ONEZONE_IA
INTELLIGENT_TIERING
REDUCED_REDUNDANCY
ONEZONE_IA
ARCHIVE GLACIER
DEEP_ARCHIVE
GLACIER

示例

执行该示例前请确保配置文件的正确性
以下代码段需要在上下文中运行

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
// 初始化 S3 客户端
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 {
    // 执行更新存储类别操作
    $copySource = "{$bucketName}/{$keyName}";
    $result = $s3Client->copyObject([
        'Bucket'            => $bucketName,
        'CopySource'        => $copySource,
        'Key'               => $keyName,
        'StorageClass'      => $storageClass,
        'MetadataDirective' => 'COPY'
    ]);

    echo "Storage class updated successfully for {$keyName} in bucket {$bucketName}." . PHP_EOL;
} catch (AwsException $e) {
    echo "Error updating storage class: " . $e->getMessage() . PHP_EOL;
}