static int Main(string[] args) { if (awsAccessKeyId.StartsWith("<INSERT")) { System.Console.WriteLine("Please examine S3Test.cs and update it with your credentials"); return -1; } testUrlEscaping(); // test all operation for both regular and vanity domains // regular: http://s3.amazonaws.com/key // subdomain: http://bucket.s3.amazonaws.com/key // testing pure vanity domains (http://<vanity domain>/key) is not covered here // but is possible with some assitional setup foreach ( CallingFormat format in callingFormats ) { AWSAuthConnection conn = new AWSAuthConnection( awsAccessKeyId, awsSecretAccessKey, false, format ); using ( Response response = conn.createBucket( bucketName, Location.DEFAULT, null ) ) { assertEquals( HTTP_OK, response.Status, "couldn't create bucket" ); } using ( ListBucketResponse listBucketResponse = conn.listBucket( bucketName, null, null, 0, null ) ) { assertEquals( HTTP_OK, listBucketResponse.Status, "Couldn't get list" ); assertEquals( 0, listBucketResponse.Entries.Count, "list wasn't empty" ); verifyBucketResponseParameters( listBucketResponse, bucketName, "", "", UnspecifiedMaxKeys, null, false, null ); } // start delimiter tests string text = "this is a test"; string key = "example.txt"; string innerKey = "test/inner.txt"; string badcharKey = "x+/last{key}.txt"; string lastKey = "z-last-key.txt"; using ( Response response = conn.put(bucketName, key, new S3Object(text, null), null) ) { assertEquals( HTTP_OK, response.Status, "couldn't put simple object"); } using ( Response response = conn.put(bucketName, innerKey, new S3Object(text, null), null) ) { assertEquals( HTTP_OK, response.Status, "couldn't put simple object"); } using ( Response response = conn.put(bucketName, lastKey, new S3Object(text, null), null) ) { assertEquals( HTTP_OK, response.Status, "couldn't put simple object"); } using ( Response response = conn.put(bucketName, badcharKey, new S3Object(text, null), null) ) { assertEquals( HTTP_OK, response.Status, "couldn't put simple object"); } // plain list using ( ListBucketResponse listBucketResponse = conn.listBucket(bucketName, null, null, 0, null) ) { assertEquals( listBucketResponse.Status, HTTP_OK, "couldn't get list"); assertEquals(4, listBucketResponse.Entries.Count, "Unexpected list size"); assertEquals(0, listBucketResponse.CommonPrefixEntries.Count, "Unexpected common prefix size"); verifyBucketResponseParameters(listBucketResponse, bucketName, "", "", UnspecifiedMaxKeys, null, false, null); } // root "directory" using ( ListBucketResponse listBucketResponse = conn.listBucket(bucketName, null, null, 0, "/", null) ) { assertEquals( HTTP_OK, listBucketResponse.Status, "couldn't get list"); assertEquals(2, listBucketResponse.Entries.Count, "Unexpected list size"); assertEquals(2, listBucketResponse.CommonPrefixEntries.Count, "Unexpected common prefix size"); verifyBucketResponseParameters(listBucketResponse, bucketName, "", "", UnspecifiedMaxKeys, "/", false, null); } // root "directory" with a max-keys of "1" using ( ListBucketResponse listBucketResponse = conn.listBucket(bucketName, null, null, 1, "/", null) ) { assertEquals( HTTP_OK, listBucketResponse.Status, "couldn't get list"); assertEquals(1, listBucketResponse.Entries.Count, "Unexpected list size"); assertEquals(0, listBucketResponse.CommonPrefixEntries.Count, "Unexpected common prefix size"); verifyBucketResponseParameters(listBucketResponse, bucketName, "", "", 1, "/", true, "example.txt"); } // root "directory" with a max-keys of "2" string marker = null; using ( ListBucketResponse listBucketResponse = conn.listBucket(bucketName, null, null, 2, "/", null) ) { assertEquals( HTTP_OK, listBucketResponse.Status, "couldn't get list"); assertEquals(1, listBucketResponse.Entries.Count, "Unexpected list size"); assertEquals(1, listBucketResponse.CommonPrefixEntries.Count, "Unexpected common prefix size"); verifyBucketResponseParameters(listBucketResponse, bucketName, "", "", 2, "/", true, "test/"); marker = listBucketResponse.NextMarker; } using ( ListBucketResponse listBucketResponse = conn.listBucket( bucketName, null, marker, 2, "/", null ) ) { assertEquals( HTTP_OK, listBucketResponse.Status, "couldn't get list"); assertEquals(1, listBucketResponse.Entries.Count, "Unexpected list size"); assertEquals(1, listBucketResponse.CommonPrefixEntries.Count, "Unexpected common prefix size"); verifyBucketResponseParameters(listBucketResponse, bucketName, "", marker, 2, "/", false, null); } // test "directory" (really just prefix) using ( ListBucketResponse listBucketResponse = conn.listBucket(bucketName, "test/", null, 0, "/", null) ) { assertEquals( HTTP_OK, listBucketResponse.Status, "couldn't get list"); assertEquals(1, listBucketResponse.Entries.Count, "Unexpected list size"); assertEquals(0, listBucketResponse.CommonPrefixEntries.Count, "Unexpected common prefix size"); verifyBucketResponseParameters(listBucketResponse, bucketName, "test/", "", UnspecifiedMaxKeys, "/", false, null); } // test "directory" 'x+' (really just prefix) using ( ListBucketResponse listBucketResponse = conn.listBucket(bucketName, "x+/", null, 0, "/", null) ) { assertEquals( HTTP_OK, listBucketResponse.Status, "couldn't get list"); assertEquals(1, listBucketResponse.Entries.Count, "Unexpected list size"); assertEquals(0, listBucketResponse.CommonPrefixEntries.Count, "Unexpected common prefix size"); verifyBucketResponseParameters(listBucketResponse, bucketName, "x+/", "", UnspecifiedMaxKeys, "/", false, null); } // remove innerkey using ( Response response = conn.delete(bucketName, innerKey, null) ) { assertEquals( HTTP_NO_CONTENT, response.Status, "couldn't delete entry"); } // remove badcharKey using ( Response response = conn.delete(bucketName, badcharKey, null) ) { assertEquals( HTTP_NO_CONTENT, response.Status, "couldn't delete entry"); } // remove last key using ( Response response = conn.delete(bucketName, lastKey, null) ) { assertEquals( HTTP_NO_CONTENT, response.Status, "couldn't delete entry"); } // End of delimiter tests SortedList metadata = new SortedList(); metadata.Add("title", "title"); using ( Response response = conn.put(bucketName, key, new S3Object(text, metadata), null) ) { assertEquals( HTTP_OK, response.Status, "couldn't put complex object"); } using ( GetResponse getResponse = conn.get(bucketName, key, null) ) { assertEquals( HTTP_OK, getResponse.Status, "couldn't get object"); assertEquals(text, getResponse.Object.Data, "didn't get the right data back"); assertEquals(1, getResponse.Object.Metadata.Count, "didn't get the right metadata back"); assertEquals( "title", getResponse.Object.Metadata["title"], "didn't get the right metadata back"); assertEquals( (long)text.Length, getResponse.Connection.ContentLength, "didn't get the right content-length"); } // end delimiter tests using ( Response response = conn.put(bucketName, key, new S3Object(text, null), null) ) { assertEquals( HTTP_OK, response.Status, "couldn't put simple object"); } SortedList metadataWithSpaces = new SortedList(); string titleWithSpaces = " \t title with leading and trailing spaces "; metadataWithSpaces.Add("title", titleWithSpaces); using ( Response response = conn.put(bucketName, key, new S3Object(text, metadataWithSpaces), null) ) { assertEquals( HTTP_OK, response.Status, "couldn't put metadata with leading and trailing spaces"); } using ( GetResponse getResponse = conn.get(bucketName, key, null) ) { assertEquals( HTTP_OK, getResponse.Status, "couldn't get object"); assertEquals(1, getResponse.Object.Metadata.Count, "didn't get the right metadata back"); assertEquals( titleWithSpaces.Trim(), getResponse.Object.Metadata["title"], "didn't get the right metadata back"); } string weirdKey = "&=/%# ++/++"; using ( Response response = conn.put(bucketName, weirdKey, new S3Object(text, null), null) ) { assertEquals( HTTP_OK, response.Status, "couldn't put weird key"); } using ( GetResponse getResponse = conn.get(bucketName, weirdKey, null) ) { assertEquals( HTTP_OK, getResponse.Status, "couldn't get weird key"); } string acl = null; using ( GetResponse getResponse = conn.getACL(bucketName, key, null) ) { assertEquals( HTTP_OK, getResponse.Status, "couldn't get acl"); acl = getResponse.Object.Data; } using ( Response response = conn.putACL(bucketName, key, acl, null) ) { assertEquals( HTTP_OK, response.Status, "couldn't put acl"); } string bucketACL = null; using ( GetResponse getResponse = conn.getBucketACL(bucketName, null) ) { assertEquals( HTTP_OK, getResponse.Status, "couldn't get bucket acl"); bucketACL = getResponse.Object.Data; } using ( Response response = conn.putBucketACL(bucketName, bucketACL, null) ) { assertEquals( HTTP_OK, response.Status, "couldn't put bucket acl"); } // bucket logging tests string bucketLogging; using ( GetResponse getResponse = conn.getBucketLogging(bucketName, null) ) { assertEquals( HTTP_OK, getResponse.Status, "couldn't get bucket logging config"); bucketLogging = getResponse.Object.Data; } using ( Response response = conn.putBucketLogging(bucketName, bucketLogging, null) ) { assertEquals( HTTP_OK, response.Status, "couldn't put bucket logging config"); } // end bucket logging tests using ( ListBucketResponse listBucketResponse = conn.listBucket(bucketName, null, null, 0, null) ) { assertEquals( HTTP_OK, listBucketResponse.Status, "couldn't list bucket"); ArrayList entries = listBucketResponse.Entries; assertEquals(2, entries.Count, "didn't get back the right number of entries"); // depends on weirdKey < $key assertEquals(weirdKey, ((ListEntry)entries[0]).Key, "first key isn't right"); assertEquals(key, ((ListEntry)entries[1]).Key, "second key isn't right"); verifyBucketResponseParameters(listBucketResponse, bucketName, "", "", UnspecifiedMaxKeys, null, false, null); } using ( ListBucketResponse listBucketResponse = conn.listBucket(bucketName, null, null, 1, null) ) { assertEquals( HTTP_OK, listBucketResponse.Status, "couldn't list bucket"); assertEquals( 1, listBucketResponse.Entries.Count, "didn't get back the right number of entries"); verifyBucketResponseParameters(listBucketResponse, bucketName, "", "", 1, null, true, null); } using ( ListBucketResponse listBucketResponse = conn.listBucket( bucketName, null, null, 0, null ) ) { assertEquals( HTTP_OK, listBucketResponse.Status, "couldn't list bucket"); // delete the bucket's content foreach ( ListEntry entry in listBucketResponse.Entries ) { using ( Response response = conn.delete( bucketName, entry.Key, null ) ) { assertEquals( HTTP_NO_CONTENT, response.Status, "couldn't delete entry" ); } } } ArrayList buckets = null; using ( ListAllMyBucketsResponse listAllMyBucketsResponse = conn.listAllMyBuckets(null) ) { assertEquals( HTTP_OK, listAllMyBucketsResponse.Status, "couldn't list all my buckets"); buckets = listAllMyBucketsResponse.Buckets; } using ( Response response = conn.deleteBucket(bucketName, null) ) { assertEquals( HTTP_NO_CONTENT, response.Status, "couldn't delete bucket"); } using ( ListAllMyBucketsResponse listAllMyBucketsResponse = conn.listAllMyBuckets(null) ) { assertEquals( HTTP_OK, listAllMyBucketsResponse.Status, "couldn't list all my buckets"); assertEquals( buckets.Count - 1, listAllMyBucketsResponse.Buckets.Count, "bucket count is incorrect"); } QueryStringAuthGenerator generator = new QueryStringAuthGenerator(awsAccessKeyId, awsSecretAccessKey, false); checkURL( generator.createBucket(bucketName, null, null), "PUT", HTTP_OK, "couldn't create bucket"); checkURL( generator.put(bucketName, key, new S3Object("test data", null), null), "PUT", HTTP_OK, "put object", "test data"); checkURL( generator.get(bucketName, key, null), "GET", HTTP_OK, "get object"); checkURL( generator.listBucket(bucketName, null, null, 0, null), "GET", HTTP_OK, "list bucket"); checkURL( generator.listAllMyBuckets(null), "GET", HTTP_OK, "list all my buckets"); checkURL( generator.getACL(bucketName, key, null), "GET", HTTP_OK, "get acl"); checkURL( generator.putACL(bucketName, key, null), "PUT", HTTP_OK, "put acl", acl); checkURL( generator.getBucketACL(bucketName, null), "GET", HTTP_OK, "get bucket acl"); checkURL( generator.putBucketACL(bucketName, null), "PUT", HTTP_OK, "put bucket acl", bucketACL); checkURL( generator.delete(bucketName, key, null), "DELETE", HTTP_NO_CONTENT, "delete object"); checkURL( generator.deleteBucket(bucketName, null), "DELETE", HTTP_NO_CONTENT, "delete bucket"); } System.Console.WriteLine("OK (" + assertionCount + " tests passed)"); return 0; }
static int Main(string[] args) { try { if ( awsAccessKeyId.StartsWith( "<INSERT" ) ) { System.Console.WriteLine( "Please examine S3Driver.cs and update it with your credentials" ); return 1; } AWSAuthConnection conn = new AWSAuthConnection( awsAccessKeyId, awsSecretAccessKey ); QueryStringAuthGenerator generator = new QueryStringAuthGenerator( awsAccessKeyId, awsSecretAccessKey ); // Check if the bucket exists. The high availability engineering of // Amazon S3 is focused on get, put, list, and delete operations. // Because bucket operations work against a centralized, global // resource space, it is not appropriate to make bucket create or // delete calls on the high availability code path of your application. // It is better to create or delete buckets in a separate initialization // or setup routine that you run less often. if (conn.checkBucketExists(bucketName)) { System.Console.WriteLine("----- bucket already exists! -----"); } else { System.Console.WriteLine( "----- creating bucket -----" ); // to create an EU located bucket change the Location param like this: // using ( Response response = conn.createBucket( bucketName, Location.EU, null ) ) using (Response response = conn.createBucket(bucketName, Location.EU, null)) { System.Console.WriteLine(response.getResponseMessage() ); } } System.Console.WriteLine("----- bucket location -----"); using (LocationResponse locationResponse = conn.getBucketLocation(bucketName)) { if (locationResponse.Location == null) System.Console.WriteLine("Location: <error>"); else if (locationResponse.Location.Length == 0) System.Console.WriteLine("Location: <default>"); else System.Console.WriteLine("Location: '{0}'", locationResponse.Location); } System.Console.WriteLine("----- listing bucket -----"); using ( ListBucketResponse listBucketResponse = conn.listBucket( bucketName, null, null, 0, null ) ) { dumpBucketListing( listBucketResponse ); } System.Console.WriteLine( "----- putting object -----" ); S3Object obj = new S3Object( "This is a test", null ); SortedList headers = new SortedList(); headers.Add( "Content-Type", "text/plain" ); using ( Response response = conn.put( bucketName, keyName, obj, headers ) ) { System.Console.WriteLine( response.getResponseMessage() ); } System.Console.WriteLine( "----- listing bucket -----" ); using ( ListBucketResponse listBucketResponse = conn.listBucket( bucketName, null, null, 0, null ) ) { dumpBucketListing( listBucketResponse ); } System.Console.WriteLine( "----- query string auth example -----" ); generator.ExpiresIn = 60 * 1000; System.Console.WriteLine( "Try this url in your web browser (it will only work for 60 seconds)\n" ); string url = generator.get( bucketName, keyName, null ); System.Console.WriteLine( url ); System.Console.Write( "\npress enter >" ); System.Console.ReadLine(); System.Console.WriteLine( "\nNow try just the url without the query string arguments. It should fail.\n" ); System.Console.WriteLine( generator.makeBaseURL(bucketName, keyName) ); System.Console.Write( "\npress enter >" ); System.Console.ReadLine(); System.Console.WriteLine( "----- putting object with metadata and public read acl -----" ); SortedList metadata = new SortedList(); metadata.Add( "blah", "foo" ); obj = new S3Object( "this is a publicly readable test", metadata ); headers = new SortedList(); headers.Add( "x-amz-acl", "public-read" ); headers.Add( "Content-Type", "text/plain" ); using ( Response response = conn.put( bucketName, keyName + "-public", obj, headers ) ) { System.Console.WriteLine( response.getResponseMessage() ); } System.Console.WriteLine( "----- anonymous read test -----" ); System.Console.WriteLine( "\nYou should be able to try this in your browser\n" ); string publicURL = generator.get(bucketName, keyName + "-public", null); System.Console.WriteLine(publicURL); System.Console.Write( "\npress enter >" ); System.Console.ReadLine(); System.Console.WriteLine("----- path style url example -----"); System.Console.WriteLine("\nNon-location-constrained buckets can also be specified as part of the url path. (This was the original url style supported by S3.)"); System.Console.WriteLine("\nTry this url out in your browser (it will only be valid for 60 seconds)\n"); generator.CallingFormat = CallingFormat.PATH; // could also have been done like this: // generator = new QueryStringAuthGenerator(awsAccessKeyId, awsSecretAccessKey, true, Utils.DEFAULT_HOST, CallingFormat.getPathCallingFormat()); generator.ExpiresIn = 60 * 1000; System.Console.WriteLine(generator.get(bucketName, keyName, null)); System.Console.Write("\npress enter> "); System.Console.ReadLine(); System.Console.WriteLine( "----- getting object's acl -----" ); using ( GetResponse response = conn.getACL( bucketName, keyName, null ) ) { System.Console.WriteLine( response.Object.Data ); } System.Console.WriteLine( "----- deleting objects -----" ); using ( Response response = conn.delete( bucketName, keyName, null ) ) { System.Console.WriteLine( response.getResponseMessage() ); } using ( Response response = conn.delete( bucketName, keyName + "-public", null ) ) { System.Console.WriteLine( response.getResponseMessage() ); } System.Console.WriteLine( "----- listing bucket -----" ); using ( ListBucketResponse listBucketResponse = conn.listBucket( bucketName, null, null, 0, null ) ) { dumpBucketListing( listBucketResponse ); } System.Console.WriteLine( "----- listing all my buckets -----" ); using ( ListAllMyBucketsResponse listBucketResponse = conn.listAllMyBuckets( null ) ) { dumpAllMyBucketListing( listBucketResponse ); } System.Console.WriteLine( "----- deleting bucket -----" ); using ( Response response = conn.deleteBucket( bucketName, null ) ) { System.Console.WriteLine( response.getResponseMessage() ); } return 0; } catch ( WebException e ) { System.Console.WriteLine( e.Status ); System.Console.WriteLine( e.Message ); System.Console.WriteLine( e.StackTrace ); System.Console.ReadLine(); return 1; } catch ( Exception e ) { System.Console.WriteLine( e.Message ); System.Console.WriteLine( e.StackTrace ); System.Console.ReadLine(); return 1; } }