分片上传

本SDK提供CreateMultipartUploadCommand,UploadPartCommand,CompleteMultipartUploadCommand,AbortMultipartUploadCommand这四个类共同实现分片上传,完整代码详见 GithubCreateMultipartUploadCommand调用的 S3 API 为 CreateMultipartUpload, 具体参见CreateMultipartUpload API 文档UploadPartCommand调用的 S3 API 为 UploadPart, 具体参见UploadPart API 文档CompleteMultipartUploadCommand调用的 S3 API 为 CompleteMultipartUpload, 具体参见CompleteMultipartUpload API 文档AbortMultipartUploadCommand调用的 S3 API 为 AbortMultipartUpload, 具体参见AbortMultipartUpload API 文档

分片上传流程

在US3中,一个完整的分片上传分为初始化分片 -> 上传分片 -> 完成分片(中止分片)三个阶段。

参数说明

  • Bucket: 文件上传后所在的存储空间
  • Key: 文件上传后在存储空间里的名称
  • UploadId: 用于关联所有分片的唯一标识符
  • PartNumber: 当前上传的分片编号
  • Body: 要上传的文件分片的内容
  • ContentMD5: 指定分片数据的 MD5 校验值

示例

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

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function MultipartUpload() {
    const [bucketName, setBucketName] = useState("");
    const [key, setKey] = useState("");
    const [selectedFile, setSelectedFile] = useState(null);
    const [uploadStatus, setUploadStatus] = useState("");

    const handleFileInput = (event) => {
        setSelectedFile(event.target.files[0]);
    };
    const calculateMD5 = async (blob) => {
        const arrayBuffer = await blob.arrayBuffer(); /
        const wordArray = CryptoJS.lib.WordArray.create(arrayBuffer);
        return CryptoJS.MD5(wordArray).toString(CryptoJS.enc.Base64);
    };
    const sliceUpload = async () => {
        if (!selectedFile) {
            setUploadStatus("请选择文件上传");
            return;
        }
        const partSize = 8 * 1024 * 1024; // 8MB

        const numParts = Math.ceil(selectedFile.size / partSize);
        let uploadId;
        try {
        
        // 创建分片上传任务

            const createMultipartUploadResponse = await s3.send(
                new CreateMultipartUploadCommand({
                    Bucket: bucketName,
                    Key: key,
                })
            );
            uploadId = createMultipartUploadResponse.UploadId;
            const uploadPromises = [];
            setUploadStatus("开始创建分片请求...");

            for (let partNumber = 1; partNumber <= numParts; partNumber++) {
                const start = (partNumber - 1) * partSize;
                const end = Math.min(start + partSize, selectedFile.size);
                const filePart = selectedFile.slice(start, end);
                const md5Hash = await calculateMD5(filePart);
                const uploadPartCommand = new UploadPartCommand({
                    Bucket: bucketName,
                    Key: key,
                    UploadId: uploadId,
                    PartNumber: partNumber,
                    Body: filePart,
                    ContentMD5: md5Hash,
                });
                uploadPromises.push(
                    s3.send(uploadPartCommand).then((uploadPartResponse) => ({
                        ETag: uploadPartResponse.ETag,
                        PartNumber: partNumber,
                    }))
                );
            }

            const uploadedParts = await Promise.all(uploadPromises);

            uploadedParts.forEach((part) => {
                if (!part.ETag) {
                    throw new Error(`Part ${part.PartNumber} 上传失败`);
                }
            });
            
             // 完成分片上传

            await s3.send(
                new CompleteMultipartUploadCommand({
                    Bucket: bucketName,
                    Key: key,
                    UploadId: uploadId,
                    MultipartUpload: {
                        Parts: uploadedParts,
                    },
                })
            );
            setUploadStatus(`成功上传 ${key}${bucketName}`);

        } catch (error) {
            console.error("上传文件失败:", error);
            setUploadStatus("上传文件失败");
            if (uploadId) {
            
            // 如果上传失败,中止上传

                await s3.send(
                    new AbortMultipartUploadCommand({
                        Bucket: bucketName,
                        Key: key,
                        UploadId: uploadId,
                    })
                );
            }
        }
    };