public string GetFilePath(string fileName, string dir = "Documents") { var s3file = new AwsS3Helper(Database.Configuration); var presignedUrl = s3file.GeneratePreSignedURLFromS3(dir, fileName); return(presignedUrl); }
public ActionResult Edit(Image model, HttpPostedFileBase newImage) { if (ModelState.IsValid) { var image = db.Images.Find(model.ID); if (image == null) { return(new HttpNotFoundResult()); } // S3 credentials var s3BktName = ConfigurationManager.AppSettings["S3BucketName"]; var s3AccessKey = ConfigurationManager.AppSettings["AWSAccessKeyId"]; var s3SecretAccessKey = ConfigurationManager.AppSettings["AWSSecretAccessKey"]; var s3Region = ConfigurationManager.AppSettings["AWSRegion"]; var s3FolderName = "Test"; var s3ObjectKey = s3FolderName + "/" + Path.GetFileName(newImage.FileName); var awsS3Helper = new AwsS3Helper(); if (awsS3Helper.ConnectS3(s3AccessKey, s3SecretAccessKey, s3Region)) { // Delete the existing image first awsS3Helper.DeleteS3Object(image.ImagePathS3, s3BktName, null); awsS3Helper.UploadS3File(s3ObjectKey, s3BktName, newImage.ContentType, newImage.InputStream); } string filePath = GetPartialFilePath(newImage); var oldImagePath = image.ImagePath; // Saving in DB image.ImageTitle = Path.GetFileNameWithoutExtension(newImage.FileName); image.ImagePath = filePath; image.ImagePathS3 = s3ObjectKey; image.OriginalFileName = Path.GetFileName(newImage.FileName); db.Entry(image).State = EntityState.Modified; db.SaveChanges(); // Delete existing file string sFName = HttpContext.Server.MapPath(oldImagePath); System.IO.File.Delete(sFName); // Saving in physical location filePath = Server.MapPath(filePath); newImage.SaveAs(filePath); return(RedirectToAction("Index")); } return(View()); }
public async Task <string> Save(IFormFile file, string dir = "Documents") { if (file == null || file.Length == 0) { return(String.Empty); } var filePath = Path.GetTempFileName(); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } var fileType = file.ContentType.Split('/').Last(); var fileName = ShortId.Generate(true, false, 8) + "." + fileType; var s3file = new AwsS3Helper(Database.Configuration); try { s3file.SaveFileToS3(filePath, dir, fileName); } catch (Exception ex) { throw ex; } // Validate url // var presignedUrl = s3file.GeneratePreSignedURLFromS3(dir, fileName); // read file to double check file is uploaded to S3 if (!await s3file.ValidateFileSizeFromS3(dir, fileName, file.Length)) { throw new Exception("Upload failed, file size exception"); } var storage = new FileStorage { OriginalFileName = file.FileName, ConvertedFileName = fileName, Size = file.Length, ContentType = file.ContentType, Directory = dir, UserId = userId }; dc.DbTran(() => { dc.Table <FileStorage>().Add(storage); }); return(storage.Id); }
public JsonResult Create(string additionInfo) { if (Request.Files.Count > 0) { // S3 credentials var s3BktName = ConfigurationManager.AppSettings["S3BucketName"]; var s3AccessKey = ConfigurationManager.AppSettings["AWSAccessKeyId"]; var s3SecretAccessKey = ConfigurationManager.AppSettings["AWSSecretAccessKey"]; var s3Region = ConfigurationManager.AppSettings["AWSRegion"]; var s3FolderName = "Test"; var s3ObjectKey = s3FolderName + "/"; var awsS3Helper = new AwsS3Helper(); var s3Connected = awsS3Helper.ConnectS3(s3AccessKey, s3SecretAccessKey, s3Region) && awsS3Helper.CreateFolder(s3BktName, s3FolderName); // Get all files from Request object HttpFileCollectionBase files = Request.Files; for (int i = 0; i < files.Count; i++) { HttpPostedFileBase file = files[i]; // Saving S3 if (s3Connected) { s3ObjectKey += Path.GetFileName(file.FileName); awsS3Helper.UploadS3File(s3ObjectKey, s3BktName, file.ContentType, file.InputStream); } string filePath = GetPartialFilePath(file); //Adding database Entry var image = new Image { ImageTitle = Path.GetFileNameWithoutExtension(file.FileName), ImagePath = filePath, ImagePathS3 = s3ObjectKey, OriginalFileName = Path.GetFileName(file.FileName) }; db.Images.Add(image); filePath = Server.MapPath(filePath); file.SaveAs(filePath); } db.SaveChanges(); return(Json(new { Message = "Images uploaded successfully." })); } return(Json(new { Message = "Please select atleast image first." })); }
public async Task <string> FileUpload(BucketFolder bucketFolder, IFormFile file) { if (file != null) { AwsS3Helper s3 = new AwsS3Helper(Configuration); List <string> path = new List <string> { Enum.GetName(typeof(BucketFolder), bucketFolder), DateTime.Now.Year.ToString(), DateTime.Now.Day.ToString() }; var s3path = await s3.UploadFileToS3(file, path); return("https://" + Configuration.GetValue <string>("S3Details:BucketName") + "." + Configuration.GetValue <string>("S3Details:BucketUrl") + "/" + s3path); } return(string.Empty); }
public ActionResult DeleteConfirmed(int id) { Image img = db.Images.Find(id); if (img == null) { return(HttpNotFound()); } // remove the file from disk... try { string sFName = HttpContext.Server.MapPath(img.ImagePath); System.IO.File.Delete(sFName); } catch (Exception exc) { throw new HttpException(500, exc.Message); } // Remove file from S3 // S3 Related works var s3BktName = ConfigurationManager.AppSettings["S3BucketName"]; var s3AccessKey = ConfigurationManager.AppSettings["AWSAccessKeyId"]; var s3SecretAccessKey = ConfigurationManager.AppSettings["AWSSecretAccessKey"]; var s3Region = ConfigurationManager.AppSettings["AWSRegion"]; var awsS3Helper = new AwsS3Helper(); if (awsS3Helper.ConnectS3(s3AccessKey, s3SecretAccessKey, s3Region)) { awsS3Helper.DeleteS3Object(img.ImagePathS3, s3BktName, null); } // Saving database entry db.Images.Remove(img); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Details(int id) { var image = db.Images.Find(id); if (image == null) { return(new HttpNotFoundResult()); } //Retrieving Images from S3 var s3BktName = ConfigurationManager.AppSettings["S3BucketName"]; var s3AccessKey = ConfigurationManager.AppSettings["AWSAccessKeyId"]; var s3SecretAccessKey = ConfigurationManager.AppSettings["AWSSecretAccessKey"]; var s3Region = ConfigurationManager.AppSettings["AWSRegion"]; var awsS3Helper = new AwsS3Helper(); if (awsS3Helper.ConnectS3(s3AccessKey, s3SecretAccessKey, s3Region, s3BktName)) { image.ImagePathS3 = awsS3Helper.GetS3FileUrl(image.ImagePathS3); } return(View(image)); }