拷贝文件

本SDK提供CopyObjectCommand类用于拷贝文件,完整代码详见 GithubCopyObjectCommand调用的 S3 API 为 CopyObject, 具体参见CopyObject API 文档

参数说明

  • sourceBucket: 源文件所在的存储空间
  • sourceKey: 源文件所在存储空间里的名称
  • destinationBucket: 目标存储空间
  • destinationKey: 文件在目标存储空间里的名称

示例

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

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
function CopyFile() {
    const [sourceBucket, setSourceBucket] = useState("");
    const [sourceKey, setSourceKey] = useState("");
    const [destinationBucket, setDestinationBucket] = useState("");
    const [destinationKey, setDestinationKey] = useState("");
    const [copyStatus, setCopyStatus] = useState("");

    const handleCopy = async () => {
        const copySource = `${sourceBucket}/${sourceKey}`;
        const params = {
            CopySource: copySource,
            Bucket: destinationBucket,
            Key: destinationKey,
        };

        try {
            const command = new CopyObjectCommand(params);
            const response = await s3.send(command);
            console.log("复制文件成功:", response);
            setCopyStatus("复制文件成功!");
        } catch (err) {
            console.error("复制文件失败:", err);
            setCopyStatus("复制文件失败");
        }
    };