public ActionResult Index(HttpPostedFileBase file) { VideoFileModel videoToCreate = null; string userID = string.Empty; string fileName = string.Empty; if (file != null && file.ContentLength > 0) { try { using (AmazonS3Client s3Client = new AmazonS3Client(_awsAccessKey, _awsSecretKey)) { var request = new PutObjectRequest() { BucketName = _bucketName, CannedACL = S3CannedACL.PublicRead, //PERMISSION TO FILE PUBLIC ACCESIBLE Key = file.FileName, InputStream = file.InputStream, //SEND THE FILE STREAM ContentType = "video/mp4" }; s3Client.PutObject(request); } } catch (Exception ex) //ToDo { } //Get the UserID from Cookie if (this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("UserID")) { userID = this.ControllerContext.HttpContext.Request.Cookies["UserID"].Value; } //ToDo add error checking videoToCreate = new VideoFileModel(); videoToCreate.FileName = file.FileName; videoToCreate.FileDescription = "Video Uploaded To Acme"; videoToCreate.FilePath = "https://s3-us-west-2.amazonaws.com/videokeith/" + fileName; videoToCreate.FileSize = file.ContentLength.ToString(); videoToCreate.UserID = userID; } UploadPageModel upm = this._repository.Create(videoToCreate); ViewBag.VideoFiles = this._repository.GetAll(Utils.ConvertToInt(userID)); if (upm.SuccessfulTransfer) { //Send out Email Utilities.Utils.SendOutEmail(fileName, "*****@*****.**"); return(View("Index", upm)); } return(RedirectToAction("Index")); //ToDo }
public UploadPageModel Create(VideoFileModel video) { int videoExistsID = 0; //ToDo use DI UploadPageModel upm = new UploadPageModel(); string outMessage = string.Empty; //populate ViewModel common values upm.FileName = Utils.ParseFileName(video.FileName); upm.FileDescription = video.FileDescription.Trim(); //Check to see if Video exists, Upsert if so videoExistsID = Exists(video.FileName.Trim()); if (videoExistsID != 0) { if (!Update(video, videoExistsID)) { upm.SuccessfulTransfer = false; upm.UploadErrors = "Update Failed!"; return(upm); } else { //Populate ViewModel status and errors upm.SuccessfulTransfer = true; upm.UploadErrors = outMessage; return(upm); } } // Creates a new video. using (ksalomon_listEntities db = new ksalomon_listEntities()) { FileInfo videoInfo = new FileInfo { FileName = Utils.ParseFileName(video.FileName), FilePath = video.FilePath, FileDescription = video.FileDescription.Trim(), FileSize = video.FileSize, MemberFK = Utils.ConvertToInt(video.UserID), CreatedDate = DateTime.Now }; // Add the new object to the Members collection. db.FileInfoes.Add(videoInfo); // Save the change to the database. try { db.SaveChanges(); //Populate ViewModel status and errors upm.SuccessfulTransfer = true; upm.UploadErrors = outMessage; } catch (DbEntityValidationException e) //Capture Entity level errors { foreach (var eve in e.EntityValidationErrors) { outMessage += string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { outMessage += string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } //Populate ViewModel status and errors upm.SuccessfulTransfer = false; upm.UploadErrors = outMessage; return(upm); } catch (Exception ex) //Capture generic errors { //Populate ViewModel status and errors upm.SuccessfulTransfer = false; upm.UploadErrors = ex.ToString(); return(upm); } } return(upm); }
public ActionResult UploadToS3(UploadPageModel inputModel) { string filePath = inputModel.FilePath /*@"C:\MP4\SampleVideo_1280x720_2mb.mp4"*/; string keyName = inputModel.FileName /*"SampleVideo_1280x720_2mb.mp4"*/; //FileName string errorMessage = string.Empty; bool fileTransferStatus = false; if (ModelState.IsValid) { try { TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1)); // 1. Upload a file, file name is used as the object key name. fileTransferUtility.Upload(filePath, _bucketName); // 2. Specify object key name explicitly. fileTransferUtility.Upload(filePath, _bucketName, keyName); // 3. Upload data from a type of System.IO.Stream. using (FileStream fileToUpload = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { fileTransferUtility.Upload(fileToUpload, _bucketName, keyName); } // 4.Specify advanced settings/options. TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest { BucketName = _bucketName, FilePath = filePath, StorageClass = S3StorageClass.ReducedRedundancy, PartSize = 6291456, // 6 MB. Key = keyName, CannedACL = S3CannedACL.PublicRead }; fileTransferUtilityRequest.Metadata.Add("param1", "Value1"); fileTransferUtilityRequest.Metadata.Add("param2", "Value2"); fileTransferUtility.Upload(fileTransferUtilityRequest); } catch (AmazonS3Exception s3Exception) { errorMessage += string.Format("Upload Errors: {0} - {1}", s3Exception.Message, s3Exception.InnerException); } catch (Exception ex) //ToDo { errorMessage += string.Format("Upload Errors: {0}", ex.ToString()); } } else { errorMessage = "<div class=\"validation-summary-errors\">" + "The following errors occurred:<ul>"; foreach (var key in ModelState.Keys) { var error = ModelState[key].Errors.FirstOrDefault(); if (error != null) { errorMessage += "<li class=\"field-validation-error\">" + error.ErrorMessage + "</li>"; } } } if (string.IsNullOrEmpty(errorMessage)) { fileTransferStatus = true; } return(Json(new UploadPageModel { UploadErrors = errorMessage, FileName = keyName, SuccessfulTransfer = fileTransferStatus })); }