Example #1
0
        public async Task init()
        {
            MyCalculateHash = new SprightlySoftAWS.S3.CalculateHash();
            //yCalculateHash.ProgressChangedEvent += MyCalculateHash_ProgressChangedEvent;

            MyUpload = new SprightlySoftAWS.S3.Upload();
            //MyUpload.ProgressChangedEvent += MyUpload_ProgressChangedEvent;

            CalculateHashBackgroundWorker                     = new System.ComponentModel.BackgroundWorker();
            CalculateHashBackgroundWorker.DoWork             += CalculateHashBackgroundWorker_DoWork;
            CalculateHashBackgroundWorker.RunWorkerCompleted += CalculateHashBackgroundWorker_RunWorkerCompleted;

            UploadBackgroundWorker                     = new System.ComponentModel.BackgroundWorker();
            UploadBackgroundWorker.DoWork             += UploadBackgroundWorker_DoWork;
            UploadBackgroundWorker.RunWorkerCompleted += UploadBackgroundWorker_RunWorkerCompleted;
            //Application.DoEvents();
            StreamWriter SW = new StreamWriter(HttpContext.Server.MapPath("../") + "init.txt");

            SW.WriteLine("public void init()");
            SW.Close();
            //Run the hash calculation in a BackgroundWorker process.  Calculating the hash of a
            //large file will take a while.  Running the process in a BackgroundWorker will prevent
            //the form from locking up.

            //Use a hash table to pass parameters to the function in the BackgroundWorker.
            Task task = new Task(ProcessDataAsync);

            task.Start();
            task.Wait();
        }
Example #2
0
        public bool MakePUTS3UploadRequestToAws(bool useSSL, string bucketName, string folderName)
        {
            var result = false;
            var mySettings = new Properties.Settings();

            try
            {
                //create an instance of the REST class
                var myUpload = new SprightlySoftAWS.S3.Upload();

                // build the URL to call. The bucket name and key name must be empty to return all buckets
                RequestURL = myUpload.BuildS3RequestURL(useSSL, "s3.amazonaws.com", bucketName, folderName + Path.GetFileName(UploadFileName), string.Empty);

                RequestMethod = "PUT";

                //add a date header
                ExtraRequestHeaders = new Dictionary<String, String> {
                {
                    "x-amz-date", DateTime.UtcNow.ToString("r")
                }};

                //generate the authorization header value
                var authorizationValue = myUpload.GetS3AuthorizationValue(RequestURL, RequestMethod, ExtraRequestHeaders, mySettings.AWSAccessKeyId, mySettings.AWSSecretAccessKey);
                // add the public access permission header
                //ExtraRequestHeaders.Add("x-amz-acl", "public-read");
                //add the authorization header
                ExtraRequestHeaders.Add("Authorization", authorizationValue);

                //call UploadFile to submit the upload request
                result = myUpload.UploadFile(RequestURL, RequestMethod, ExtraRequestHeaders, UploadFileName);

                var requestResult = myUpload.LogData;

                if (result)
                {
                    util.LogMessage("UploadSuccess", requestResult);
                }
                else
                {
                    util.LogMessage("UploadFailure", requestResult);
                }
            }
            catch (Exception ex)
            {
                util.ErrorNotification(ex);
            }

            return result;
        }
