使用S3-MultiPartAPI我得到一個異常:S3多聲部上傳異常
「{」您提出的上傳超過允許的最小尺寸更小的「}」
我測試這個2個文件11MB和7MB。 1MB的工作沒有問題。
FileStream fs = new FileStream(foo,FileAccess.ReadWrite); (新的S3Tools.S3CopyMemoryStream(「bar」,新的字節[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;
}
}
任何建議的問題是什麼?
我想我需要相同的功能而言,我上傳到WCF服務從客戶和服務器上的文件,我需要開始上傳到S3,而林創建從塊字節流。我認爲你的代碼做我需要的。我對嗎? 這是我的文章。 http://stackoverflow.com/questions/15113448/c-sharp-upload-in-real-time-from-client-to-server-to-amazon-s3#comment21267920_15113448 – VAAA
看到我自己的答案。總的來說,它的工作。 – zirbel