public void TestAppendObject() { string key = "key-1"; string content = "What goes up"; PutObjectRequestECS por = new PutObjectRequestECS() { BucketName = temp_bucket, Key = key, ContentBody = content }; // create the object client.PutObject(por); // append to the object long result = client.AppendObject(temp_bucket, key, " must come down."); // verify the append worked GetObjectResponse response = client.GetObject(temp_bucket, key); Stream responseStream = response.ResponseStream; StreamReader reader = new StreamReader(responseStream); string readContent = reader.ReadToEnd(); Assert.AreEqual("What goes up must come down.", readContent); Assert.AreEqual(content.Length, result); }
public void TestObjectRetentionPeriod() { string key = "retention-1"; string content = "sample retention content ..."; PutObjectRequestECS por = new PutObjectRequestECS() { BucketName = temp_bucket, Key = key, RetentionPeriod = 5, ContentBody = content }; client.PutObject(por); try { client.AppendObject(temp_bucket, key, "nogonna work her no more ..."); Assert.Fail("Allowed to update an object under retention"); } catch (AmazonS3Exception e) { Assert.AreEqual("ObjectUnderRetention", e.ErrorCode); } System.Threading.Thread.Sleep(5000); client.AppendObject(temp_bucket, key, "update now works ..."); }
public static void Main(string[] args) { // create the ECS S3 client ECSS3Client s3 = ECSS3Factory.getS3Client(); // retrieve the object key and object content from the user Console.Write("Enter the object key: "); string key = Console.ReadLine(); Console.Write("Enter the object content: "); string content = Console.ReadLine(); // create object request with retrieved input PutObjectRequestECS request = new PutObjectRequestECS() { BucketName = ECSS3Factory.S3_BUCKET, ContentBody = content, Key = key }; // create the object in demo bucket s3.PutObject(request); // print out object key/value for validation Console.WriteLine(string.Format("Created object {0}/{1} with content: {2}", ECSS3Factory.S3_BUCKET, key, content)); Console.ReadLine(); }
/// <summary> /// Creates or updates an object using provided parameters. /// </summary> /// <param name="request">Container for the necessary parameters to execute the PutObject service method.</param> /// <returns>The response from the PutObject service method, as returned by ECS.</returns> public PutObjectResponseECS PutObject(PutObjectRequestECS request) { var marshaller = new PutObjectRequestMarshallerECS(); var unmarshaller = PutObjectResponseUnmarshallerECS.Instance; return(Invoke <PutObjectRequestECS, PutObjectResponseECS>(request, marshaller, unmarshaller)); }
public void TestUpdateObjectWithRange() { string key = "key-1"; string content = "The cat crossed the road."; int offset = content.IndexOf("cat"); PutObjectRequestECS por = new PutObjectRequestECS() { BucketName = temp_bucket, Key = key, ContentBody = content }; // create the object client.PutObject(por); string updatePart = "dog"; por = new PutObjectRequestECS() { BucketName = temp_bucket, Key = key, Range = Range.fromOffsetLength(offset, updatePart.Length), ContentBody = updatePart }; // update the object client.PutObject(por); // verify update GetObjectResponse response = client.GetObject(temp_bucket, key); Stream responseStream = response.ResponseStream; StreamReader reader = new StreamReader(responseStream); string readContent = reader.ReadToEnd(); Assert.AreEqual("The dog crossed the road.", readContent); updatePart = "very lucky animal crossed the road."; por = new PutObjectRequestECS() { BucketName = temp_bucket, Key = key, Range = Range.fromOffset(offset), ContentBody = updatePart }; client.PutObject(por); // verify update response = client.GetObject(temp_bucket, key); responseStream = response.ResponseStream; reader = new StreamReader(responseStream); readContent = reader.ReadToEnd(); Assert.AreEqual(content.Substring(0, offset) + updatePart, readContent); }
/// <summary> /// Creates or updates an object using provided parameters. /// </summary> /// <param name="request">Container for the necessary parameters to execute the PutObject service method.</param> /// <returns>The response from the PutObject service method, as returned by ECS.</returns> public PutObjectResponseECS PutObject(PutObjectRequestECS request) { var marshaller = new PutObjectRequestMarshallerECS(); var unmarshaller = PutObjectResponseUnmarshallerECS.Instance; InvokeOptions invokeOptions = new InvokeOptions(); invokeOptions.RequestMarshaller = marshaller; invokeOptions.ResponseUnmarshaller = unmarshaller; return(Invoke <PutObjectResponseECS>(request, invokeOptions)); }
public void TestObjectRetentionPolicy() { string key = "retention-1"; string content = "sample retention content ..."; PutObjectRequestECS por = new PutObjectRequestECS() { BucketName = temp_bucket, Key = key, RetentionPolicy = "hold-me", ContentBody = content }; client.PutObject(por); }
/// <summary> /// Appends the provided data to the end of the object. /// </summary> /// <param name="bucketName">Container for the necessary parameters to execute the PutObject service method.</param> /// <returns>The response from the PutObject service method, as returned by ECS.</returns> public long AppendObject(string bucketName, string keyName, string contentBody) { var marshaller = new PutObjectRequestMarshallerECS(); var unmarshaller = PutObjectResponseUnmarshallerECS.Instance; PutObjectRequestECS request = new PutObjectRequestECS() { BucketName = bucketName, Key = keyName, ContentBody = contentBody, Range = Range.fromOffset(-1) }; PutObjectResponseECS response = Invoke <PutObjectRequestECS, PutObjectResponseECS>(request, marshaller, unmarshaller); return(response.AppendOffset); }
public static void Main(string[] args) { // create the ECS S3 client ECSS3Client s3 = ECSS3Factory.getS3Client(); // object key to create, update, and append string key = "atomic-append.txt"; string bucketName = ECSS3Factory.S3_BUCKET; // bucket to create object in string content = "Hello World!"; // first create an initial object Console.WriteLine(string.Format("creating initial object {0}/{1} with content: {2}", ECSS3Factory.S3_BUCKET, key, content)); PutObjectRequestECS request = new PutObjectRequestECS() { BucketName = bucketName, Key = key, ContentBody = content }; s3.PutObject(request); // read object and print content Console.WriteLine(string.Format("initial object {0}/{1} with content: [{2}]", bucketName, key, readObject(s3, bucketName, key))); // append to the end of the object string content2 = " ... and Universe!!"; // the offset at which our appended data was written is returned // (this is the previous size of the object) long appendOffset = s3.AppendObject(bucketName, key, content2); Console.WriteLine(string.Format("append successful at offset {0}", appendOffset)); // read object and print content Console.WriteLine(string.Format("final object {0}/{1} with content: [{2}]", bucketName, key, readObject(s3, bucketName, key))); Console.ReadLine(); }
public static void Main(string[] args) { // create the ECS S3 Client ECSS3Client s3 = ECSS3Factory.getS3Client(); // retrieve the object key and object content from the user Console.Write("Enter the object key: "); string key = Console.ReadLine(); Console.Write("Enter the object content: "); string content = Console.ReadLine(); // retrieve the object metadata key and value from user Console.Write("Enter the metadata key: "); string metaKey = Console.ReadLine(); Console.Write("Enter the metadata value: "); string metaValue = Console.ReadLine(); // create object request with retrieved input PutObjectRequestECS request = new PutObjectRequestECS() { BucketName = ECSS3Factory.S3_BUCKET, ContentBody = content, Key = key }; // add metadata to request request.Metadata.Add(metaKey, metaValue); // create the object with metadata in the demo bucket s3.PutObject(request); // print out object key/value and metadata key/value for validation Console.WriteLine(string.Format("Create object {0}/{1} with metadata {2}={3} and content: {4}", ECSS3Factory.S3_BUCKET, key, metaKey, metaValue, content)); Console.ReadLine(); }
public void TestPutObject() { string key = "key-1"; string content = "TestPutObject"; PutObjectRequestECS por = new PutObjectRequestECS() { BucketName = temp_bucket, Key = key, ContentBody = content }; // create the object client.PutObject(por); GetObjectResponse respone = client.GetObject(temp_bucket, key); Stream responeStream = respone.ResponseStream; StreamReader reader = new StreamReader(responeStream); string readContent = reader.ReadToEnd(); Assert.AreEqual(content.Length, readContent.Length); Assert.AreEqual("TestPutObject", readContent); }
public static void Main(string[] args) { ECSS3Client s3 = ECSS3Factory.getS3Client(); PutObjectRequestECS request; string key = "update-append.txt"; // object key to create, update, and append string bucketName = ECSS3Factory.S3_BUCKET; // bucket to create object in string content = "The tan fox jumped over the dog"; // initial object content int tanIndex = content.IndexOf("tan"); Console.WriteLine(string.Format("creating initial object {0}/{1} with content: {2}", ECSS3Factory.S3_BUCKET, key, content)); // first create an initial object request = new PutObjectRequestECS(); request.BucketName = bucketName; request.Key = key; request.ContentBody = content; s3.PutObject(request); // read object and print content Console.WriteLine(string.Format("initial object {0}/{1} with content: [{2}]", bucketName, key, readObject(s3, bucketName, key))); // update the object in the middle string content2 = "red"; request = new PutObjectRequestECS(); request.BucketName = bucketName; request.Key = key; request.ContentBody = content2; request.Range = Range.fromOffsetLength(tanIndex, content2.Length); Console.WriteLine(string.Format("updating object at offset {0}", tanIndex)); s3.PutObject(request); // read object and print content Console.WriteLine(string.Format("updated object {0}/{1} with content: [{2}]", bucketName, key, readObject(s3, bucketName, key))); // append to the object string content3 = " and cat"; Console.WriteLine(string.Format("appending object at offset {0}", content.Length)); request = new PutObjectRequestECS(); request.BucketName = bucketName; request.Key = key; request.ContentBody = content3; request.Range = Range.fromOffset(content.Length); s3.PutObject(request); // read object and print content Console.WriteLine(string.Format("appended object {0}/{1} with content: [{2}]", bucketName, key, readObject(s3, bucketName, key))); // create a sparse object by appending past the end of the object string content4 = "#last byte#"; Console.WriteLine(string.Format("sparse append object at offset {0}", 45)); request = new PutObjectRequestECS(); request.BucketName = bucketName; request.Key = key; request.ContentBody = content4; request.Range = Range.fromOffset(45); s3.PutObject(request); // read object and print content Console.WriteLine(string.Format("sparse append object {0}/{1} with content: [{2}]", bucketName, key, readObject(s3, bucketName, key))); Console.ReadLine(); }
public void TestConditionalObject() { string key = "key-1"; string content = "testing a conditional PUT"; DateTime in_the_past = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now.AddMinutes(-5)); DateTime in_the_future = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now.AddMinutes(10)); PutObjectRequestECS por = new PutObjectRequestECS() { BucketName = temp_bucket, Key = key, ContentBody = content, ContentType = "text/plain" }; PutObjectResponse response = client.PutObject(por); string eTag = response.ETag; por.UnmodifiedSinceDate = in_the_past; try { client.PutObject(por); Assert.Fail("Expected 412 response code"); } catch (AmazonS3Exception e) { Assert.AreEqual(System.Net.HttpStatusCode.PreconditionFailed, e.StatusCode); } // clear out unmodified por = new PutObjectRequestECS() { BucketName = temp_bucket, Key = key, ContentBody = content, ContentType = "text/plain" }; // set same stamp as modified - test por.ModifiedSinceDate = in_the_past; client.PutObject(por); por.ModifiedSinceDate = in_the_future; try { client.PutObject(por); Assert.Fail("Expected 412 response code"); } catch (AmazonS3Exception e) { Assert.AreEqual(System.Net.HttpStatusCode.PreconditionFailed, e.StatusCode); } // clear out modified por = new PutObjectRequestECS() { BucketName = temp_bucket, Key = key, ContentBody = content, ContentType = "text/plain" }; // if etag match - pass por.EtagToMatch = eTag; client.PutObject(por); por.EtagToMatch = null; // if etag doesn't match - fail por.EtagToNotMatch = eTag; try { client.PutObject(por); Assert.Fail("Expected 412 response code"); } catch (AmazonS3Exception e) { Assert.AreEqual(System.Net.HttpStatusCode.PreconditionFailed, e.StatusCode); } eTag = "0f7373bfe4bda6531b15229e9b9e8f75"; // if etag doesn't match - pass por.EtagToNotMatch = eTag; client.PutObject(por); por.EtagToNotMatch = null; // if etag to match - fail por.EtagToMatch = eTag; try { client.PutObject(por); Assert.Fail("Expected 412 response code"); } catch (AmazonS3Exception e) { Assert.AreEqual(System.Net.HttpStatusCode.PreconditionFailed, e.StatusCode); } por.EtagToMatch = null; // if match * (if the key exists, i.e. update only) pass por.EtagToMatch = "*"; client.PutObject(por); por.EtagToMatch = null; // test if-none-match * (if key is new, i.e. create only) fail por.EtagToNotMatch = "*"; try { client.PutObject(por); Assert.Fail("Expected 412 response code"); } catch (AmazonS3Exception e) { Assert.AreEqual(System.Net.HttpStatusCode.PreconditionFailed, e.StatusCode); } por.Key = "key-2"; // if-non-match * (if the key is new i.e. create only) pass client.PutObject(por); por.EtagToNotMatch = null; por.Key = "key-3"; // if-match * (if the key exists i.e. update only) fail por.EtagToMatch = "*"; try { //client.PutObject(por); STORAGE - 14736 //Assert.Fail("Expected 412 response code"); STORAGE - 14736 } catch (AmazonS3Exception e) { Assert.AreEqual(System.Net.HttpStatusCode.PreconditionFailed, e.StatusCode); } }
public static void Main(string[] args) { // create the ECS S3 client ECSS3Client s3 = ECSS3Factory.getS3Client(); // Create the bucket with indexed keys List <MetaSearchKey> bucketMetadataSearchKeys = new List <MetaSearchKey>() { new MetaSearchKey() { Name = USER_PREFIX + FIELD_ACCOUNT_ID, Type = MetaSearchDatatype.integer }, new MetaSearchKey() { Name = USER_PREFIX + FIELD_BILLING_DATE, Type = MetaSearchDatatype.datetime }, new MetaSearchKey() { Name = USER_PREFIX + FIELD_BILL_TYPE, Type = MetaSearchDatatype.@string } }; PutBucketRequestECS pbr = new PutBucketRequestECS(); pbr.BucketName = BUCKET_NAME; pbr.SetMetadataSearchKeys(bucketMetadataSearchKeys); s3.PutBucket(pbr); foreach (string key in KEY_LIST) { PutObjectRequestECS por = new PutObjectRequestECS(); por.BucketName = BUCKET_NAME; por.Key = key; por.Metadata.Add(FIELD_ACCOUNT_ID, extractAccountId(key)); por.Metadata.Add(FIELD_BILLING_DATE, extractBillDate(key)); por.Metadata.Add(FIELD_BILL_TYPE, extractBillType(key)); s3.PutObject(por); } while (true) { Console.Write("Enter the account id (empty for none): "); string accountId = Console.ReadLine(); Console.Write("Enter the billing date (e.g. 2016-09-22, empty for none): "); string billingDate = Console.ReadLine(); Console.Write("Enter the bill type (e.g. xml. empty for none): "); string billType = Console.ReadLine(); QueryObjectsRequest qor = new QueryObjectsRequest() { BucketName = BUCKET_NAME }; StringBuilder query = new StringBuilder(); if (accountId.Length > 0) { query.Append(USER_PREFIX + FIELD_ACCOUNT_ID + "==" + accountId + ""); } if (billingDate.Length > 0) { if (query.Length > 0) { query.Append(" and "); } query.Append(USER_PREFIX + FIELD_BILLING_DATE + "==" + billingDate + "T00:00:00Z"); } if (billType.Length > 0) { if (query.Length > 0) { query.Append(" and "); } query.Append(USER_PREFIX + FIELD_BILL_TYPE + "=='" + billType + "'"); } qor.Query = query.ToString(); QueryObjectsResponse res = s3.QueryObjects(qor); Console.WriteLine("--------------------------"); Console.WriteLine("Bucket: " + res.BucketName); Console.WriteLine("Query: " + qor.Query); Console.WriteLine(); Console.WriteLine("Key"); Console.WriteLine("--------------------------"); foreach (QueryObject obj in res.ObjectMatches) { Console.WriteLine(string.Format("{0}", obj.Name)); } Console.Write("Another? (Y/N) "); string another = Console.ReadLine(); if (another.ToUpper() == "N") { break; } } //cleanup foreach (string key in KEY_LIST) { s3.DeleteObject(BUCKET_NAME, key); } s3.DeleteBucket(BUCKET_NAME); }