//POST file to Amazon server public int UploadFile(AmazonAddRequest saveFile, string title) { bool retVal = false; int id = 0; //instantiate the request model for the SQL server FileInsertRequest payload = new FileInsertRequest(); // grab the current user id from the User Service int personId = UserService.UserSelect().PersonId; //hydrate the payload with information from the amazon object payload.PersonID = personId; payload.FileUrl = saveFile.keyName; payload.FileTitle = title.Replace("\"", ""); payload.CreatedBy = userGuid; payload.ModifiedBy = payload.CreatedBy; payload.FileType = saveFile.FileType; //upload the amazon object to the amazon server retVal = _uploadService.UploadFileFromFolder(saveFile); if (retVal) { //if successful (true), send the payload object to the SQL server. id = fms.FileInsert(payload); } ; return(id); }
public bool UploadFileFromFolder(AmazonAddRequest file) { bool retVal = false; var accessKey = ConfigurationManager.AppSettings["AWSAccessKey"]; // Get access key from a secure store var secretKey = ConfigurationManager.AppSettings["AWSSecretKey"]; // Get secret key from a secure store string fileName = file.keyName; string localFile = file.localFilePath; string message = string.Empty; using (AmazonS3Client client = new AmazonS3Client(accessKey, secretKey, Amazon.RegionEndpoint.USWest2)) try { PutObjectRequest req = new PutObjectRequest() { BucketName = bucketName, Key = fileName, FilePath = localFile }; PutObjectResponse resp = client.PutObject(req); retVal = resp.HttpStatusCode == HttpStatusCode.OK ? true : false; } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { message = "Check the provided AWS Credentials. \n For service sign up go to http://aws.amazon.com/s3"; //give back the error to main() throw new Exception("Check the provided AWS Credentials."); } else { message = String.Format("Error occurred. Message:'{0}' when writing an object", amazonS3Exception.Message); //throw new Exception("Error occurred: " + amazonS3Exception.Message); } } return(retVal); }
public async Task <HttpResponseMessage> FilePost() { ItemResponse <int> response = new ItemResponse <int>(); // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = HttpContext.Current.Server.MapPath("~/assets/images/"); var uploadFile = new MultipartFormDataStreamProvider(root); try { // Read the form data. upload the file await Request.Content.ReadAsMultipartAsync(uploadFile); // This illustrates how to get the file names. foreach (MultipartFileData file in uploadFile.FileData) { Trace.WriteLine(file.Headers.ContentDisposition.FileName); Trace.WriteLine("Server file path: " + file.LocalFileName); //instantiate the request object AmazonAddRequest saveFile = new AmazonAddRequest(); //grab the custom title from user input to use in payload later in the FileUploadApiController. string title = file.Headers.ContentDisposition.Name; //hydrate the object saveFile.rootFolder = ("/~assets/images"); saveFile.localFilePath = file.LocalFileName; saveFile.fileName = System.IO.Path.GetFileNameWithoutExtension(file.Headers.ContentDisposition.FileName.Replace("\"", "")); saveFile.extension = file.Headers.ContentType.MediaType.Split('/')[1]; //saveFile.extension = System.IO.Path.GetExtension(file.Headers.ContentType); saveFile.keyName = saveFile.fileName + Guid.NewGuid() + saveFile.extension; saveFile.urlFilePath = HttpContext.Current.Server.MapPath(saveFile.rootFolder + saveFile.keyName); saveFile.fileNameWithExtension = saveFile.fileName + saveFile.extension; //Check File type! FileType UserFileType = CheckFileType.ReturnFiletype(saveFile.extension); saveFile.FileType = UserFileType; if (UserFileType == FileType.NoType) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType)); } //Send payload to FileUploadApiController response.Item = this.UploadFile(saveFile, title); if (!Request.Content.IsMimeMultipartContent("form-data")) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType)); } } return(Request.CreateResponse(response)); } catch (System.Exception e) { ErrorLogService svc = new ErrorLogService(); ErrorLogAddRequest error = new ErrorLogAddRequest(); error.ErrorFunction = "Sabio.Web.Controllers.Api.FilePost"; error.ErrorMessage = e.Message; error.UserId = UserService.UserSelect().PersonId; svc.ErrorLogInsert(error); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message)); } }