static int Main(string[] args) { if (awsAccessKeyId.StartsWith("<INSERT")) { 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 (var format in callingFormats) { var conn = new AWSAuthConnection(awsAccessKeyId, awsSecretAccessKey, false, format); using (var response = conn.createBucket(bucketName, Location.DEFAULT, null)) assertEquals( HTTP_OK, response.Status, "couldn't create bucket"); using (var 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 const string text = "this is a test"; var key = "example.txt"; var innerKey = "test/inner.txt"; const string badcharKey = "x+/last{key}.txt"; const string lastKey = "z-last-key.txt"; using (var response = conn.put(bucketName, key, new S3Object(text, null), null)) assertEquals( HTTP_OK, response.Status, "couldn't put simple object"); using (var response = conn.put(bucketName, innerKey, new S3Object(text, null), null)) assertEquals( HTTP_OK, response.Status, "couldn't put simple object"); using (var response = conn.put(bucketName, lastKey, new S3Object(text, null), null)) assertEquals( HTTP_OK, response.Status, "couldn't put simple object"); using (var response = conn.put(bucketName, badcharKey, new S3Object(text, null), null)) assertEquals( HTTP_OK, response.Status, "couldn't put simple object"); // plain list using (var 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 (var 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 (var 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; using (var 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 (var 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 (var 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 (var 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 (var response = conn.delete(bucketName, innerKey, null)) assertEquals( HTTP_NO_CONTENT, response.Status, "couldn't delete entry"); // remove badcharKey using (var response = conn.delete(bucketName, badcharKey, null)) assertEquals( HTTP_NO_CONTENT, response.Status, "couldn't delete entry"); // remove last key using (var response = conn.delete(bucketName, lastKey, null)) assertEquals( HTTP_NO_CONTENT, response.Status, "couldn't delete entry"); // End of delimiter tests var metadata = new SortedList { { "title", "title" } }; using (var response = conn.put(bucketName, key, new S3Object(text, metadata), null)) assertEquals( HTTP_OK, response.Status, "couldn't put complex object"); using (var 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 (var response = conn.put(bucketName, key, new S3Object(text, null), null)) assertEquals( HTTP_OK, response.Status, "couldn't put simple object"); var metadataWithSpaces = new SortedList(); const string titleWithSpaces = " \t title with leading and trailing spaces "; metadataWithSpaces.Add("title", titleWithSpaces); using (var 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 (var 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"); } var weirdKey = "&=/%# ++/++"; using (var response = conn.put(bucketName, weirdKey, new S3Object(text, null), null)) assertEquals( HTTP_OK, response.Status, "couldn't put weird key"); using (var getResponse = conn.get(bucketName, weirdKey, null)) assertEquals( HTTP_OK, getResponse.Status, "couldn't get weird key"); string acl; using (var getResponse = conn.getACL(bucketName, key, null)) { assertEquals( HTTP_OK, getResponse.Status, "couldn't get acl"); acl = getResponse.Object.Data; } using (var response = conn.putACL(bucketName, key, acl, null)) assertEquals( HTTP_OK, response.Status, "couldn't put acl"); string bucketACL; using (var getResponse = conn.getBucketACL(bucketName, null)) { assertEquals( HTTP_OK, getResponse.Status, "couldn't get bucket acl"); bucketACL = getResponse.Object.Data; } using (var response = conn.putBucketACL(bucketName, bucketACL, null)) assertEquals( HTTP_OK, response.Status, "couldn't put bucket acl"); // bucket logging tests string bucketLogging; using (var getResponse = conn.getBucketLogging(bucketName, null)) { assertEquals( HTTP_OK, getResponse.Status, "couldn't get bucket logging config"); bucketLogging = getResponse.Object.Data; } using (var response = conn.putBucketLogging(bucketName, bucketLogging, null)) assertEquals( HTTP_OK, response.Status, "couldn't put bucket logging config"); // end bucket logging tests using (var listBucketResponse = conn.listBucket(bucketName, null, null, 0, null)) { assertEquals( HTTP_OK, listBucketResponse.Status, "couldn't list bucket"); var 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 (var 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 (var 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 (var response = conn.delete(bucketName, entry.Key, null)) assertEquals( HTTP_NO_CONTENT, response.Status, "couldn't delete entry"); } } ArrayList buckets; using (var listAllMyBucketsResponse = conn.listAllMyBuckets(null)) { assertEquals( HTTP_OK, listAllMyBucketsResponse.Status, "couldn't list all my buckets"); buckets = listAllMyBucketsResponse.Buckets; } using (var response = conn.deleteBucket(bucketName, null)) assertEquals( HTTP_NO_CONTENT, response.Status, "couldn't delete bucket"); using (var 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"); } var 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"); } Console.WriteLine("OK (" + assertionCount + " tests passed)"); return(0); }
static int Main(string[] args) { try { if (awsAccessKeyId.StartsWith("<INSERT")) { Console.WriteLine("Please examine S3Driver.cs and update it with your credentials"); return(1); } var conn = new AWSAuthConnection(awsAccessKeyId, awsSecretAccessKey); var 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)) { Console.WriteLine("----- bucket already exists! -----"); } else { 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 (var response = conn.createBucket(bucketName, Location.EU, null)) Console.WriteLine(response.getResponseMessage()); } Console.WriteLine("----- bucket location -----"); using (var locationResponse = conn.getBucketLocation(bucketName)) if (locationResponse.Location == null) { Console.WriteLine("Location: <error>"); } else if (locationResponse.Location.Length == 0) { Console.WriteLine("Location: <default>"); } else { Console.WriteLine("Location: '{0}'", locationResponse.Location); } Console.WriteLine("----- listing bucket -----"); using (var listBucketResponse = conn.listBucket(bucketName, null, null, 0, null)) dumpBucketListing(listBucketResponse); Console.WriteLine("----- putting object -----"); var obj = new S3Object("This is a test", null); var headers = new SortedList { { "Content-Type", "text/plain" } }; using (var response = conn.put(bucketName, keyName, obj, headers)) Console.WriteLine(response.getResponseMessage()); Console.WriteLine("----- listing bucket -----"); using (var listBucketResponse = conn.listBucket(bucketName, null, null, 0, null)) dumpBucketListing(listBucketResponse); Console.WriteLine("----- query string auth example -----"); generator.ExpiresIn = 60 * 1000; Console.WriteLine("Try this url in your web browser (it will only work for 60 seconds)\n"); var url = generator.get(bucketName, keyName, null); Console.WriteLine(url); Console.Write("\npress enter >"); Console.ReadLine(); Console.WriteLine( "\nNow try just the url without the query string arguments. It should fail.\n"); Console.WriteLine(generator.makeBaseURL(bucketName, keyName)); Console.Write("\npress enter >"); Console.ReadLine(); Console.WriteLine("----- putting object with metadata and public read acl -----"); var metadata = new SortedList { { "blah", "foo" } }; obj = new S3Object("this is a publicly readable test", metadata); headers = new SortedList { { "x-amz-acl", "public-read" }, { "Content-Type", "text/plain" } }; using (var response = conn.put(bucketName, keyName + "-public", obj, headers)) Console.WriteLine(response.getResponseMessage()); Console.WriteLine("----- anonymous read test -----"); Console.WriteLine("\nYou should be able to try this in your browser\n"); var publicURL = generator.get(bucketName, keyName + "-public", null); Console.WriteLine(publicURL); Console.Write("\npress enter >"); Console.ReadLine(); Console.WriteLine("----- path style url example -----"); 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.)"); 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; Console.WriteLine(generator.get(bucketName, keyName, null)); Console.Write("\npress enter> "); Console.ReadLine(); Console.WriteLine("----- getting object's acl -----"); using (var response = conn.getACL(bucketName, keyName, null)) Console.WriteLine(response.Object.Data); Console.WriteLine("----- deleting objects -----"); using (var response = conn.delete(bucketName, keyName, null)) Console.WriteLine(response.getResponseMessage()); using (var response = conn.delete(bucketName, keyName + "-public", null)) Console.WriteLine(response.getResponseMessage()); Console.WriteLine("----- listing bucket -----"); using (var listBucketResponse = conn.listBucket(bucketName, null, null, 0, null)) dumpBucketListing(listBucketResponse); Console.WriteLine("----- listing all my buckets -----"); using (var listBucketResponse = conn.listAllMyBuckets(null)) dumpAllMyBucketListing(listBucketResponse); Console.WriteLine("----- deleting bucket -----"); using (var response = conn.deleteBucket(bucketName, null)) Console.WriteLine(response.getResponseMessage()); return(0); } catch (WebException e) { Console.WriteLine(e.Status); Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); Console.ReadLine(); return(1); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); Console.ReadLine(); return(1); } }