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, string contentBody, string contentType, Stream fileStream) { PutObjectRequest request = new PutObjectRequest(); request.WithContentBody(contentBody) .WithContentType(contentType) .WithBucketName(bucketName); request.WithInputStream(fileStream); request.CannedACL = S3CannedACL.PublicRead; S3Response response = _client.PutObject(request); response.Dispose(); }
private void UploadEvents(LoggingEvent[] events, AmazonS3 client) { var key = Filename(Guid.NewGuid().ToString()); var content = new StringBuilder(events.Count()); Array.ForEach(events, logEvent => { using (var writer = new StringWriter()) { Layout.Format(writer, logEvent); content.AppendLine(writer.ToString()); } }); var request = new PutObjectRequest(); request.WithBucketName(_bucketName); request.WithKey(key); request.WithContentBody(content.ToString()); client.PutObject(request); }
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); } } }
/// <summary> /// Writes byte array data into an S3 Bucket /// </summary> /// <param name="data">The byte array data to write to the bucket.</param> /// <param name="location">The location as to where you want to save the data</param> /// <param name="guid">The guid of the content you're uploading</param> public void WriteObject(string data, StorageLocations location, string guid) { var keyName = string.Format("{0}/{1}.onx", StorageLocationToString(location), guid); var request = new PutObjectRequest(); request.WithContentBody(data) .WithBucketName(Bucket) .WithKey(keyName); S3Response response = _client.PutObject(request); response.Dispose(); }
public bool UploadStringToS3Object(string s3ObjectName, string input) { try { PutObjectRequest request = new PutObjectRequest(); request.WithBucketName(bucketName); request.WithKey(s3ObjectName); request.WithContentBody(input); amazonS3Client.PutObject(request); return true; } catch (Exception e) { structuredLog("E", "Exception in UploadFileToS3Object: " + e); return false; } }
/// <summary> /// Runs the specified result. /// </summary> /// <param name="result">The result.</param> public void Run(IIntegrationResult result) { /* Create a client to use when interacting with Amazon S3 */ AmazonS3 client = AWSClientFactory.CreateAmazonS3Client(AccessKey, SecretKey); /* Get an instance of the artifact directory, so we can grab the files from it */ var artifactDirectory = new DirectoryInfo(ArtifactDirectory); /* Initiate the upload for each xml file in the artifact directory */ foreach (FileInfo artifactFile in artifactDirectory.GetFiles("*.xml")) { Log.Info("Beginning upload of artifact directory to S3..."); /* Read the contents of the file */ TextReader tr = new StreamReader(artifactFile.FullName); /* Read the full contents of the file */ string fileContents = tr.ReadToEnd(); /* Close the file, we have the content we need */ tr.Close(); try { /* Create a new PutObject request that will specify the data to upload and where to put it */ var request = new PutObjectRequest(); request.WithContentBody(fileContents) .WithBucketName(BucketName) .WithKey(artifactFile.Name); /* Put the object into the specified S3 bucket */ using (S3Response response = client.PutObject(request)) { Log.Info(string.Format("Successfully uploaded {0} to S3.", artifactFile.Name)); } } catch (AmazonS3Exception amazonS3Exception) { Log.Error(string.Format("Failed to upload {0} to S3.", artifactFile.Name)); Log.Error(amazonS3Exception.ErrorCode); Log.Error(amazonS3Exception.Message); } Log.Info("Upload of artifact directory is complete."); } }
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!"); }