/* * MindTouch Dream - a distributed REST framework * Copyright (C) 2006, 2007 MindTouch, Inc. * www.mindtouch.com oss@mindtouch.com * * For community documentation and downloads visit www.opengarden.org; * please review the licensing section. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * http://www.gnu.org/copyleft/lesser.html */ using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; namespace MindTouch.Dream.Samples { internal class Program { //--- Constants --- private const string AWS_DATE = "X-Amz-Date"; //--- Class Fields --- private static string _public_key; private static string _private_key; private static Plug _s3 = Plug.New("http://s3.amazonaws.com").WithPreHandler(S3AuthenticationHeader); //--- Class Methods --- private static void Main(string[] args) { // verify argument count if(args.Length < 4) { ShowUsage(); return; } // store public and private keys _public_key = args[0]; _private_key = args[1]; // check command XDoc result = XDoc.Empty; switch(args[2]) { case "list": // get the contents of a bucket (argument 3) result = _s3.At(args[3]).GetAsync().Wait().AsDocument(); break; case "add": // upload file (argument 4) into a bucket (argument 3) result = _s3.At(args[3], System.IO.Path.GetFileName(args[4])).PutAsync(DreamMessage.FromFile(args[4])).Wait().AsDocument(); break; default: // unrecognized command ShowUsage(); return; } // show outcome on console Console.WriteLine(result.ToPrettyString()); } private static void ShowUsage() { Console.WriteLine("Amazon S3 Utility"); Console.WriteLine("USAGE: s3.exe [arg1] ... [argN]"); Console.WriteLine(" list list contents of a bucket"); Console.WriteLine(" add add file to a bucket"); } private static DreamMessage S3AuthenticationHeader(string verb, XUri uri, DreamMessage message) { // add amazon date header string date = DateTime.UtcNow.ToString("r"); message.Headers[AWS_DATE] = date; // add authorization header string result = string.Format("{0}\n{1}\n{2}\n\n{3}:{4}\n{5}", verb, message.Headers[DreamMessage.HEADER_CONTENT_MD5], message.ContentType, AWS_DATE.ToLower(), date, uri.Path); HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(_private_key)); string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(result))); message.Headers[DreamMessage.HEADER_AUTHORIZATION] = string.Format("AWS {0}:{1}", _public_key, signature); return message; } } }