S3多部分上传例外

时间:2012-11-28 12:18:51

标签: c# stream amazon-s3

使用S3-MultiPartAPI我得到一个例外:

  

“{”您建议的上传小于最小允许尺寸“}”

我测试了2个11MB和7MB的文件。 1MB的工作没有问题。

  

FileStream fs = new FileStream(foo,FileAccess.ReadWrite);

     

fs.CopyTo(new S3Tools.S3CopyMemoryStream(“bar”,new byte [Buffersize + Buffersize / 2],transferUtility));

我将配置utilityConfig.MinSizeBeforePartUpload设置为2.

public class S3CopyMemoryStream : MemoryStream
    {
        private string _key;
        private byte[] _buffer;
        private S3TransferUtility _transferUtility;
        public StartUploadS3CopyFileStreamEvent StartUploadS3FileStreamEvent { get; set; }

        public int WriteCount { get; private set; }
        public bool EndOfPart { get; private set; }
        public bool InitiatingPart { get; private set; }
        public string UploadPartId { get; set; }
        public List<PartETag> PartETagCollection { get; set; }

        public S3CopyMemoryStream WithS3CopyFileStreamEvent(StartUploadS3CopyFileStreamEvent doing)
        {
            S3CopyMemoryStream s3CopyStream = new S3CopyMemoryStream(this._key, this._buffer, this._transferUtility);

            s3CopyStream.StartUploadS3FileStreamEvent = new S3CopyMemoryStream.StartUploadS3CopyFileStreamEvent(CreateMultiPartS3Blob);

            return s3CopyStream;
        }

        public S3CopyMemoryStream(string key, byte[] buffer, S3TransferUtility transferUtility)
            : base(buffer)
        {
            InitiatingPart = true;
            EndOfPart = false;
            WriteCount = 1;
            PartETagCollection = new List<PartETag>();
            QueueWithIncompleteParts = new Queue<int>();

            _buffer = buffer;
            _key = key;
            _transferUtility = transferUtility;
        }

        public delegate void StartUploadS3CopyFileStreamEvent(S3TransferUtility transferUtility, string key, S3CopyMemoryStream stream);          

        public override bool CanSeek
        {
            get { return false; }
        }

        public override void Write(byte[] array, int offset, int count)
        {
            base.Write(array, offset, count);

            if(Position >= Buffersize)
            {
                if (WriteCount > 1)
                    InitiatingPart = false;
                StartUploadS3FileStreamEvent.Invoke(_transferUtility, _key, this);

                WriteCount++;
                base.Flush();
                base.Seek(0, SeekOrigin.Begin);
                base.Flush();

            }              
        }

        public override void Close()
        {
            if(WriteCount > 1)
                InitiatingPart = false;

            EndOfPart = true;
            StartUploadS3FileStreamEvent.Invoke(_transferUtility, _key, this);
            base.Close();
        }

    }

这是Uploadevent。在这一行我得到了例外。

  

CompleteMultipartUploadResponse completeMultipartUploadResponse =                           transferUtility.S3Client.CompleteMultipartUpload(completeMultipartUploadRequest);

 static internal void CreateMultiPartS3Blob(S3TransferUtility transferUtility, string key, S3CopyMemoryStream stream)
    {

        if (stream.InitiatingPart)
        {
            InitiateMultipartUploadRequest initiateMultipartUploadRequest =
                new InitiateMultipartUploadRequest()
                    .WithBucketName(transferUtility.BucketName)
                    .WithKey(key);


            InitiateMultipartUploadResponse initiateResponse =
                transferUtility.S3Client.InitiateMultipartUpload(initiateMultipartUploadRequest);
            stream.UploadPartId = initiateResponse.UploadId;

        }


            UploadPartRequest uploadPartRequest =
                new UploadPartRequest()
                    .WithBucketName(transferUtility.BucketName)
                    .WithKey(key)
                    .WithPartNumber(stream.WriteCount)
                    .WithPartSize(stream.Position)
                    .WithUploadId(stream.UploadPartId)
                    .WithInputStream(stream) as UploadPartRequest;

            UploadPartResponse response = transferUtility.S3Client.UploadPart(uploadPartRequest);
            PartETag etag = new PartETag(response.PartNumber, response.ETag);
            stream.PartETagCollection.Add(etag);



        if (stream.EndOfPart)
        {

            CompleteMultipartUploadRequest completeMultipartUploadRequest =
                new CompleteMultipartUploadRequest()
                    .WithBucketName(transferUtility.BucketName)
                    .WithKey(key)
                    .WithPartETags(stream.PartETagCollection)
                    .WithUploadId(stream.UploadPartId);

            CompleteMultipartUploadResponse completeMultipartUploadResponse =
                transferUtility.S3Client.CompleteMultipartUpload(completeMultipartUploadRequest);
            string loc = completeMultipartUploadResponse.Location;

        }

    }

有什么建议问题是什么?

1 个答案:

答案 0 :(得分:1)

请求使用当前位置的流,因此我必须在上传之前将位置设置为零。

首先想到的是发布.. * g

问候