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); }
static void WritingAnObject() { try { // simple object put PutObjectRequest request = new PutObjectRequest(); request.WithContentBody("this is a test") .WithBucketName(bucketName) .WithKey(keyName); S3Response response = client.PutObject(request); response.Dispose(); // put a more complex object with some metadata and http headers. PutObjectRequest titledRequest = new PutObjectRequest(); titledRequest.WithMetaData("title", "the title") .WithContentBody("this object has a title") .WithBucketName(bucketName) .WithKey(keyName); using (S3Response responseWithMetadata = client.PutObject(titledRequest)) { WebHeaderCollection headers = response.Headers; foreach (string key in headers.Keys) { Console.WriteLine("Response Header: {0}, Value: {1}", key, headers.Get(key)); } } } 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 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); }
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 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)); } } }
public void UploadObject(UploadRequest request) { CheckUri(request.Uri); try { var putRequest = new PutObjectRequest(); using (var client = CreateAmazonS3Client()) { var absolutePath = HttpUtility.UrlDecode(request.Uri.AbsolutePath); var key = absolutePath.TrimStart(Convert.ToChar("/")); putRequest.WithBucketName(bucketName) .WithKey(key) .WithInputStream(request.InputStream); if (accessControlEnabledGlobally && !request.IgnoreAccessControl) { putRequest.WithCannedACL(S3CannedACL.Private); } else { putRequest.WithCannedACL(S3CannedACL.PublicRead); } if (request.Headers != null && request.Headers.Count > 0) { putRequest.AddHeaders(request.Headers); } if (request.MetaData != null && request.MetaData.Count > 0) { putRequest.WithMetaData(request.MetaData); } putRequest.ContentType = MimeTypeUtility.DetermineContentType(request.Uri); using (client.PutObject(putRequest)) { } } } catch (Exception e) { throw new StorageException(string.Format("Failed to upload object with request {0}.", request), e); } }
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; } } } }
public static void UploadFile(string file, string awsPath) { PutObjectRequest request = new PutObjectRequest() { FilePath = file, BucketName = ConfigurationManager.AppSettings["AWSBucketName"], Key = awsPath }; if (!bool.Parse(ConfigurationManager.AppSettings["ManagedEncryption"])) { request.WithServerSideEncryptionMethod(ServerSideEncryptionMethod.AES256); } else { request.WithMetaData("customencrypt", "true"); } var responce = S3.PutObject(request); }
public void WriteToS3(string command) { System.Diagnostics.Trace.WriteLine(string.Format("Writing to S3 Log File Started, from a {0} command.", command)); PutObjectRequest request = new PutObjectRequest(); request.Key = LogFileName; var memoryStream = new MemoryStream(); using (memoryStream) { using (var writer = new StreamWriter(memoryStream, UTF8Encoding.UTF8, 8192, true)) { // Various for loops etc as necessary that will ultimately do this: foreach (var line in _stringFile) { writer.WriteLine(line); writer.Flush(); //System.Diagnostics.Trace.WriteLine(line); } } long size = memoryStream.Length; memoryStream.Position = 0; request.InputStream = memoryStream; request.WithMetaData("ContentLength", size.ToString()); request.BucketName = _bucketName; _s3Client.PutObject(request); } System.Diagnostics.Trace.WriteLine(string.Format("Writing to S3 Log File Completed, from a {0} command.",command)); }
public static void objectSerial(String filePath) { System.Console.WriteLine("\nhello,object!!"); NameValueCollection appConfig = ConfigurationManager.AppSettings; AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client(appConfig["AWSAccessKey"], appConfig["AWSSecretKey"]); String bucketName = "chttest3"; String objectName = "hello"; //String filePath = "D:\\Visual Studio Project\\TestNetSDK\\TestNetSDK\\pic.jpg"; //PutBucket System.Console.WriteLine("PutBucket: {0}\n",bucketName); PutBucketResponse response = s3Client.PutBucket(new PutBucketRequest().WithBucketName(bucketName)); //PutObject System.Console.WriteLine("PutObject!\n"); PutObjectRequest request = new PutObjectRequest(); request.WithBucketName(bucketName); request.WithKey(objectName); request.WithFilePath(filePath); PutObjectResponse PutResult = s3Client.PutObject(request); System.Console.WriteLine("Uploaded Object Etag: {0}\n", PutResult.ETag); //HeadObject System.Console.WriteLine("HeadObject!\n"); GetObjectMetadataResponse HeadResult = s3Client.GetObjectMetadata(new GetObjectMetadataRequest().WithBucketName(bucketName).WithKey(objectName)); System.Console.WriteLine("HeadObject: (1)ContentLength: {0} (2)ETag: {1}\n", HeadResult.ContentLength,HeadResult.ETag); //GetObject System.Console.WriteLine("GetObject!\n"); GetObjectResponse GetResult = s3Client.GetObject(new GetObjectRequest().WithBucketName(bucketName).WithKey(objectName).WithByteRange(1,15)); Stream responseStream = GetResult.ResponseStream; StreamReader reader = new StreamReader(responseStream); System.Console.WriteLine("Get Object Content:\n {0}\n", reader.ReadToEnd()); System.Console.WriteLine("Get Object ETag:\n {0}\n", GetResult.ETag); //CopyObject CopyObjectResponse CopyResult = s3Client.CopyObject(new CopyObjectRequest().WithSourceBucket(bucketName).WithSourceKey(objectName).WithDestinationBucket(bucketName).WithDestinationKey("hihi")); System.Console.WriteLine("CopyObject: ETag: {0} \n",CopyResult.ETag); //DeleteObject System.Console.WriteLine("Delete Object!\n"); s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey(objectName)); s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey("hihi")); //copied object //==============================jerry add ============================== System.Console.WriteLine("Jerry Add!\n"); String objectName2 = "hello2"; NameValueCollection nvc = new NameValueCollection(); nvc.Add("A", "ABC"); System.Console.WriteLine("PutObject!\n"); PutObjectRequest request2 = new PutObjectRequest(); request2.WithBucketName(bucketName); request2.WithKey(objectName2); request2.WithAutoCloseStream(true); request2.WithCannedACL(S3CannedACL.BucketOwnerFullControl); request2.WithContentBody("test"); request2.WithContentType("test/xml"); request2.WithGenerateChecksum(true); request2.WithMD5Digest("CY9rzUYh03PK3k6DJie09g=="); request2.WithMetaData(nvc); request2.WithWebsiteRedirectLocation("http://hicloud.hinet.net/"); PutObjectResponse PutResult2 = s3Client.PutObject(request2); System.Console.WriteLine("Uploaded Object Etag: {0}\n", PutResult2.ETag); System.Console.WriteLine("GetObject!\n"); GetObjectRequest request3 = new GetObjectRequest(); request3.WithBucketName(bucketName); request3.WithKey(objectName2); request3.WithByteRange(1, 2); DateTime datetime = DateTime.UtcNow; // 似乎不支援 datetime request3.WithModifiedSinceDate(datetime.AddHours(-1)); request3.WithUnmodifiedSinceDate(datetime.AddHours(1)); request3.WithETagToMatch(PutResult2.ETag); request3.WithETagToNotMatch("notMatch"); GetObjectResponse GetResult2 = s3Client.GetObject(request3); Stream responseStream2 = GetResult2.ResponseStream; StreamReader reader2 = new StreamReader(responseStream2); System.Console.WriteLine("Get Object Content(es):\n {0}\n", reader2.ReadToEnd()); System.Console.WriteLine("Get Object ETag:\n {0}\n", GetResult2.ETag); System.Console.WriteLine("HeadObject!\n"); GetObjectMetadataRequest request4 = new GetObjectMetadataRequest(); request4.WithBucketName(bucketName); request4.WithKey(objectName2); DateTime datetime2 = DateTime.UtcNow; // 似乎不支援 datetime request4.WithModifiedSinceDate(datetime2.AddHours(-1)); request4.WithUnmodifiedSinceDate(datetime2.AddHours(1)); request4.WithETagToMatch(PutResult2.ETag); request4.WithETagToNotMatch("notMatch"); GetObjectMetadataResponse HeadResult2 = s3Client.GetObjectMetadata(request4); System.Console.WriteLine("HeadObject: (1)ContentLength: {0} (2)ETag: {1}\n", HeadResult2.ContentLength, HeadResult2.ETag); CopyObjectRequest request5 = new CopyObjectRequest(); request5.WithSourceBucket(bucketName); request5.WithSourceKey(objectName2); request5.WithDestinationBucket(bucketName); request5.WithDestinationKey("hihi2"); DateTime datetime3 = DateTime.UtcNow; // 似乎不支援 datetime request5.WithModifiedSinceDate(datetime3.AddHours(-1)); request5.WithUnmodifiedSinceDate(datetime3.AddHours(1)); request5.WithETagToMatch(PutResult2.ETag); request5.WithETagToNotMatch("notMatch"); request5.WithMetaData(nvc); request5.WithCannedACL(S3CannedACL.PublicRead); request5.WithContentType("test/xml"); request5.WithWebsiteRedirectLocation("http://hicloud.hinet.net/"); CopyObjectResponse CopyResult2 = s3Client.CopyObject(request5); System.Console.WriteLine("CopyObject: ETag: {0} \n", CopyResult2.ETag); System.Console.WriteLine("Delete Object!\n"); s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey(objectName2)); s3Client.DeleteObject(new DeleteObjectRequest().WithBucketName(bucketName).WithKey("hihi2")); //copied object //==============================jerry add end============================== //DeleteBucket System.Console.WriteLine("Delete Bucket!\n"); //s3Client.DeletesBucket(new DeleteBucketRequest().WithBucketName(bucketName)); s3Client.DeleteBucket(new DeleteBucketRequest().WithBucketName(bucketName)); System.Console.WriteLine("END!"); }
public void UploadObject(UploadRequest request) { CheckUri(request.Uri); try { var putRequest = new PutObjectRequest(); using (var client = CreateAmazonS3Client()) { var absolutePath = request.Uri.AbsolutePath; var key = absolutePath.TrimStart(Convert.ToChar("/")); putRequest.WithBucketName(bucketName) .WithKey(key) .WithCannedACL(S3CannedACL.PublicRead) .WithInputStream(request.InputStream); if (request.Headers != null && request.Headers.Count > 0) { putRequest.AddHeaders(request.Headers); } if (request.MetaData != null && request.MetaData.Count > 0) { putRequest.WithMetaData(request.MetaData); } using (client.PutObject(putRequest)) { } } } catch (Exception e) { throw new StorageException(string.Format("Failed to upload object with request {0}.", request), e); } }
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); } }
public Task<bool> Upload(string id, Stream stream) { try{ NameValueCollection metadata; if(Exists(id)) metadata = new NameValueCollection { {LastWriteTimeKey, DateTime.UtcNow.ToString("u")} }; else metadata = new NameValueCollection { {CreationTimeKey, DateTime.UtcNow.ToString("u")}, {LastWriteTimeKey, DateTime.UtcNow.ToString("u")} }; var req = new PutObjectRequest { BucketName = BucketName, Key = id, InputStream = stream, Timeout = -1, ReadWriteTimeout = 300000 }; using (var client = SetupClient()) { var response = Task.Factory.FromAsync<PutObjectRequest, PutObjectResponse>( client.BeginPutObject, client.EndPutObject, req.WithMetaData(metadata), null); return response.ContinueWith(r => true); } } catch(AmazonS3Exception e) { throw new Exception(e.Message); } }