如何在ASP.NET中将文件流上传到AWS S3?

问题描述 投票:0回答:1

我对AWS S3非常陌生,并尝试通过分块进程上传大文件。从UI,我将文件的分块数据(blob)发送到WCF服务,我将使用MultiPartAPI将其上传到S3。请注意,该文件可以是GB的。这就是为什么我正在制作文件块并将其上传到S3。

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

    //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 // Throwing Exceptions
                        };

    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
        {
            _s3Client.CompleteMultipartUpload(completeRequest);
        }
        catch
        {
            //do some logging and return null response
            return null;
        }
    }

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

这里,stream.Position = 0和stream.Length抛出如下的异常:

在System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream.get_Length()

然后我看到那条流.CanSeek是假的。

我是否需要实际缓冲整个流,提前将其加载到内存中以使其正常工作?

更新:我在下面做,它正在工作,但不知道它是否有效。

    var ms = new MemoryStream();
    stream.CopyTo(ms);
    ms.Position = 0;

有没有其他方法可以做到这一点?提前致谢。

amazon-web-services wcf amazon-s3 aws-sdk-net
1个回答
0
投票

这是一种公平的方式,但我选择了使用签名网址直接上传到S3的不同方法。这样做的好处是可以减轻服务器负担,减少数据传输。

根据您的应用,可能值得考虑这个:

在C#中获取Presigned URL:

public string GetPreSignedUrl(string bucketName, string keyPrefix, string fileName)
{
    var client = new AmazonS3Client(_credentials, _region);
    var keyName = $"{keyPrefix}/{fileName}";
    var preSignedUrlRequest = new GetPreSignedUrlRequest()
    {
        BucketName = bucketName,
        Key = keyName,
        Expires = DateTime.Now.AddMinutes(5),
        Protocol = (Protocol.HTTPS)
    };
    return client.GetPreSignedURL(preSignedUrlRequest);
}

这会创建一个URL,供客户端直接上传到S3,您需要将其传递给UI。然后,您可以使用分段上传到预签名网址。

以下是使用axious:https://github.com/prestonlimlianjie/aws-s3-multipart-presigned-upload/blob/master/frontend/pages/index.js进行分段上传的一个很好的例子

© www.soinside.com 2019 - 2024. All rights reserved.