public void Execute() { Console.WriteLine("doc Started."); try { //fetch all the resources to be uploaded AssetDataFactory _dataFactory = new AssetDataFactory(); S3Uploader cdnManager = new S3Uploader(); int chunckSize = 0; Console.WriteLine("Fetch asset for syncing"); int totalCount = _dataFactory.GetAppointmentAssetCount((int)AssetTypes.Appointment_Document, true); Console.WriteLine("Upload asset is in progress"); do { List <Assets> assets = _dataFactory.GetAppointmentAsset((int)AssetTypes.Appointment_Document, true, 2);//asset type //loop through each asset and upload to CDN foreach (var item in assets) { var assetRequest = new UploadAssetRequestParameters(); string directoryName = Path.GetDirectoryName(item.TargetPath); //int pos = item.TargetPath.LastIndexOf("/") + 1; string bucketRelativePath = directoryName;//item.TargetPath.Substring(0, pos); assetRequest.BucketName = ConfigurationManager.AppSettings["S3BucketName"] + @"/private/" + directoryName; assetRequest.FilePath = ConfigurationManager.AppSettings["FileStoreRoot"] + item.TargetPath; assetRequest.Tags = new Dictionary <string, string>() { { "Asset Type", "Appointment Document" }, { "Asset Id", item.AssetId.ToString() } }; if (item.AppointmentAssets.Count > 0) { assetRequest.Tags.Add("Asset Entity Id", item.AppointmentAssets.FirstOrDefault().appointmentID.ToString()); } int monthDiff = S3Uploader.GetMonthDifference(DateTime.UtcNow, item.UpdatedDateTime ?? item.CreatedDateTime.Value); S3StorageClass storageClass = S3StorageClass.Standard; if (monthDiff > 6 && monthDiff < 36) { storageClass = S3StorageClass.StandardInfrequentAccess; } else if (monthDiff > 36) { storageClass = S3StorageClass.Glacier; } string objectKey = cdnManager.UploadFile(assetRequest, storageClass); if (!string.IsNullOrEmpty(objectKey)) { _dataFactory.UpdateAssetPath(item.AssetId, bucketRelativePath + objectKey); } } chunckSize = chunckSize + 2; } while (totalCount > chunckSize); Console.WriteLine("Upload asset is completed"); } catch (Exception e) { ErrorUtility.LogError(e, GartnerApplication.Unknown, string.Format("Error: Upload Doc error: " + e.Message)); } }
private static async Task LifecycleConfigAsync() { client = new AmazonS3Client(bucketRegion); string input = Console.ReadLine(); try { int action = Convert.ToInt32(input); var lifeCycleConfiguration = new LifecycleConfiguration(); switch (action) { case 1: await AddUpdateDeleteLifecycleConfigAsync(); break; case 2: lifeCycleConfiguration = await RetrieveLifecycleConfigAsync(client); foreach (var rule in lifeCycleConfiguration.Rules) { Console.WriteLine("Rule Id {0} with status of {1} and expiration Days is ", rule.Id, rule.Status, rule.Expiration != null ? rule.Expiration.Days: 0); Console.WriteLine("Transition"); foreach (var trans in rule.Transitions) { Console.WriteLine("Storage class {0} and expiration Days is ", trans.StorageClass, trans.Days); } Console.WriteLine("Filter"); if (rule.Filter.LifecycleFilterPredicate is LifecyclePrefixPredicate) { LifecyclePrefixPredicate prefix = rule.Filter.LifecycleFilterPredicate as LifecyclePrefixPredicate; Console.WriteLine("Filter Prefix {0} and expiration Days is ", prefix.Prefix); } if (rule.Filter.LifecycleFilterPredicate is LifecycleTagPredicate) { LifecycleTagPredicate tags = rule.Filter.LifecycleFilterPredicate as LifecycleTagPredicate; Console.WriteLine("Filter Tag Key {0} and value ", tags.Tag.Key, tags.Tag.Value); } Console.WriteLine("*************************************"); } break; case 3: await RemoveLifecycleConfigAsync(client); break; default: break; } } catch (Exception e) { ErrorUtility.LogError(e, GApplication.Unknown, string.Format("Error: Lifecycle Error: " + e.Message)); } }
public void Execute() { Console.WriteLine("Delete Started."); try { //fetch all the resources to be uploaded AssetDataFactory _dataFactory = new AssetDataFactory(); S3Uploader cdnManager = new S3Uploader(); Console.WriteLine("Please specify the relative path in bucket which need to be deleted"); Console.WriteLine(bucketName); string inputRelativePath = Console.ReadLine(); client = new AmazonS3Client(bucketRegion); ListingObjectsAsync(inputRelativePath).Wait(); } catch (Exception e) { ErrorUtility.LogError(e, GartnerApplication.Unknown, string.Format("Error: Upload Doc error: " + e.Message)); } }
public string UploadFile(UploadAssetRequestParameters request, S3StorageClass storageClass, long assetId) { string objectKey = GetS3FileName(request.FilePath, assetId); try { List <Amazon.S3.Model.Tag> tagSet = new List <Amazon.S3.Model.Tag>(); if (request.Tags != null) { foreach (KeyValuePair <string, string> entry in request.Tags) { tagSet.Add(new Amazon.S3.Model.Tag { Key = entry.Key, Value = entry.Value }); } } var awsRegion = Amazon.RegionEndpoint.GetBySystemName(ConfigurationManager.AppSettings["AWSRegion"].ToString()); IAmazonS3 client = new AmazonS3Client(awsRegion); PutObjectRequest putRequest = new PutObjectRequest { BucketName = request.BucketName, Key = objectKey, TagSet = tagSet, StorageClass = storageClass }; //upload by filepath or memorystream if (request.InputStream != null && request.InputStream.Length > 0) { putRequest.InputStream = new MemoryStream(request.InputStream); } else { putRequest.FilePath = request.FilePath; } PutObjectResponse response = client.PutObject(putRequest); return(objectKey);//to fetch the uploaded content append cloudfrontUrl + objectKey } catch (AmazonS3Exception amazonS3Exception) { string errorMessage = String.Empty; if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { throw new Exception("Check the provided AWS Credentials."); } else { errorMessage = string.Format("Error occurred. Message:'{0}' when writing an object", amazonS3Exception.Message); ErrorUtility.LogError(amazonS3Exception, GartnerApplication.UtilityService, string.Format("Error: AWS: UploadFile: " + errorMessage)); } } catch (Exception e) { ErrorUtility.LogError(e, GartnerApplication.Unknown, string.Format("Error: UploadFile: " + e.Message)); } return(string.Empty);//empty string denote that process has got some error }
public void Execute() { Console.WriteLine("audio asset Syn Started."); try { //fetch all the resources to be uploaded AssetDataFactory _dataFactory = new AssetDataFactory(); Console.WriteLine("Fetch asset for syncing"); List <Assets> assets = _dataFactory.GetAppointmentAsset((int)AssetTypes.Appointment_Audio, true);//asset type Console.WriteLine("asset has been fetched and now upload to S3 will be done"); AmazonS3UploadHelper cdnManager = new AmazonS3UploadHelper(); //loop through each asset and upload to CDN Console.WriteLine("Upload asset started at {0}", DateTime.Now.ToLongTimeString()); DateTime dtStartDateTime = DateTime.Now; int counter = 1; foreach (var item in assets) { ProgressHelper.ShowPercentProgress("progress of audio upload ", counter, assets.Count()); counter++; try { var assetRequest = new UploadAssetRequestParameters(); int pos = item.TargetPath.LastIndexOf("/"); string bucketRelativePath = item.TargetPath.Substring(0, pos); assetRequest.BucketName = ConfigurationManager.AppSettings["S3BucketName"] + @"/private/" + bucketRelativePath; assetRequest.BucketName = assetRequest.BucketName.ToLower(); assetRequest.BucketName = assetRequest.BucketName.Replace(@"\\", @"/").Replace(@"\", @"/"); assetRequest.FilePath = ConfigurationManager.AppSettings["FileStoreRoot"] + item.TargetPath; assetRequest.Tags = new Dictionary <string, string>() { { "Asset Type", "Appointment Audio" }, { "Asset Id", item.AssetId.ToString() } }; if (item.AppointmentAssets.Count > 0) { assetRequest.Tags.Add("Asset Entity Id", item.AppointmentAssets.FirstOrDefault().appointmentID.ToString()); } int monthDiff = S3Uploader.GetMonthDifference(DateTime.UtcNow, item.UpdatedDateTime ?? item.CreatedDateTime.Value); S3StorageClass storageClass = S3StorageClass.Standard; if (monthDiff > 6) { storageClass = S3StorageClass.StandardInfrequentAccess; } assetRequest.AssetId = item.AssetId; assetRequest.s3StorageClass = storageClass; //Uploading file string objectKey = cdnManager.UploadFile(assetRequest); if (!string.IsNullOrEmpty(objectKey)) { _dataFactory.UpdateAssetPath(item.AssetId, bucketRelativePath + @"/" + objectKey, @"private/" + bucketRelativePath + @"/" + objectKey); } } catch (Exception ex) { ErrorUtility.LogError(ex, GartnerApplication.Unknown, string.Format("Error: upload image Asset (Asset Id : {0}) error: {1}", item.AssetId, ex.Message)); Console.WriteLine(ex.Message); } } DateTime dtEndDateTime = DateTime.Now; Console.WriteLine("Uploading of audio files completed at {0}", dtEndDateTime.ToLongTimeString()); Console.WriteLine("Time took to upload audio files is {0}", (dtEndDateTime - dtStartDateTime).Duration().TotalMinutes); } catch (Exception ex) { ErrorUtility.LogError(ex, GartnerApplication.Unknown, string.Format("Error: upload image error: " + ex.Message)); Console.WriteLine(ex.Message); } }
public void Execute() { Console.WriteLine("doc Started."); try { //fetch all the resources to be uploaded AssetDataFactory _dataFactory = new AssetDataFactory(); AmazonS3UploadHelper cdnManager = new AmazonS3UploadHelper(); int chunckSize = 0; int defaultchunckSize = 30000; Console.WriteLine("Fetch asset for syncing"); int totalCount = _dataFactory.GetAppointmentAssetCount((int)AssetTypes.Appointment_Document, true); int chunkCounter = 1; int chunkTotal = totalCount / defaultchunckSize; if (chunkTotal == 0) { chunkTotal++; } Console.WriteLine("Upload asset will take place in chunks"); Console.WriteLine("Upload asset started at {0}", DateTime.Now.ToLongTimeString()); DateTime dtStartDateTime = DateTime.Now; do { List <Assets> assets = _dataFactory.GetAppointmentAsset((int)AssetTypes.Appointment_Document, true, defaultchunckSize);//asset type //loop through each asset and upload to CDN int counter = 1; foreach (var item in assets) { ProgressHelper.ShowPercentProgress(string.Format("Uploading documents of chunk {0} out of {1} of which ", chunkCounter, chunkTotal), counter, assets.Count); counter++; try { var assetRequest = new UploadAssetRequestParameters(); item.TargetPath = item.TargetPath.ToLower(); item.TargetPath = item.TargetPath.Replace(@"\\", @"/").Replace(@"\", @"/"); //string directoryName = Path.GetDirectoryName(item.TargetPath); int pos = item.TargetPath.LastIndexOf("/"); string bucketRelativePath = item.TargetPath.Substring(0, pos); assetRequest.BucketName = ConfigurationManager.AppSettings["S3BucketName"] + @"/private/" + bucketRelativePath; assetRequest.FilePath = ConfigurationManager.AppSettings["FileStoreRoot"] + item.TargetPath; assetRequest.Tags = new Dictionary <string, string>() { { "Asset Type", "Appointment Document" }, { "Asset Id", item.AssetId.ToString() } }; if (item.AppointmentAssets.Count > 0) { assetRequest.Tags.Add("Asset Entity Id", item.AppointmentAssets.FirstOrDefault().appointmentID.ToString()); } int monthDiff = S3Uploader.GetMonthDifference(DateTime.UtcNow, item.CreatedDateTime ?? DateTime.UtcNow); S3StorageClass storageClass = S3StorageClass.Standard; if (monthDiff > 6) { storageClass = S3StorageClass.StandardInfrequentAccess; } //else if (monthDiff > 36) // storageClass = S3StorageClass.Glacier; assetRequest.AssetId = item.AssetId; assetRequest.s3StorageClass = storageClass; //Uploading file string extension = Path.GetExtension(item.TargetPath); if (!string.IsNullOrEmpty(extension) && extension == "pdf") { assetRequest.ContentType = "application/pdf"; } if (storageClass != S3StorageClass.Glacier) { string objectKey = cdnManager.UploadFile(assetRequest); if (!string.IsNullOrEmpty(objectKey)) { _dataFactory.UpdateAssetPath(item.AssetId, bucketRelativePath + @"/" + objectKey, @"private/" + bucketRelativePath + @"/" + objectKey); } } } catch (Exception ex) { ErrorUtility.LogError(ex, GartnerApplication.Unknown, string.Format("Error: upload image Asset (Asset Id : {0}) error: {1}", item.AssetId, ex.Message)); Console.WriteLine(ex.Message); } } chunkCounter++; chunckSize = chunckSize + defaultchunckSize; } while (totalCount > chunckSize); DateTime dtEndDateTime = DateTime.Now; Console.WriteLine("Uploading of appointment documents completed at {0}", dtEndDateTime.ToLongTimeString()); Console.WriteLine("Time took to upload appointment documents is {0}", (dtEndDateTime - dtStartDateTime).Duration().TotalMinutes); } catch (Exception e) { ErrorUtility.LogError(e, GartnerApplication.Unknown, string.Format("Error: Upload Doc error: " + e.Message)); Console.WriteLine("Exception while uploading document : {0}", e.Message); } }