public static Boolean SendImageToS3(String key, Stream imageStream) { var success = false; using (var client = new AmazonS3Client(RegionEndpoint.USWest2)) { try { PutObjectRequest request = new PutObjectRequest() { InputStream = imageStream, BucketName = BucketName, Key = key }; client.PutObject(request); success = true; } catch (Exception ex) { // swallow everything for now. } } return success; }
public async Task UploadDataFileAsync(string data, string storageKeyId, string storageSecret, string region) { var regionIdentifier = RegionEndpoint.GetBySystemName(region); using (var client = new AmazonS3Client(storageKeyId, storageSecret, regionIdentifier)) { try { var putRequest1 = new PutObjectRequest { BucketName = AwsBucketName, Key = AwsBucketFileName, ContentBody = data, ContentType = "application/json" }; await client.PutObjectAsync(putRequest1); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { throw new SecurityException("Invalid Amazon S3 credentials - data was not uploaded.", amazonS3Exception); } throw new HttpRequestException("Unspecified error attempting to upload data: " + amazonS3Exception.Message, amazonS3Exception); } var response = await client.GetObjectAsync(AwsBucketName, AwsBucketFileName); using (var reader = new StreamReader(response.ResponseStream)) { Debug.WriteLine(await reader.ReadToEndAsync()); } } }
public void Enviar(bool compactar) { const string mime = "application/octet-stream"; var nome = _arquivo.Substring(_arquivo.LastIndexOf('\\') + 1) + ".gz"; var mem = new MemoryStream(); using (var stream = File.OpenRead(_arquivo)) using (var gz = new GZipStream(mem, CompressionMode.Compress, true)) { stream.CopyTo(gz); } var len = mem.Length; var req = new PutObjectRequest() .WithContentType(mime) .WithBucketName(_bucketName) .WithKey(nome) .WithCannedACL(S3CannedACL.Private) .WithAutoCloseStream(true); req.InputStream = mem; _client.PutObject(req); _log.AppendFormat("Envio do arquivo {0} de {1} KB\n", nome, (len / 1024f)); }
public async Task UploadObject(string key, string body) { Console.WriteLine("SERIALIZED BODY TO UPLOAD: " + body); Console.WriteLine("BUCKET NAME: " + _bucket); try { // Create UploadObject request var request = new Amazon.S3.Model.PutObjectRequest { BucketName = _bucket, Key = key, ContentBody = body, // ContentType = "application/json" }; Console.WriteLine("REQUEST: " + request); Console.WriteLine("REQUEST: " + JsonConvert.SerializeObject(request)); // Issue UploadObject request await _s3Client.PutObjectAsync(request); } catch (Exception e) { Console.WriteLine($"Error putting object {key} into bucket {_bucket}. Make sure it exist and your bucket is in the same region as this function."); Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); throw; } }
public static bool Write_File_To_S3(String path, String key_name) { DateTime before = DateTime.Now; try { // simple object put PutObjectRequest request = new PutObjectRequest(); request.WithFilePath(path) .WithBucketName(bucketName) .WithKey(key_name); S3Response response = s3_client.PutObject(request); response.Dispose(); } catch (Exception e) { Console.WriteLine("Exception n Write_File_To_S3(String path=" + path + ", String key_name=" + key_name + "). e.Message="+e.Message); Console.WriteLine("Write_File_To_S3(String path=" + path + ", String key_name=" + key_name + ") failed!!!"); return false; } TimeSpan uploadTime = DateTime.Now - before; Console.WriteLine("Uploading " + path + " into S3 Bucket=" + bucketName + " , key=" + key_name + " took " + uploadTime.TotalMilliseconds.ToString() + " milliseconds"); return true; }
static void Main(string[] args) { using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client()) { try { NameValueCollection appConfig = ConfigurationManager.AppSettings; // get file path string folder = Path.GetFullPath(Environment.CurrentDirectory + "../../../../S3Uploader/"); // write uploader form to s3 const string uploaderTemplate = "uploader.html"; string file = Path.Combine(folder, "Templates/" + uploaderTemplate); var request = new PutObjectRequest(); request.WithBucketName(appConfig["AWSBucket"]) .WithKey(appConfig["AWSUploaderPath"] + uploaderTemplate) .WithInputStream(File.OpenRead(file)); request.CannedACL = S3CannedACL.PublicRead; request.StorageClass = S3StorageClass.ReducedRedundancy; S3Response response = client.PutObject(request); response.Dispose(); // write js to s3 var jsFileNames = new[] { "jquery.fileupload.js", "jquery.iframe-transport.js", "s3_uploader.js" }; foreach (var jsFileName in jsFileNames) { file = Path.Combine(folder, "Scripts/s3_uploader/" + jsFileName); request = new PutObjectRequest(); request.WithBucketName(appConfig["AWSBucket"]) .WithKey(appConfig["AWSUploaderPath"] + "js/" + jsFileName) .WithInputStream(File.OpenRead(file)); request.CannedACL = S3CannedACL.PublicRead; request.StorageClass = S3StorageClass.ReducedRedundancy; response = client.PutObject(request); response.Dispose(); } } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Please check the provided AWS Credentials."); Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3"); } else { Console.WriteLine("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message); } } } }
public async Task UploadFile(string filePath, string s3Bucket, string newFileName, bool deleteLocalFileOnSuccess) { //save in s3 Amazon.S3.Model.PutObjectRequest s3PutRequest = new Amazon.S3.Model.PutObjectRequest(); s3PutRequest = new Amazon.S3.Model.PutObjectRequest(); s3PutRequest.FilePath = filePath; s3PutRequest.BucketName = s3Bucket; //s3PutRequest.ContentType = contentType; //key - new file name if (!string.IsNullOrWhiteSpace(newFileName)) { s3PutRequest.Key = newFileName; } s3PutRequest.Headers.Expires = new DateTime(2020, 1, 1); try { Amazon.S3.Model.PutObjectResponse s3PutResponse = await S3Client.PutObjectAsync(s3PutRequest); if (deleteLocalFileOnSuccess) { //Delete local file if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } } catch (Exception ex) { //handle exceptions } }
public override void Execute() { int timeout = this._config.DefaultTimeout; if (this._fileTransporterRequest.Timeout != 0) timeout = this._fileTransporterRequest.Timeout; PutObjectRequest putRequest = new PutObjectRequest { BucketName = this._fileTransporterRequest.BucketName, Key = this._fileTransporterRequest.Key, CannedACL = this._fileTransporterRequest.CannedACL, ContentType = this._fileTransporterRequest.ContentType, FilePath = this._fileTransporterRequest.FilePath, Timeout = timeout, StorageClass = this._fileTransporterRequest.StorageClass, AutoCloseStream = this._fileTransporterRequest.AutoCloseStream, ServerSideEncryptionMethod = this._fileTransporterRequest.ServerSideEncryptionMethod, }; putRequest.PutObjectProgressEvent += new EventHandler<PutObjectProgressArgs>(this.putObjectProgressEventCallback); putRequest.InputStream = this._fileTransporterRequest.InputStream; if (this._fileTransporterRequest.metadata != null && this._fileTransporterRequest.metadata.Count > 0) putRequest.WithMetaData(this._fileTransporterRequest.metadata); if (this._fileTransporterRequest.Headers != null && this._fileTransporterRequest.Headers.Count > 0) putRequest.AddHeaders(this._fileTransporterRequest.Headers); this._s3Client.PutObject(putRequest); }
public string PutImage(IWebCamImage webCamImage, out string key) { if (webCamImage.Data != null) // accept the file { // AWS credentials. const string accessKey = "{sensitive}"; const string secretKey = "{sensitive}"; // Setup the filename. string fileName = Guid.NewGuid() + ".jpg"; string url = null; // Drop the image. AmazonS3 client; using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey)) { var request = new PutObjectRequest(); var config = ConfigurationManager.AppSettings; request.WithBucketName(config["awsBucket"]) .WithCannedACL(S3CannedACL.PublicRead) .WithKey(fileName).WithInputStream(new MemoryStream(webCamImage.Data)); S3Response response = client.PutObject(request); url = config["awsRootUrl"] + "/" + config["awsBucket"] + "/" + fileName; } // Return our result. key = fileName; return url; } key = null; return null; }
private bool UploadFile(string filePath, string s3directory) { //clean up s3 directory s3directory = s3directory.Replace('\\', '/'); if (s3directory.StartsWith("/")) s3directory = s3directory.Substring(1); if (Config.LowerCaseEnforce) s3directory = s3directory.ToLower(); var key = string.Format("{0}{1}{2}", s3directory, (string.IsNullOrEmpty(s3directory) ? null : "/"), Path.GetFileName(filePath)); PutObjectRequest request = new PutObjectRequest() { BucketName = _bucketName, Key = key, FilePath = filePath, }; PutObjectResponse response = this._client.PutObject(request); bool success = (response.HttpStatusCode == System.Net.HttpStatusCode.OK); if (success) Logger.log.Info("Successfully uploaded file " + filePath + " to " + key); else Logger.log.Info("Failed to upload file " + filePath); return success; }
public ActionResult UploadFile() { var file = Request.Files[0]; if (file != null && file.ContentLength > 0) { var account = _readOnlyRepository.First<Account>(x => x.Email == User.Identity.Name); var client = AWSClientFactory.CreateAmazonS3Client(); var putObjectRequest = new PutObjectRequest { BucketName = account.BucketName, Key = file.FileName, InputStream = file.InputStream }; client.PutObject(putObjectRequest); //save to database here Success("The file " + file.FileName + " was uploaded."); return RedirectToAction("Index"); } Error("There was a problem uploading the file."); return View("Upload"); }
public string Put(string key, Stream inputStream) { VerifyKey(key); key = ConvertKey(key); // Setting stream position is not necessary for AWS API // but we are bing explicit to demostrate that the position does matter depending on the underlying API. // For example, GridFs API does require position to be at zero! inputStream.Position = 0; var request = new PutObjectRequest { BucketName = BucketName, InputStream = inputStream, Key = key, CannedACL = S3CannedACL.PublicRead, AutoCloseStream = false, }; var response = S3.PutObject(request); inputStream.Position = 0; // Rewind the stream as the stream could be used again after the method returns. if (response.HttpStatusCode == HttpStatusCode.OK) { return BucketUrl + key; } return string.Empty; }
public TimeSpan CalculateDiffClocks() { try { SQ.Repository.File clockFile = new RemoteFile (Constant.CLOCK_TIME); PutObjectRequest putObject = new PutObjectRequest () { BucketName = bucketName, Key = clockFile.Name, ContentBody = string.Empty }; DateTime localClock; using (S3Response response = Connect ().PutObject (putObject)){ localClock = DateTime.Now; response.Dispose (); } ListObjectsResponse files = Connect ().ListObjects (new ListObjectsRequest ().WithBucketName (bucketName)); S3Object remotefile = files.S3Objects.Where (o => o.Key == clockFile.Name).FirstOrDefault(); string sRemoteclock = remotefile.LastModified; Delete(clockFile); DateTime remoteClock = Convert.ToDateTime (sRemoteclock); return localClock.Subtract(remoteClock); } catch(Exception e) { return new TimeSpan(0); Logger.LogInfo ("Connection","Fail to determinate a remote clock: "+e.Message +" \n"); } }
public override void Execute() { int timeout = this._config.DefaultTimeout; if (this._fileTransporterRequest.Timeout != 0) timeout = this._fileTransporterRequest.Timeout; PutObjectRequest putRequest = new PutObjectRequest() .WithBucketName(this._fileTransporterRequest.BucketName) .WithKey(this._fileTransporterRequest.Key) .WithCannedACL(this._fileTransporterRequest.CannedACL) .WithContentType(this._fileTransporterRequest.ContentType) .WithFilePath(this._fileTransporterRequest.FilePath) .WithTimeout(timeout) .WithStorageClass(this._fileTransporterRequest.StorageClass) .WithAutoCloseStream(this._fileTransporterRequest.AutoCloseStream) .WithSubscriber(new EventHandler<PutObjectProgressArgs>(this.putObjectProgressEventCallback)); putRequest.InputStream = this._fileTransporterRequest.InputStream; if (this._fileTransporterRequest.metadata != null && this._fileTransporterRequest.metadata.Count > 0) putRequest.WithMetaData(this._fileTransporterRequest.metadata); if (this._fileTransporterRequest.Headers != null && this._fileTransporterRequest.Headers.Count > 0) putRequest.AddHeaders(this._fileTransporterRequest.Headers); this._s3Client.PutObject(putRequest); }
public async Task<IEnumerable<string>> Post() { var result = new List<string>(); var watch = new Stopwatch(); var bucketRequest = new PutBucketRequest { BucketName = TestBucketName, BucketRegion = S3Region.EUC1 }; await _client.PutBucketAsync(bucketRequest); for (int i = 1; i < 11; i++) { watch.Restart(); var request = new PutObjectRequest { BucketName = TestBucketName, Key = $"test{i}.txt", ContentBody = "Testcontent" + i }; await _client.PutObjectAsync(request); watch.Stop(); result.Add($"S3 {i} try: {watch.ElapsedMilliseconds}"); } return result; }
/// <summary> /// Saves image to images.climbfind.com /// </summary> /// <param name="stream"></param> /// <param name="filePath"></param> /// <param name="key"></param> public override void SaveImage(Stream stream, string filePath, string key) { try { using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(Stgs.AWSAccessKey, Stgs.AWSSecretKey, S3Config)) { // simple object put PutObjectRequest request = new PutObjectRequest(); request.WithBucketName("images.climbfind.com" + filePath); request.WithInputStream(stream); request.ContentType = "image/jpeg"; request.Key = key; request.WithCannedACL(S3CannedACL.PublicRead); client.PutObject(request); } } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Please check the provided AWS Credentials."); Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3"); } else { Console.WriteLine("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message); } } }
private PutObjectRequest ConstructRequest() { PutObjectRequest putRequest = new PutObjectRequest() { BucketName = this._fileTransporterRequest.BucketName, Key = this._fileTransporterRequest.Key, CannedACL = this._fileTransporterRequest.CannedACL, ContentType = this._fileTransporterRequest.ContentType, StorageClass = this._fileTransporterRequest.StorageClass, AutoCloseStream = this._fileTransporterRequest.AutoCloseStream, AutoResetStreamPosition = this._fileTransporterRequest.AutoResetStreamPosition, ServerSideEncryptionMethod = this._fileTransporterRequest.ServerSideEncryptionMethod, Headers = this._fileTransporterRequest.Headers, Metadata = this._fileTransporterRequest.Metadata, #if (BCL && !BCL45) Timeout = ClientConfig.GetTimeoutValue(this._config.DefaultTimeout, this._fileTransporterRequest.Timeout) #endif }; #if BCL putRequest.FilePath = this._fileTransporterRequest.FilePath; #elif WIN_RT || WINDOWS_PHONE if (this._fileTransporterRequest.IsSetStorageFile()) { putRequest.StorageFile = this._fileTransporterRequest.StorageFile; } #endif var progressHandler = new ProgressHandler(this.PutObjectProgressEventCallback); putRequest.StreamUploadProgressCallback += progressHandler.OnTransferProgress; putRequest.InputStream = this._fileTransporterRequest.InputStream; return putRequest; }
static void SaveObjectInAws(Stream pObject, string keyname) { try { using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client()) { // simple object put PutObjectRequest request = new PutObjectRequest(); request.WithBucketName(ConfigurationManager.AppSettings["bucketname"]).WithKey(keyname).WithInputStream(pObject); using (client.PutObject(request)) { } } } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Please check the provided AWS Credentials."); Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3"); throw; } Console.WriteLine("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message); throw; } }
public BucketRegionTestRunner(bool useSigV4, bool useSigV4SetExplicitly = false) { originalUseSignatureVersion4 = AWSConfigsS3.UseSignatureVersion4; originalUseSigV4SetExplicitly = GetAWSConfigsS3InternalProperty(); AWSConfigsS3.UseSignatureVersion4 = useSigV4; SetAWSConfigsS3InternalProperty(useSigV4SetExplicitly); CreateAndCheckTestBucket(); if (TestBucketIsReady) { GetObjectMetadataRequest = new GetObjectMetadataRequest() { BucketName = TestBucket.BucketName, Key = TestObjectKey }; PutObjectRequest = new PutObjectRequest() { BucketName = TestBucket.BucketName, Key = TestObjectKey, ContentBody = TestContent }; PreSignedUrlRequest = new GetPreSignedUrlRequest { BucketName = BucketName, Key = BucketRegionTestRunner.TestObjectKey, Expires = DateTime.Now.AddHours(1) }; } }
public static bool CreateFileFromStream(Stream InputStream, string FileName, string _bucketName = "doc2xml") { bool _saved=false; try { IAmazonS3 client; AmazonS3Config objCon = new AmazonS3Config() ; objCon.RegionEndpoint = RegionEndpoint.USEast1; using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(_awsAccessKey, _awsSecretKey,objCon)) { var request = new PutObjectRequest() { BucketName = _bucketName, CannedACL = S3CannedACL.PublicRead,//PERMISSION TO FILE PUBLIC ACCESIBLE Key = string.Format("{0}", FileName), InputStream = InputStream//SEND THE FILE STREAM }; client.PutObject(request); _saved = true; } } catch (Exception ex) { ex.ToString(); } return _saved; }
/// <summary> /// 上传文件 /// </summary> /// <param name="s3Folder"></param> /// <param name="localFilePath"></param> public static void UploadFile(string s3Folder, string localFilePath) { FileInfo fileInf = new FileInfo(localFilePath); //设置存储路径及上传区域 CognitoAWSCredentials credentials = new CognitoAWSCredentials( endpoint, RegionEndpoint.CNNorth1 ); try { //创建客户端及上传文件 using (var s3Client = new AmazonS3Client(RegionEndpoint.CNNorth1)) { var request = new Amazon.S3.Model.PutObjectRequest() { BucketName = bucketName, Key = s3Folder + "/" + fileInf.Name, FilePath = localFilePath, //ContentType = "text/plain" }; //request.Metadata.Add("x-amz-meta-title", "someTitle"); PutObjectResponse response2 = s3Client.PutObject(request); LogHelper.WriteLog("上传图片信息:" + JsonConvert.SerializeObject(response2)); } } catch (AmazonS3Exception) { throw; } }
public string UploadFile(string filename, HttpPostedFileBase file) { var client = new AmazonS3Client(accessKey, AWSSecretKey, Amazon.RegionEndpoint.USEast1); try { PutObjectRequest putRequest = new PutObjectRequest { BucketName = bucketName, Key = filename, InputStream = file.InputStream }; PutObjectResponse response = client.PutObject(putRequest); return("https://xohricontentimages.s3.us-east-2.amazonaws.com/" + filename); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { throw new Exception("Check the provided AWS Credentials."); } else { throw new Exception("Error occurred: " + amazonS3Exception.Message); } } }
public override void Execute() { PutObjectRequest putRequest = new PutObjectRequest() { BucketName = this._fileTransporterRequest.BucketName, Key = this._fileTransporterRequest.Key, CannedACL = this._fileTransporterRequest.CannedACL, ContentType = this._fileTransporterRequest.ContentType, FilePath = this._fileTransporterRequest.FilePath, StorageClass = this._fileTransporterRequest.StorageClass, AutoCloseStream = this._fileTransporterRequest.AutoCloseStream, AutoResetStreamPosition = this._fileTransporterRequest.AutoResetStreamPosition, ServerSideEncryptionMethod = this._fileTransporterRequest.ServerSideEncryptionMethod, Headers = this._fileTransporterRequest.Headers, Metadata = this._fileTransporterRequest.Metadata, #if (BCL && !BCL45) Timeout = ClientConfig.GetTimeoutValue(this._config.DefaultTimeout, this._fileTransporterRequest.Timeout) #endif }; putRequest.StreamUploadProgressCallback += new EventHandler<Runtime.StreamTransferProgressArgs>(this.putObjectProgressEventCallback); putRequest.InputStream = this._fileTransporterRequest.InputStream; this._s3Client.PutObject(putRequest); }
public async Task<bool> Save(Stream stream, string path) { using (var api = AmazonS3Helper.GetApi(_account)) { string bucket; string filename; GetBucketAndKey(path, out bucket, out filename); try // Does parent folder exists? { var folderName = CloudPath.GetDirectoryName(filename); var getResponse = await api.GetObjectMetadataAsync(bucket, folderName + "/"); } catch (AmazonS3Exception ex) { if (ex.StatusCode == System.Net.HttpStatusCode.NotFound) return false; //status wasn't not found, so throw the exception throw; } var request = new PutObjectRequest { BucketName = bucket, Key = filename, InputStream = stream }; var response = await api.PutObjectAsync(request); return response != null; } }
public void Write(string key, Stream stream) { try { using (var s3 = AWSClientFactory.CreateAmazonS3Client(Settings.AccessKey, Settings.SecretAccessKey, RegionEndpoint.EUWest1)) { stream.Position = 0; var request = new PutObjectRequest { BucketName = Settings.UploadBucket, CannedACL = S3CannedACL.PublicRead, InputStream = stream, AutoCloseStream = true, ContentType = "application/octet-stream", Key = key }; s3.PutObject(request); } } catch (AmazonS3Exception e) { throw new UnhandledException("Upload failed", e); } }
protected void UploadFileButton_Click(object sender, EventArgs e) { string filePath = Server.MapPath(VideoUploader.PostedFile.FileName); string existingBucketName = "ccdem"; string keyName = Membership.GetUser().ProviderUserKey.GetHashCode().ToString(); string fileName = UtilityFunctions.GenerateChar() + VideoUploader.PostedFile.FileName; IAmazonS3 client; using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(System.Web.Configuration.WebConfigurationManager.AppSettings[0].ToString(), System.Web.Configuration.WebConfigurationManager.AppSettings[1].ToString())) { var stream = VideoUploader.FileContent; stream.Position = 0; PutObjectRequest request = new PutObjectRequest(); request.InputStream = stream; request.BucketName = existingBucketName; request.CannedACL = S3CannedACL.PublicRead; request.Key = keyName + "/" + fileName; PutObjectResponse response = client.PutObject(request); } string bucketUrl = "https://s3-us-west-2.amazonaws.com/" + existingBucketName + "/" + keyName + "/" + fileName; cloudFrontUrl = cloudFrontUrl + keyName + "/" + fileName; TranscoderUtility.Transcode(keyName + "/" + fileName, keyName + "/mob_" + fileName, existingBucketName); // lblPath.Text = "<br/>Successfully uploaded into S3:" + bucketUrl + "<br/> Cloudfront distribution url is " + cloudFrontUrl; Models.Video video = new Models.Video() { Url = cloudFrontUrl }; int newVid = DAL.DataAccessLayer.AddVideo(video, (Guid)Membership.GetUser().ProviderUserKey); string vid=Request.QueryString["vid"].ToString(); if(!vid.Equals("")) DAL.DataAccessLayer.AddResponseVideo(vid,newVid.ToString()); RefreshPage(); }
public void UploadFile() { var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast2); try { Amazon.S3.Model.PutObjectRequest putRequest = new Amazon.S3.Model.PutObjectRequest { BucketName = bucketName, Key = keyName, FilePath = filePath, ContentType = "application/vnd.ms-excel" }; PutObjectResponse response = client.PutObject(putRequest); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { throw new Exception("Check the aws credentials"); } else { throw new Exception("Error occured: " + amazonS3Exception.Message); } } }
private async void ButtonUpload_Click(object sender, RoutedEventArgs e) { cts = new CancellationTokenSource(); FileListBox.SelectedItems.Clear(); while (FileListBox.Items.Count > 0) { FileListBox.SelectedIndex = 0; ButtonCancelUpload.IsEnabled = true; UploadProgress.Visibility = Visibility.Visible; UploadProgress.Value = 0; string path = FileListBox.SelectedItem.ToString(); string extension = Path.GetExtension(path); string filename = Path.GetFileName(path); string contentType = AmazonS3Util.MimeTypeFromExtension(extension); FilePathTextBlock.Text = filename; try { using (FileStream fs = File.OpenRead(path)) { var streamRequest = new PutObjectRequest { BucketName = S3Client.BucketName, Key = filename, InputStream = fs, ContentType = contentType, CannedACL = S3CannedACL.PublicRead, ReadWriteTimeout = TimeSpan.FromHours(1) }; streamRequest.StreamTransferProgress += OnUploadProgress; await S3Client.Client.PutObjectAsync(streamRequest, cts.Token); FileListBox.Items.Remove(FileListBox.SelectedItem); } } catch (OperationCanceledException) { MessageBox.Show(filename + " Upload Cancelled"); break; } catch (AmazonS3Exception amazonS3Exception) { MessageBox.Show(amazonS3Exception.Message); break; } catch (Exception) { MessageBox.Show("Other exception"); break; } finally { FilePathTextBlock.Text = ""; UploadProgress.Value = 0; UploadProgress.Visibility = Visibility.Hidden; ButtonCancelUpload.IsEnabled = false; } } }
public async System.Threading.Tasks.Task<IHttpActionResult> PostUpload(string folder, string filekey) { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var root = HttpContext.Current.Server.MapPath("~/App_Data/Temp/FileUploads"); if (!Directory.Exists(root)) { Directory.CreateDirectory(root); } var provider = new MultipartFormDataStreamProvider(root); try { var result = await Request.Content.ReadAsMultipartAsync(provider); } catch (Exception ex) { } try { string bucketName = "aws-yeon-test-fims-support"; var credentials = new StoredProfileAWSCredentials("s3"); IAmazonS3 client = new AmazonS3Client(credentials, Amazon.RegionEndpoint.APNortheast2); foreach (var file in provider.FileData) { PutObjectRequest putRequest = new PutObjectRequest { BucketName = bucketName, Key = file.Headers.ContentDisposition.FileName.Substring(1, file.Headers.ContentDisposition.FileName.Length - 2), FilePath = file.LocalFileName //ContentType = "text/plain" }; putRequest.Headers.ContentLength = 168059; PutObjectResponse response = client.PutObject(putRequest); } } catch (AmazonS3Exception amazonS3Exception) { return InternalServerError(amazonS3Exception); } catch (Exception ex) { return InternalServerError(ex); } finally { } return Ok(); }
public ActionResult CreateFolder(string folderName, string clientDateTime) { if (folderName.Length > 25) { Error("Folder name should be 25 characters maximum!!!"); return RedirectToAction("ListAllContent"); } var userData = _readOnlyRepository.First<Account>(x => x.EMail == User.Identity.Name); if (userData.Files.Count(l=>l.Name==folderName)>0) { Error("Folder already exists!!!"); return RedirectToAction("ListAllContent"); } var actualPath = Session["ActualPath"].ToString(); var putFolder = new PutObjectRequest { BucketName = userData.BucketName, Key = actualPath+folderName+"/", ContentBody = string.Empty }; AWSClient.PutObject(putFolder); AddActivity("El usuario ha creado el folder "+folderName); //var serverFolderPath = Server.MapPath("~/App_Data/UploadedFiles/" + actualPath + "/"+folderName); //var folderInfo = new DirectoryInfo(serverFolderPath); //if (folderInfo.Exists) //{ // Error("Folder already exists!!!"); // return RedirectToAction("ListAllContent"); //} var clientDate = Convert.ToDateTime(clientDateTime); userData.Files.Add(new File { Name = folderName, CreatedDate = clientDate, FileSize = 0, IsArchived = false, IsDirectory = true, ModifiedDate = clientDate, Type = "", Url = actualPath }); //var result=Directory.CreateDirectory(serverFolderPath); //if(!result.Exists) // Error("The folder was not created!!! Try again please!!!"); //else //{ Success("The folder was created successfully!!!"); _writeOnlyRepository.Update(userData); //} return RedirectToAction("ListAllContent"); }
/// <summary>DEPRECATED: Execute the NAnt task</summary> /// This task exists ONLY to satisfy compatibilty with older versions of the task and script that rely on it protected override void ExecuteTask() { // Ensure the configured bucket exists if (!BucketExists(BucketName)) { //Project.Log(Level.Error, "[ERROR] S3 Bucket '{0}' not found!", BucketName); S3CreateBucketTask cb = new S3CreateBucketTask(); try { cb.CreateBucket(); } catch (Exception ex) { Project.Log(Level.Error, "[ERROR] Error creating bucket. Msg: \r\n" + ex); } return; } // Ensure the specified file exists if (!File.Exists(FilePath)) { Project.Log(Level.Error, "[ERROR] Local file '{0}' doesn't exist!", FilePath); return; } // Ensure the overwrite is false and the file doesn't already exist in the specified bucket if (!Overwrite && FileExists(FileName)) return; // Send the file to S3 using (Client) { try { Project.Log(Level.Info, "Uploading file: {0}", FileName); PutObjectRequest request = new PutObjectRequest { Key = FileName, BucketName = BucketName, FilePath = FilePath, Timeout = timeout }; var response = Client.PutObject(request); } catch (AmazonS3Exception ex) { ShowError(ex); } } if (!FileExists(FileName)) Project.Log(Level.Error, "Upload FAILED!"); else Project.Log(Level.Info, "Upload successful!"); }
public static string CreateNewFolder(AmazonS3 client, string foldername) { String S3_KEY = foldername; PutObjectRequest request = new PutObjectRequest(); request.WithBucketName(BUCKET_NAME); request.WithKey(S3_KEY); request.WithContentBody(""); client.PutObject(request); return S3_KEY; }
public void CreateObject(string bucketName, Stream fileStream) { PutObjectRequest request = new PutObjectRequest(); request.WithBucketName(bucketName); request.WithInputStream(fileStream); request.CannedACL = S3CannedACL.PublicRead; S3Response response = _client.PutObject(request); response.Dispose(); }
public void PutScreenshot(Amazon.S3.AmazonS3 amazonS3Client, string bucketName, string path, Stream stream) { var putObjectRequest = new PutObjectRequest(); putObjectRequest.WithInputStream(stream); putObjectRequest.WithBucketName(bucketName); putObjectRequest.WithKey(path); amazonS3Client.PutObject(putObjectRequest); }
private void EditFile(HttpChallenge httpChallenge, bool delete, TextWriter msg) { var filePath = httpChallenge.FilePath; // We need to strip off any leading '/' in the path or // else it creates a path with an empty leading segment if (filePath.StartsWith("/")) { filePath = filePath.Substring(1); } using (var s3 = new Amazon.S3.AmazonS3Client( CommonParams.ResolveCredentials(), CommonParams.RegionEndpoint)) { if (delete) { LOG.Debug("Deleting S3 object at Bucket [{0}] and Key [{1}]", BucketName, filePath); var s3Requ = new Amazon.S3.Model.DeleteObjectRequest { BucketName = BucketName, Key = filePath, }; var s3Resp = s3.DeleteObject(s3Requ); if (LOG.IsDebugEnabled) { LOG.Debug("Delete response: [{0}]", NLog.Targets.DefaultJsonSerializer.Instance.SerializeObject(s3Resp)); } msg.WriteLine("* Challenge Response has been deleted from S3"); msg.WriteLine(" at Bucket/Key: [{0}/{1}]", BucketName, filePath); } else { var s3Requ = new Amazon.S3.Model.PutObjectRequest { BucketName = BucketName, Key = filePath, ContentBody = httpChallenge.FileContent, ContentType = ContentType, CannedACL = S3CannedAcl, }; var s3Resp = s3.PutObject(s3Requ); msg.WriteLine("* Challenge Response has been written to S3"); msg.WriteLine(" at Bucket/Key: [{0}/{1}]", BucketName, filePath); msg.WriteLine("* Challenge Response should be accessible with a MIME type of [text/json]"); msg.WriteLine(" at: [{0}]", httpChallenge.FileUrl); } } }
public async Task CreateFolder(string bucketName, string folderName, string awsAccessKey, string awsSecretKey) { var client = new AmazonS3Client(awsAccessKey, awsSecretKey, RegionEndpoint.APSouth1); var folderKey = folderName + "/"; //end the folder name with "/" var request = new Amazon.S3.Model.PutObjectRequest() { BucketName = bucketName, Key = folderKey // <-- in S3 key represents a path }; //request.StorageClass = S3StorageClass.Standard; //request.ServerSideEncryptionMethod = ServerSideEncryptionMethod.None; //request.CannedACL = S3CannedACL.BucketOwnerFullControl; var response = await client.PutObjectAsync(request); }
/// <summary> /// Allows to upload a Text Object to a S3 Bucket. Object Size is limited to IRIS string size. /// https://docs.aws.amazon.com/AmazonS3/latest/dev/UploadingObjects.html /// </summary> /// <param name="key">The Object name in S3</param> /// <param name="content">The String to upload as the Object Content</param> /// <returns></returns> public string PutText(string key, string content) { LOGINFO("PutText, key=" + key); var putRequest = new Amazon.S3.Model.PutObjectRequest { BucketName = awsBucket, Key = key, ContentBody = content }; var putResponse = s3Client.PutObjectAsync(putRequest).Result; return(putResponse.HttpStatusCode.ToString()); }
/// <summary> /// Uploads a fileStream to S3. This shoudl be used for files smaller than 5MB /// For files graeate than 5MB, AWS recommends using Multiparts upload /// </summary> /// <param name="key"></param> /// <param name="filename"></param> /// <returns></returns> public string PutStream(string key, string filename) { LOGINFO("PutStream, key=" + key); var putRequest = new Amazon.S3.Model.PutObjectRequest { BucketName = awsBucket, Key = key, FilePath = filename, ContentType = "text/plain" }; // public virtual Task<PutObjectResponse> PutObjectAsync(PutObjectRequest request, CancellationToken cancellationToken = default); var putResponse = s3Client.PutObjectAsync(putRequest).Result; return(putResponse.HttpStatusCode.ToString()); }
/// <summary> /// Put object /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> private async Task <PutObjectResponse> PutObjectAsync(Amazon.S3.Model.PutObjectRequest request, CancellationToken cancellationToken = default) { if (request == null) { throw new ArgumentNullException(nameof(request)); } this.Logger.LogTrace(JsonConvert.SerializeObject(value: request)); var response = await this.Repository.PutObjectAsync(request : request, cancellationToken : cancellationToken == default?this.CancellationToken.Token : cancellationToken); this.Logger.LogTrace(JsonConvert.SerializeObject(value: response)); return(response); }
public async Task <PutObjectResponse> PutObjectAsync(string bucket, string key, string contents, S3CannedACL s3CannedAcl = null, Encoding encoding = null, CancellationToken cancellationToken = default) { this.Logger.LogDebug($"[{nameof(this.PutObjectAsync)}]"); this.Logger.LogTrace(JsonConvert.SerializeObject(new { bucket, key, s3CannedAcl, encoding })); if (string.IsNullOrWhiteSpace(bucket)) { throw new ArgumentNullException(nameof(bucket)); } if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentNullException(nameof(key)); } if (string.IsNullOrWhiteSpace(contents)) { throw new ArgumentNullException(nameof(contents)); } if (s3CannedAcl == null) { s3CannedAcl = S3CannedACL.BucketOwnerFullControl; } var filePath = this.CreateTempFile(contents: contents, encoding: encoding); var request = new Amazon.S3.Model.PutObjectRequest { BucketName = bucket, CannedACL = s3CannedAcl, FilePath = filePath, Key = key, }; return(await this.PutObjectAsync(request : request, cancellationToken : cancellationToken)); }
private void EditFile(HttpChallenge httpChallenge, bool delete) { var filePath = httpChallenge.FilePath; // We need to strip off any leading '/' in the path or // else it creates a path with an empty leading segment if (filePath.StartsWith("/")) { filePath = filePath.Substring(1); } using (var s3 = new Amazon.S3.AmazonS3Client( CommonParams.ResolveCredentials(), CommonParams.RegionEndpoint)) { if (delete) { var s3Requ = new Amazon.S3.Model.DeleteObjectRequest { BucketName = BucketName, Key = filePath, }; var s3Resp = s3.DeleteObject(s3Requ); } else { var s3Requ = new Amazon.S3.Model.PutObjectRequest { BucketName = BucketName, Key = filePath, ContentBody = httpChallenge.FileContent, ContentType = ContentType, CannedACL = S3CannedAcl, }; var s3Resp = s3.PutObject(s3Requ); } } }
public string UploadFile(S3Request model) { //- Build Put Object var s3PutRequest = new Amazon.S3.Model.PutObjectRequest { FilePath = model.FilePath, BucketName = S3Bucket, CannedACL = Amazon.S3.S3CannedACL.PublicRead }; if (!string.IsNullOrWhiteSpace(model.NewFileName)) { s3PutRequest.Key = model.NewFileName; } try { Amazon.S3.Model.PutObjectResponse s3PutResponse = this.S3Client.PutObject(s3PutRequest); if (model.DeleteLocalFileOnSuccess) { if (System.IO.File.Exists(model.FilePath)) { System.IO.File.Delete(model.FilePath); } } } catch (Exception ex) { throw ex; } string url = ServiceUrl + "/" + S3Bucket + "/" + model.NewFileName; return(url); }
public async Task AutoThumbnail(S3Event evnt, ILambdaContext context) { var logger = context.Logger; logger.LogLine($"CreateThumbnail3: request {JsonConvert.SerializeObject(evnt)}"); logger.LogLine($"CreateThumbnail4: context {JsonConvert.SerializeObject(context)}"); var srcBucket = evnt.Records[0].S3.Bucket.Name; var dstBucket = "ygubbay-photo-albums-thumbnails"; var fileKey = HttpUtility.UrlDecode(evnt.Records[0].S3.Object.Key); var imageType = System.IO.Path.GetExtension(fileKey).Replace(".", "").ToLower(); logger.LogLine($"Image type: " + imageType); if (imageType != "jpg" && imageType != "png") { logger.LogLine($"Uploaded file: {evnt.Records[0].S3.Object.Key}. Unsupported image type: ${imageType}. No thumbnail created."); return; } logger.LogLine($"Getting s3client"); var s3Client = new AmazonS3Client(Amazon.RegionEndpoint.EUWest2); logger.LogLine($"try GetObjectAsync: srcBucket [{srcBucket}], fileKey [{fileKey}]"); var origimage = await s3Client.GetObjectAsync(srcBucket, fileKey); logger.LogLine($"GetObjectAsync: {origimage.BucketName}/{origimage.Key} successful"); using (Stream responseStream = origimage.ResponseStream) { using (var outStream = new MemoryStream()) { IImageFormat imgFormat; using (var thumbImg = Image.Load(responseStream, out imgFormat)) { var origSize = thumbImg.Size(); if (origSize.Width > 200) { thumbImg.Mutate(x => x.Resize(new ResizeOptions { Mode = ResizeMode.Crop, Position = AnchorPositionMode.Center, Size = new SixLabors.ImageSharp.Size(200, Convert.ToInt32(origSize.Height / origSize.Width * 200)) }) .AutoOrient()); } thumbImg.Save(outStream, imgFormat); Amazon.S3.Model.PutObjectRequest putRequest = new Amazon.S3.Model.PutObjectRequest( ); putRequest.BucketName = dstBucket; putRequest.ContentType = "image/" + imageType; putRequest.Key = fileKey; putRequest.InputStream = outStream; await s3Client.PutObjectAsync(putRequest); } } } logger.LogLine($"Successfully resized {srcBucket}/{fileKey} and uploaded to {dstBucket}/{fileKey}"); }
private Amazon.S3.Model.PutObjectResponse CallAWSServiceOperation(IAmazonS3 client, Amazon.S3.Model.PutObjectRequest request) { try { #if DESKTOP return(client.PutObject(request)); #elif CORECLR return(client.PutObjectAsync(request).GetAwaiter().GetResult()); #else #error "Unknown build edition" #endif } catch (AmazonServiceException exc) { var webException = exc.InnerException as System.Net.WebException; if (webException != null) { throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException); } throw; } }
private PutObjectRequest ConstructRequest() { PutObjectRequest putRequest = new PutObjectRequest() { Headers = this._fileTransporterRequest.Headers, BucketName = this._fileTransporterRequest.BucketName, Key = this._fileTransporterRequest.Key, CannedACL = this._fileTransporterRequest.CannedACL, StorageClass = this._fileTransporterRequest.StorageClass, AutoCloseStream = this._fileTransporterRequest.AutoCloseStream, AutoResetStreamPosition = this._fileTransporterRequest.AutoResetStreamPosition, ServerSideEncryptionMethod = this._fileTransporterRequest.ServerSideEncryptionMethod, ServerSideEncryptionCustomerMethod = this._fileTransporterRequest.ServerSideEncryptionCustomerMethod, ServerSideEncryptionCustomerProvidedKey = this._fileTransporterRequest.ServerSideEncryptionCustomerProvidedKey, ServerSideEncryptionCustomerProvidedKeyMD5 = this._fileTransporterRequest.ServerSideEncryptionCustomerProvidedKeyMD5, ServerSideEncryptionKeyManagementServiceKeyId = this._fileTransporterRequest.ServerSideEncryptionKeyManagementServiceKeyId, Metadata = this._fileTransporterRequest.Metadata, #if (BCL && !BCL45) Timeout = ClientConfig.GetTimeoutValue(this._config.DefaultTimeout, this._fileTransporterRequest.Timeout) #endif }; // Avoid setting ContentType to null, as that may clear // out an existing value in Headers collection if (!string.IsNullOrEmpty(this._fileTransporterRequest.ContentType)) putRequest.ContentType = this._fileTransporterRequest.ContentType; putRequest.FilePath = this._fileTransporterRequest.FilePath; var progressHandler = new ProgressHandler(this.PutObjectProgressEventCallback); ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)putRequest).StreamUploadProgressCallback += progressHandler.OnTransferProgress; ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)putRequest).AddBeforeRequestHandler(this.RequestEventHandler); putRequest.InputStream = this._fileTransporterRequest.InputStream; return putRequest; }