private void UploadDocument( Document doc ) { doc.ID = Guid.NewGuid().ToString(); string modifiedBy = null; string fileExtension = Path.GetExtension(doc.FileName); string storageProviderFolder = null; byte[] fileContents = null; modifiedBy = UserIdentityService.GetInstance().IdentityProvider.GetUserName(); foreach (Setting setting in doc.Settings) { switch (setting.Name.ToLower()) { case "modifiedby": if (!string.IsNullOrEmpty(setting.Value)) { modifiedBy = setting.Value; } break; } } fileContents = new byte[doc.Size]; using (Stream str = doc.Stream) { str.Read(fileContents, 0, doc.Size); // more code } DocumentFunctions utility = new DocumentFunctions(); utility.InsertDocument( doc.ID, "DB", doc.EntityID, doc.EntityType, doc.FileName, doc.DocumentType, doc.ContentType, doc.Size, 1, doc.Url, fileContents, false, modifiedBy); }
private void UploadDocument( Document doc ) { string modifiedBy = null; string fileExtension = Path.GetExtension(doc.FileName); string storageProviderFolder = null; byte[] fileContents = null; modifiedBy = UserIdentityService.GetInstance().IdentityProvider.GetUserName(); foreach (Setting setting in doc.Settings) { switch (setting.Name.ToLower()) { case "folder": storageProviderFolder = setting.Value; break; case "modifiedby": if (!string.IsNullOrEmpty(setting.Value)) { modifiedBy = setting.Value; } break; } } string accessKeyID = GetUserName(storageProviderUserNameSource, storageProviderUserNameKey); string secretAccessKey = GetPassword(storageProviderPasswordSource, storageProviderPasswordKey); string region = GetRegion(storageProviderRegionSource, storageProviderRegionKey); TransferUtility fileTransferUtility = new TransferUtility(accessKeyID, secretAccessKey, RegionEndpoint.GetBySystemName(region)); TransferUtilityUploadRequest request = new TransferUtilityUploadRequest(); request.BucketName = GetBucket(storageProviderContainerSource, storageProviderContainerKey); //TODO - need to secure and provide options for ACL and storage class request.CannedACL = S3CannedACL.PublicRead; request.StorageClass = S3StorageClass.ReducedRedundancy; doc.ID = Guid.NewGuid().ToString(); if (String.IsNullOrEmpty(storageProviderFolder)) { request.Key = String.Format("{0}{1}", doc.ID, fileExtension); } else { request.Key = String.Format("{0}/{1}{2}", storageProviderFolder, doc.ID, fileExtension); } request.AutoCloseStream = false; request.ContentType = doc.ContentType; request.InputStream = doc.Stream; request.InputStream.Position = 0; fileTransferUtility.Upload(request); doc.Url = String.Format("{0}.s3.amazonaws.com/{1}", request.BucketName, request.Key); DocumentFunctions utility = new DocumentFunctions(); utility.InsertDocument( doc.ID, "AmazonS3", doc.EntityID, doc.EntityType, doc.FileName, doc.DocumentType, doc.ContentType, doc.Size, 1, doc.Url, fileContents, false, modifiedBy); }