0

我正在尝试使用Multipart Upload API将大图像上传到AWS S3。从 UI,我发送图像的块(blob),当最后一部分到达时,完成上传并获取上传文件 url。它工作得很好。

示例代码:

public UploadPartResponse UploadChunk(Stream stream, string fileName, string uploadId, List<PartETag> eTags, int partNumber, bool lastPart)
{
    stream.Position = 0;

    //Step 1: build and send a multi upload request
    if (partNumber == 1)
    {
        var initiateRequest = new InitiateMultipartUploadRequest
        {
            BucketName = _settings.Bucket,
            Key = fileName
        };

        var initResponse = _s3Client.InitiateMultipartUpload(initiateRequest);
        uploadId = initResponse.UploadId;
    }

    //Step 2: upload each chunk (this is run for every chunk unlike the other steps which are run once)
    var uploadRequest = new UploadPartRequest
                        {
                            BucketName = _settings.Bucket,
                            Key = fileName,
                            UploadId = uploadId,
                            PartNumber = partNumber,
                            InputStream = stream,
                            IsLastPart = lastPart,
                            PartSize = stream.Length
                        };

    var response = _s3Client.UploadPart(uploadRequest);

    //Step 3: build and send the multipart complete request
    if (lastPart)
    {
        eTags.Add(new PartETag
        {
            PartNumber = partNumber,
            ETag = response.ETag
        });

        var completeRequest = new CompleteMultipartUploadRequest
        {
            BucketName = _settings.Bucket,
            Key = fileName,
            UploadId = uploadId,
            PartETags = eTags
        };

        try
        {
            var res = _s3Client.CompleteMultipartUpload(completeRequest);
            return res.Location;
        }
        catch
        {
            //do some logging and return null response
            return null;
        }
    }

    response.ResponseMetadata.Metadata["uploadid"] = uploadRequest.UploadId;
    return response;
}

现在,我需要获取上传图像的缩略图并将该图像也上传到缩略图目录中。

所以基本上,当原始图像的最后一部分(块)到达时,我正在完成上传并检索文件 url。那时,我还需要上传缩略图并取回缩略图网址。

我看到人们指的是lambda函数,但不知道如何合并到我的多部分 api 代码设置中。

任何人都可以在这里给我一些指导吗?提前致谢。

4

0 回答 0