Example #3
0
        public static int Main(string[] args)
        {
            try
            {
                NameValueCollection settings = new NameValueCollection();

                List<string> indexedArgs = new List<string>();
                string command = null;

                foreach (var arg in args)
                {
                    if (arg.StartsWith("--"))
                    {
                        if (arg.Contains("="))
                        {
                            // Key/Value pair
                            int equalIndex = arg.IndexOf('=');
                            string key = arg.Substring(2, equalIndex - 2);
                            string value = arg.Substring(equalIndex + 1);

                            settings.Add(key.ToLower(), value);
                            continue;
                        }
                    }
                    else
                    {
                        if (command == null)
                            command = arg.ToUpper();
                        else
                            indexedArgs.Add(arg);
                    }
                }

                if (string.IsNullOrEmpty(command))
                    throw new ArgumentException("Missing command");

                if (string.IsNullOrEmpty(settings["accesskey"]))
                    throw new ArgumentException("Missing AccessKey");
                if (string.IsNullOrEmpty(settings["accesssecret"]))
                    throw new ArgumentException("Missing AccessSecret");

                if (string.IsNullOrEmpty(settings["server"]))
                    settings.Add("server", "s3.amazonaws.com");

                string endpoint = settings["server"];
                if (endpoint.EndsWith("/"))
                    endpoint = endpoint.Substring(0, endpoint.Length - 1);

                if (!string.IsNullOrEmpty(settings["bucket"]))
                    endpoint += "/" + settings["bucket"];

                if (command == "PUT")
                {
                    if (indexedArgs.Count < 1)
                        throw new ArgumentException("Missing filename");

                    var upload = new SprightlySoftAWS.S3.Upload();

                    foreach (var localFile in indexedArgs)
                    {
                        string fileName = System.IO.Path.GetFileName(localFile);

                        var requestURL = upload.BuildS3RequestURL(true, endpoint, string.Empty, fileName, string.Empty);

                        var headers = new Dictionary<string, string>();
                        headers.Add("x-amz-date", DateTime.UtcNow.ToString("r"));

                        var authValue = upload.GetS3AuthorizationValue(requestURL, "PUT", headers, settings["accesskey"], settings["accesssecret"]);
                        headers.Add("Authorization", authValue);

                        if (!upload.UploadFile(requestURL, "PUT", headers, localFile))
                            throw new Exception(string.Format("Failed to upload file: {0}   Error: {1}",
                                localFile, upload.ErrorDescription));
                    }

                    return 0;
                }
                else if (command == "DELETE")
                {
                    if (indexedArgs.Count < 1)
                        throw new ArgumentException("Missing filename");

                    var delete = new SprightlySoftAWS.REST();

                    foreach (var remoteFilename in indexedArgs)
                    {
                        var requestURL = delete.BuildS3RequestURL(true, endpoint, string.Empty, remoteFilename, string.Empty);

                        var headers = new Dictionary<string, string>();
                        headers.Add("x-amz-date", DateTime.UtcNow.ToString("r"));

                        var authValue = delete.GetS3AuthorizationValue(requestURL, "DELETE", headers, settings["accesskey"], settings["accesssecret"]);
                        headers.Add("Authorization", authValue);

                        if (!delete.MakeRequest(requestURL, "PUT", headers, remoteFilename))
                            throw new Exception(string.Format("Failed to delete file: {0}   Error: {1}",
                                remoteFilename, delete.ErrorDescription));
                    }

                    return 0;
                }
                else if (command == "GET")
                {
                    if (indexedArgs.Count < 1)
                        throw new ArgumentException("Missing filename");

                    var download = new SprightlySoftAWS.S3.Download();

                    foreach (var remoteFilename in indexedArgs)
                    {
                        var requestURL = download.BuildS3RequestURL(true, endpoint, string.Empty, remoteFilename, string.Empty);

                        var headers = new Dictionary<string, string>();
                        headers.Add("x-amz-date", DateTime.UtcNow.ToString("r"));

                        var authValue = download.GetS3AuthorizationValue(requestURL, "GET", headers, settings["accesskey"], settings["accesssecret"]);
                        headers.Add("Authorization", authValue);

                        string localFilename = System.IO.Path.GetFullPath(remoteFilename);

                        System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(localFilename));

                        if (!download.DownloadFile(requestURL, "GET", headers, localFilename, false))
                            throw new Exception(string.Format("Failed to download file: {0}   Error: {1}",
                                remoteFilename, download.ErrorDescription));
                    }

                    return 0;
                }
                else
                    Console.WriteLine("Unknown command: " + command);

                return 1;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                return 255;
            }
        }