Example #1
0
        private void Upload(IFileStreamWrap stream, string bucketName, string key, bool setPublicAccess)
        {
            try
            {
                key = key.ToLowerInvariant();

                var existsResponse = FileExists(bucketName, key);
                if (existsResponse != null && existsResponse.ContentLength == stream.Length)
                {
                    Logger.Log(string.Format("Skipping {0} because it already exists in {1}", key, bucketName));
                    return;
                }

                var uploadRequest = new TransferUtilityUploadRequest();
                uploadRequest.WithInputStream(stream.StreamInstance);
                uploadRequest.WithBucketName(bucketName);
                Logger.Log(String.Format("Bucket {0}", bucketName));
                if (setPublicAccess)
                {
                    uploadRequest.CannedACL = S3CannedACL.PublicRead;
                }
                uploadRequest.Key = key;
                Logger.Log(String.Format("Key {0}", key));
                uploadRequest.WithTimeout(14400000); // 4 Hours

                var lastKnownPercentage = 0;
                uploadRequest.UploadProgressEvent += (s, e) =>
                {
                    if (e.PercentDone <= lastKnownPercentage)
                    {
                        return;
                    }

                    Logger.Log(String.Format("UploadProgress:{0} :{1}%", key, e.PercentDone));
                    lastKnownPercentage = e.PercentDone;
                };

                TransferUtility.Upload(uploadRequest);
            }
            catch (Exception exception)
            {
                Logger.Log("Error uploading to s3");
                Logger.Log(exception.Message);
                throw;
            }
        }