public override void Execute()
{
InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest()
{
BucketName = this._fileTransporterRequest.BucketName,
Key = this._fileTransporterRequest.Key,
CannedACL = this._fileTransporterRequest.CannedACL,
ContentType = determineContentType(),
StorageClass = this._fileTransporterRequest.StorageClass,
ServerSideEncryptionMethod = this._fileTransporterRequest.ServerSideEncryptionMethod
};
initRequest.BeforeRequestEvent += this.RequestEventHandler;
if (this._fileTransporterRequest.Metadata != null && this._fileTransporterRequest.Metadata.Count > 0)
initRequest.Metadata = this._fileTransporterRequest.Metadata;
if (this._fileTransporterRequest.Headers != null && this._fileTransporterRequest.Headers.Count > 0)
initRequest.Headers = this._fileTransporterRequest.Headers;
InitiateMultipartUploadResponse initResponse = this._s3Client.InitiateMultipartUpload(initRequest);
_logger.DebugFormat("Initiated upload: {0}", initResponse.UploadId);
try
{
_logger.DebugFormat("Queue up the UploadPartRequests to be executed");
long filePosition = 0;
for (int i = 1; filePosition < this._contentLength; i++)
{
UploadPartRequest uploadRequest = new UploadPartRequest()
{
BucketName = this._fileTransporterRequest.BucketName,
Key = this._fileTransporterRequest.Key,
UploadId = initResponse.UploadId,
PartNumber = i,
PartSize = this._partSize,
#if (BCL && !BCL45)
Timeout = ClientConfig.GetTimeoutValue(this._config.DefaultTimeout,this._fileTransporterRequest.Timeout)
#endif
};
if ((filePosition + this._partSize >= this._contentLength) && _s3Client is AmazonS3EncryptionClient)
{
uploadRequest.IsLastPart = true;
uploadRequest.PartSize = 0;
}
uploadRequest.StreamUploadProgressCallback += this.uploadPartProgressEventCallback;
uploadRequest.BeforeRequestEvent += this.RequestEventHandler;
if (this._fileTransporterRequest.IsSetFilePath())
{
uploadRequest.FilePosition = filePosition;
uploadRequest.FilePath = this._fileTransporterRequest.FilePath;
}
else
{
uploadRequest.InputStream = this._fileTransporterRequest.InputStream;
}
this._partsToUpload.Enqueue(uploadRequest);
filePosition += this._partSize;
}
this._totalNumberOfParts = this._partsToUpload.Count;
_logger.DebugFormat("Starting threads to execute the {0} UploadPartRequests in the queue", this._totalNumberOfParts);
startInvokerPool();
_logger.DebugFormat("Waiting for threads to complete. ({0})", initResponse.UploadId);
waitTillAllThreadsComplete();
_logger.DebugFormat("Beginning completing multipart. ({0})", initResponse.UploadId);
CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest()
{
BucketName = this._fileTransporterRequest.BucketName,
Key = this._fileTransporterRequest.Key,
UploadId = initResponse.UploadId
};
compRequest.AddPartETags(this._uploadResponses);
compRequest.BeforeRequestEvent += this.RequestEventHandler;
this._s3Client.CompleteMultipartUpload(compRequest);
_logger.DebugFormat("Done completing multipart. ({0})", initResponse.UploadId);
}
catch (Exception e)
{
_logger.Error(e, "Exception while uploading. ({0})", initResponse.UploadId);
shutdown(initResponse.UploadId);
throw;
}
finally
{
if (this._fileTransporterRequest.InputStream != null && !this._fileTransporterRequest.IsSetFilePath() && this._fileTransporterRequest.AutoCloseStream)
{
this._fileTransporterRequest.InputStream.Close();
}
if (_logger != null)
{
_logger.Flush();
}
}
}