public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken ct) { if (_isComplete) { throw new InvalidOperationException("Cannot write anymore to a completed stream"); } _length += count; while (count > 0) { var toCopy = Math.Min(ReadWriteBufferSize - _currentOffset, count); Buffer.BlockCopy(buffer, offset, _buffer, _currentOffset, toCopy); offset += toCopy; count -= toCopy; _currentOffset += toCopy; if (_currentOffset == ReadWriteBufferSize) { if (_uploader == null) { _uploader = new AmazonMultiUpload(_client, _bucket, _key, _metadata, ct); } _uploader.PushBlockOfData(_buffer, _partNumber); _partNumber++; _buffer = new byte[ReadWriteBufferSize]; _currentOffset = 0; } } return Task.FromResult(0); }
public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken ct) { if (_isComplete) { throw new InvalidOperationException("Cannot write anymore to a completed stream"); } _length += count; while (count > 0) { var toCopy = Math.Min(ReadWriteBufferSize - _currentOffset, count); Buffer.BlockCopy(buffer, offset, _buffer, _currentOffset, toCopy); offset += toCopy; count -= toCopy; _currentOffset += toCopy; if (_currentOffset == ReadWriteBufferSize) { if (_uploader == null) { _uploader = new AmazonMultiUpload(_client, _bucket, _key, _metadata, ct); } _uploader.PushBlockOfData(_buffer, _partNumber); _partNumber++; _buffer = new byte[ReadWriteBufferSize]; _currentOffset = 0; } } return(Task.FromResult(0)); }
public void Dispose() { if (_uploader != null) { // We just need to finish this off on a new thread try { SafeTask.SafeWait(() => _uploader.Abort()); } catch (Exception) { } _uploader = null; } _isComplete = true; }
public async Task Complete(CancellationToken ct) { if (!_isComplete) { string versionId; if (_uploader != null) { versionId = await _uploader.Complete().ConfigureAwait(false); } else { // Just a single upload request... using (var ms = new MemoryStream(_buffer, 0, _currentOffset)) { var request = new PutObjectRequest { AutoCloseStream = false, AutoResetStreamPosition = false, BucketName = _bucket, Key = _key, InputStream = ms }; // Copy the metadata across if (_metadata != null) { foreach (var m in _metadata) { request.Metadata.Add("x-amz-meta-" + m.Key, m.Value); } } var resp = await _client.PutObjectAsync(request, ct).ConfigureAwait(false); versionId = resp.VersionId; } } VersionId = versionId; _isComplete = true; _uploader = null; } }
public void Dispose() { if (_uploader != null) { _uploader.Abort(); _uploader = null; } _isComplete = true; }