public string PutRequest(string key, Stream data, string contentType) { key = key.Replace('\\', '/'); var putRequest = new PutObjectRequest() .WithBucketName(_configuration.BucketName) .WithCannedACL(S3CannedACL.PublicRead) .WithContentType(contentType) .WithKey(key) .WithMetaData("Content-Length", data.Length.ToString(CultureInfo.InvariantCulture)) .WithStorageClass(S3StorageClass.Standard); putRequest.WithInputStream(data); _client.PutObject(putRequest); return string.Concat(_baseStorageEndpoint, key); }
/// <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); } } }
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); }
public string CreateObject(string bucketName, Stream fileStream, string contentType, string objectKey) { PutObjectRequest request = new PutObjectRequest(); request.WithBucketName(bucketName).WithContentType(contentType).WithKey(objectKey); request.WithInputStream(fileStream); request.CannedACL = S3CannedACL.PublicRead; S3Response response = _client.PutObject(request); response.Dispose(); return response.RequestId; }
/// <summary> /// The save file. /// </summary> /// <param name="awsAccessKey"> /// The AWS access key. /// </param> /// <param name="awsSecretKey"> /// The AWS secret key. /// </param> /// <param name="bucket"> /// The bucket. /// </param> /// <param name="path"> /// The path. /// </param> /// <param name="fileStream"> /// The file stream. /// </param> public static void SaveFile(string awsAccessKey, string awsSecretKey, string bucket, string path, Stream fileStream) { using (var amazonClient = AWSClientFactory.CreateAmazonS3Client(awsAccessKey, awsSecretKey)) { var createFileRequest = new PutObjectRequest { CannedACL = S3CannedACL.PublicRead, Timeout = int.MaxValue }; createFileRequest.WithKey(path); createFileRequest.WithBucketName(bucket); createFileRequest.WithInputStream(fileStream); amazonClient.PutObject(createFileRequest); } }
protected void Add(string bucketName, Guid id, object newItem) { string serializedUser = JsonConvert.SerializeObject(newItem); byte[] byteStream = Encoding.UTF8.GetBytes(serializedUser); var metadata = new NameValueCollection(); metadata.Add(IdKey, id.ToString()); metadata.Add(SizeKey, byteStream.Length.ToString()); metadata.Add(CreationTimeKey, DateTime.UtcNow.ToString()); metadata.Add(LastWriteTimeKey, DateTime.UtcNow.ToString()); var stream = new MemoryStream(); stream.Write(byteStream, 0, byteStream.Length); var req = new PutObjectRequest(); req.WithInputStream(stream); using (AmazonS3Client client = SetupClient()) { client.PutObject(req.WithBucketName(bucketName).WithKey(id.ToString()).WithMetaData(metadata)); } }
public void WriteFile(string virtualPath, Stream inputStream) { var request = new PutObjectRequest() .WithMetaData("Expires", DateTime.Now.AddYears(10).ToString("R")) .WithBucketName(this.bucketName) .WithCannedACL(S3CannedACL.PublicRead) .WithTimeout(60 * 60 * 1000) // 1 hour .WithReadWriteTimeout(60 * 60 * 1000) // 1 hour .WithKey(FixPathForS3(virtualPath)); var contentType = virtualPath.Substring(virtualPath.LastIndexOf(".", StringComparison.Ordinal)); if (string.IsNullOrWhiteSpace(contentType)) { request.ContentType = contentType; } request.WithInputStream(inputStream); using (this.s3.PutObject(request)) { } if (FileWritten != null) FileWritten.Invoke(this, new FileEventArgs(FixPathForN2(virtualPath), null)); }
/// <summary> /// Copies an InputStream to Amazon S3 and grants access to MessageGears. /// </summary> /// <param name="stream"> /// An input stream for the data to be copied to S3. /// </param> /// <param name="bucketName"> /// The name of the S3 bucket where the file will be copied. /// </param> /// <param name="key"> /// The S3 key of the file to be created. /// </param> public void PutS3File(Stream stream, String bucketName, String key) { // Check to see if the file already exists in S3 ListObjectsRequest listRequest = new ListObjectsRequest().WithBucketName(bucketName).WithPrefix(key); ListObjectsResponse listResponse = listFiles(listRequest); if(listResponse.S3Objects.Count > 0) { String message = "File " + key + " already exists."; log.Warn("PutS3File failed: " + message); throw new ApplicationException(message); } // Copy a file to S3 PutObjectRequest request = new PutObjectRequest().WithKey(key).WithBucketName(bucketName).WithTimeout(properties.S3PutTimeoutSecs * 1000); request.WithInputStream(stream); putWithRetry(request); setS3PermissionsWithRetry(bucketName, key); log.Info("PutS3File successful: " + key); }
public override string SavePrivate(string domain, string path, Stream stream, DateTime expires) { using (AmazonS3 client = GetClient()) { var request = new PutObjectRequest(); string objectKey = MakePath(domain, path); request.WithBucketName(_bucket) .WithKey(objectKey) .WithCannedACL(S3CannedACL.BucketOwnerFullControl) .WithContentType("application/octet-stream") .WithMetaData("private-expire", expires.ToFileTimeUtc().ToString()); var headers = new NameValueCollection(); headers.Add("Cache-Control", string.Format("public, maxage={0}", (int)TimeSpan.FromDays(5).TotalSeconds)); headers.Add("Etag", (DateTime.UtcNow.Ticks).ToString()); headers.Add("Last-Modified", DateTime.UtcNow.ToString("R")); headers.Add("Expires", DateTime.UtcNow.Add(TimeSpan.FromDays(5)).ToString("R")); headers.Add("Content-Disposition", "attachment"); request.AddHeaders(headers); request.WithInputStream(stream); client.PutObject(request); //Get presigned url GetPreSignedUrlRequest pUrlRequest = new GetPreSignedUrlRequest() .WithBucketName(_bucket) .WithExpires(expires) .WithKey(objectKey) .WithProtocol(Protocol.HTTP) .WithVerb(HttpVerb.GET); string url = client.GetPreSignedURL(pUrlRequest); //TODO: CNAME! return url; } }
public Uri Save(string domain, string path, Stream stream, string contentType, string contentDisposition, ACL acl) { bool postWriteCheck = false; if (QuotaController != null) { try { QuotaController.QuotaUsedAdd(_modulename, domain, _dataList.GetData(domain), stream.Length); } catch (Exception) { postWriteCheck = true; } } using (AmazonS3 client = GetClient()) { var request = new PutObjectRequest(); string mime = string.IsNullOrEmpty(contentType) ? MimeMapping.GetMimeMapping(Path.GetFileName(path)) : contentType; request.WithBucketName(_bucket) .WithKey(MakePath(domain, path)) .WithCannedACL(acl == ACL.Auto ? GetDomainACL(domain) : GetS3Acl(acl)) .WithContentType(mime); var headers = new NameValueCollection(); headers.Add("Cache-Control", string.Format("public, maxage={0}", (int)TimeSpan.FromDays(5).TotalSeconds)); headers.Add("Etag", (DateTime.UtcNow.Ticks).ToString()); headers.Add("Last-Modified", DateTime.UtcNow.ToString("R")); headers.Add("Expires", DateTime.UtcNow.Add(TimeSpan.FromDays(5)).ToString("R")); if (!string.IsNullOrEmpty(contentDisposition)) { headers.Add("Content-Disposition", contentDisposition); } else if (mime == "application/octet-stream") { headers.Add("Content-Disposition", "attachment"); } request.AddHeaders(headers); //Send body var buffered = stream.GetBuffered(); if (postWriteCheck) { QuotaController.QuotaUsedAdd(_modulename, domain, _dataList.GetData(domain), buffered.Length); } request.AutoCloseStream = false; request.WithInputStream(buffered); client.PutObject(request); InvalidateCloudFront(MakePath(domain, path)); return GetUri(domain, path); } }
private static void UploadFile(FileInfo fileInfo) { System.Console.WriteLine("Uploading " + fileInfo.Name); using (FileStream inputFileStream = new FileStream(fileInfo.FullName, FileMode.Open)) using (MemoryStream outputMemoryStream = new MemoryStream()) using (CryptoStream cryptoStream = new CryptoStream(outputMemoryStream, _aesEncryptor, CryptoStreamMode.Write)) { int data; while ((data = inputFileStream.ReadByte()) != -1) { cryptoStream.WriteByte((byte)data); } cryptoStream.FlushFinalBlock(); PutObjectRequest createRequest = new PutObjectRequest(); createRequest.WithMetaData("x-amz-meta-LWT", fileInfo.LastWriteTime.ToString("G")); createRequest.WithBucketName(BucketName); createRequest.WithKey(fileInfo.Name); createRequest.WithInputStream(outputMemoryStream); _amazonS3Client.PutObject(createRequest); } }
private void UploadToS3(string backupPath, PeriodicBackupSetup localBackupConfigs) { var awsRegion = RegionEndpoint.GetBySystemName(localBackupConfigs.AwsRegionEndpoint) ?? RegionEndpoint.USEast1; using (var client = new Amazon.S3.AmazonS3Client(awsAccessKey, awsSecretKey, awsRegion)) using (var fileStream = File.OpenRead(backupPath)) { var key = Path.GetFileName(backupPath); var request = new PutObjectRequest(); request.WithMetaData("Description", GetArchiveDescription()); request.WithInputStream(fileStream); request.WithBucketName(localBackupConfigs.S3BucketName); request.WithKey(key); using (client.PutObject(request)) { logger.Info(string.Format("Successfully uploaded backup {0} to S3 bucket {1}, with key {2}", Path.GetFileName(backupPath), localBackupConfigs.S3BucketName, key)); } } }
private static void CopyImage(object o) { UserData u = (UserData)o; try { string imagePath = String.Format("{0}\\L{1:00}\\R{2:x8}\\C{3:x8}.{4}", TileDirectory, u.Level, u.Row, u.Column, ContentType == "image/png" ? "png" : "jpg"); if (File.Exists(imagePath)) { byte[] file = File.ReadAllBytes(imagePath); PutObjectRequest putObjectRequest = new PutObjectRequest(); putObjectRequest.WithBucketName(BucketName); putObjectRequest.WithKey(String.Format("{0}/{1}/{2}/{3}", MapName, u.Level, u.Row, u.Column)); putObjectRequest.WithInputStream(new MemoryStream(file)); putObjectRequest.WithContentType(ContentType); putObjectRequest.WithCannedACL(S3CannedACL.PublicRead); _s3Client.PutObject(putObjectRequest); } } catch (Exception ex) { } finally { _threadPool.Release(); } }
/// <summary> /// To upload an image to amazon service bucket. /// </summary> /// <param name="imageBytesToUpload">image in byte array</param> /// <param name="contentType">content type of the image.</param> /// <returns>Url of the image from amazon web service</returns> public string UploadImage(byte[] imageBytesToUpload, string contentType) { try { using (var client = GetClient) { string keyName = Guid.NewGuid().ToString(); using (MemoryStream mStream = new MemoryStream(imageBytesToUpload)) { PutObjectRequest request = new PutObjectRequest(); request.WithBucketName(ImagesBucketName) .WithKey(keyName); request.WithContentType(contentType); request.WithInputStream(mStream); S3Response response = client.PutObject(request); response.Dispose(); } GetPreSignedUrlRequest requestUrl = new GetPreSignedUrlRequest() .WithBucketName(ImagesBucketName) .WithKey(keyName) .WithProtocol(Protocol.HTTP) .WithExpires(DateTime.Now.AddYears(10)); return client.GetPreSignedURL(requestUrl); } } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { throw new Exception("Please check the provided AWS Credentials."); } else { throw new Exception(string.Format("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message)); } } }
/// <summary> /// To upload a agreement pdf to the svn amazon web service. /// </summary> /// <param name="pdfBytesToUpload">agreement pdf in byte array</param> /// <param name="pdfDocumentId">agreement id of the customer object</param> public void UploadDocument(byte[] pdfBytesToUpload, string pdfDocumentId, DocType documentType = DocType.PDF) { try { using (var client = GetClient) { using (MemoryStream mStream = new MemoryStream(pdfBytesToUpload)) { PutObjectRequest request = new PutObjectRequest(); request.WithBucketName(DocsBucketName) .WithKey(pdfDocumentId); if (documentType == DocType.PDF) request.WithContentType("application/pdf"); else if (documentType == DocType.HTML) request.WithContentType("text/html"); else request.WithContentType("application/pdf"); request.WithInputStream(mStream); S3Response response = client.PutObject(request); response.Dispose(); } } } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { throw new Exception("Please check the provided AWS Credentials."); } else { throw new Exception(string.Format("An error occurred with the message '{0}' when writing an object", amazonS3Exception.Message)); } } }
public ActionResult UploadFile(FormCollection formCollection) { var hpf = Request.Files["image"]; string accessKey = System.Configuration.ConfigurationManager.AppSettings["AWSAccessKey"]; string secretAccessKey = System.Configuration.ConfigurationManager.AppSettings["AWSSecretKey"]; string bucketName = System.Configuration.ConfigurationManager.AppSettings["AWSPublicFilesBucket"]; string publicFile = hpf.FileName; PutObjectRequest request = new PutObjectRequest(); request.WithBucketName(bucketName); request.WithKey(publicFile); request.WithInputStream(hpf.InputStream); request.AutoCloseStream = true; request.CannedACL = S3CannedACL.PublicRead; AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretAccessKey); using (S3Response r = client.PutObject(request)) { foreach (var key in r.Headers.AllKeys) { log.DebugFormat("header key {0} value{1}", key, r.Headers[key]); } ViewBag.Response= r; } var images = session.Query<Image>(); return View(images); }
private void DoUpload(string backupPath, PeriodicBackupSetup backupConfigs) { var AWSRegion = RegionEndpoint.GetBySystemName(backupConfigs.AwsRegionEndpoint) ?? RegionEndpoint.USEast1; var desc = string.Format("Raven.Database.Backup {0} {1}", Database.Name, DateTimeOffset.UtcNow.ToString("u")); if (!string.IsNullOrWhiteSpace(backupConfigs.GlacierVaultName)) { var manager = new ArchiveTransferManager(awsAccessKey, awsSecretKey, AWSRegion); var archiveId = manager.Upload(backupConfigs.GlacierVaultName, desc, backupPath).ArchiveId; logger.Info(string.Format("Successfully uploaded backup {0} to Glacier, archive ID: {1}", Path.GetFileName(backupPath), archiveId)); return; } if (!string.IsNullOrWhiteSpace(backupConfigs.S3BucketName)) { var client = new Amazon.S3.AmazonS3Client(awsAccessKey, awsSecretKey, AWSRegion); using (var fileStream = File.OpenRead(backupPath)) { var key = Path.GetFileName(backupPath); var request = new PutObjectRequest(); request.WithMetaData("Description", desc); request.WithInputStream(fileStream); request.WithBucketName(backupConfigs.S3BucketName); request.WithKey(key); using (S3Response _ = client.PutObject(request)) { logger.Info(string.Format("Successfully uploaded backup {0} to S3 bucket {1}, with key {2}", Path.GetFileName(backupPath), backupConfigs.S3BucketName, key)); return; } } } }
private static void UploadFile(Stream stream, string key, string bucket) { string AWSAccessKey = ConfigurationManager.AppSettings["AWSAccessKey"]; string AWSSecretKey = ConfigurationManager.AppSettings["AWSSecretKey"]; using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKey, AWSSecretKey)) { PutObjectRequest request = new PutObjectRequest(); request.WithBucketName(bucket); request.WithKey(key); request.WithInputStream(stream); request.AutoCloseStream = true; request.CannedACL = S3CannedACL.PublicRead; client.PutObject(request); } }
public ActionResult Upload() { //Convert pdf to swf /* Process pc = new Process(); String currentPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString(); //ProcessStartInfo psi = new ProcessStartInfo(currentPath + "/Flash/pdf2swf.exe", currentPath+"/Flash/testPDF.pdf" +" -o " +currentPath+"/Flash/testPDF.swf"); //pc.StartInfo = psi; pc.StartInfo.FileName = currentPath + "\\Flash\\pdf2swf.exe"; pc.StartInfo.Arguments = currentPath + "\\Flash\\testPDF2.pdf" + " -o " + currentPath + "\\Flash\\testPDF2.swf"; pc.Start(); pc.WaitForExit(); pc.Close(); * */ //Upload pdf file to the server and database foreach (string upload in Request.Files) { if (!helperClass.HasFile(Request.Files[upload])) continue; string path = "https://s3-us-west-1.amazonaws.com/airpdfstorage/"; string filename = Path.GetFileName(Request.Files[upload].FileName); DateTime nowTime = DateTime.Now; string user = User.Identity.Name.ToString(); //store to app_data folder for thumbnail string path2 = AppDomain.CurrentDomain.BaseDirectory + "App_Data/"; Request.Files[upload].SaveAs(Path.Combine(path2, filename)); //upload to amazon s3 string accessKey = "AKIAIO4BEQ2UGFAMHRFQ"; string secretAccessKey = "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"; string bucketName = "airpdfstorage"; string keyName = filename; AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretAccessKey); PutObjectRequest request = new PutObjectRequest(); request.WithInputStream(Request.Files[upload].InputStream); request.CannedACL = S3CannedACL.PublicRead; request.WithBucketName(bucketName); request.WithKey(keyName); request.StorageClass = S3StorageClass.ReducedRedundancy; //set storage to reduced redundancy client.PutObject(request); client.Dispose(); using (var conn = new SqlConnection(connect)) { var qry = "INSERT INTO PDFs (Title, Author, uploadTime,fileURL) VALUES (@Title, @Author, @uploadTime, @fileURL)"; var cmd = new SqlCommand(qry, conn); cmd.Parameters.AddWithValue("@Title", filename); cmd.Parameters.AddWithValue("@Author", user); cmd.Parameters.AddWithValue("@uploadTime", nowTime); cmd.Parameters.AddWithValue("@fileURL", Path.Combine(path, filename)); conn.Open(); cmd.ExecuteNonQuery(); } } return View(); }
string UploadToAmazon(string fileName, Stream fileStream) { try { string uniqueKeyItemName = string.Format("{0}-{1}", keyName, fileName); PutObjectRequest request = new PutObjectRequest(); request.WithInputStream(fileStream); request.WithBucketName(bucketName) .WithKey(uniqueKeyItemName); request.WithMetaData("title", fileName); // Add a header to the request. //request.AddHeaders(AmazonS3Util.CreateHeaderEntry ("ContentType", contentType)); S3CannedACL anonPolicy = S3CannedACL.PublicRead; request.WithCannedACL(anonPolicy); S3Response response = client.PutObject(request); //GetPreSignedUrlRequest publicUrlRequest = new GetPreSignedUrlRequest().WithBucketName(bucketName).WithKey( uniqueKeyItemName ).WithExpires(DateTime.Now.AddMonths(3) ); //var urlResponse = client.GetPreSignedURL(publicUrlRequest); response.Dispose(); var urlResponse = string.Format("https://s3.amazonaws.com/{0}/{1}", bucketName, uniqueKeyItemName ); return urlResponse; } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { throw new Exception("Error - Invalid Credentials - please check the provided AWS Credentials", amazonS3Exception); } else { throw new Exception(String.Format("Error occured when uploading media: {0}",amazonS3Exception.Message),amazonS3Exception); } } }
private void UploadToAmazonService(HttpPostedFileBase file, string filename) { string bucketName = System.Configuration.ConfigurationManager.AppSettings["AWSPublicFilesBucket"]; //commute bucket string publicFile = "Pictures/" + filename; //We have Pictures folder in the bucket Session["publicFile"] = publicFile; PutObjectRequest request = new PutObjectRequest(); request.WithBucketName(bucketName); request.WithKey(publicFile); request.WithInputStream(file.InputStream); request.AutoCloseStream = true; request.CannedACL = S3CannedACL.PublicRead; //Read access for everyone AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(); //uses AWSAccessKey and AWSSecretKey defined in Web.config using (S3Response r = client.PutObject(request)) { } }