예제 #1
0
        /// <summary>
        /// requires aws cli to be configured
        /// </summary>
        /// <param name="sourceRegionEndpoint"></param>
        /// <param name="sourceBucketName"></param>
        /// <param name="sourceKey"></param>
        /// <param name="destRegionEndpoint"></param>
        /// <param name="destBucketName"></param>
        /// <param name="destKey"></param>
        /// <returns></returns>
        public static bool AwsCliCopyToUsEastIfDne(
            string AWSAccessKey,
            string AWSSecretKey,
            RegionEndpoint sourceRegionEndpoint,
            string sourceBucketName,
            string sourceKey,
            RegionEndpoint destRegionEndpoint,
            string destBucketName,
            string destKey
            )
        {
            try {
                string destUrl;
                if (Exists.Execute(
                        fileKey: destKey,
                        bucketName: destBucketName,
                        url: out destUrl,
                        re: destRegionEndpoint,
                        AWSAccessKey: AWSAccessKey,
                        AWSSecretKey: AWSSecretKey
                        ))
                {
                    return(true);
                }

                string sourceUrl;
                if (!Exists.Execute(
                        fileKey: sourceKey,
                        bucketName: sourceBucketName,
                        url: out sourceUrl,
                        re: sourceRegionEndpoint,
                        AWSAccessKey: AWSAccessKey,
                        AWSSecretKey: AWSSecretKey
                        ))
                {
                    LogIt.W("source did not exist:" + sourceBucketName + "|" + sourceKey);
                    return(false);
                }

                //execute copy
                //aws s3 sync s3://resurveys.original/45277802/ec92a4ed-8900-42d0-b8f4-31209130b38e/ s3://resurveys.original.useast/45277802/ec92a4ed-8900-42d0-b8f4-31209130b38e/ --source-region us-west-2 --region us-east-1
                try {
                    // Call WaitForExit and then the using-statement will close.
                    var args = "/c aws s3 sync"
                               + " s3://" + sourceBucketName + "/" + sourceKey.Replace(Path.GetFileName(sourceKey), "")
                               + " s3://" + destBucketName + "/" + destKey.Replace(Path.GetFileName(destKey), "")
                               + " --source-region " + sourceRegionEndpoint.SystemName
                               + " --region " + destRegionEndpoint.SystemName;

                    var psi = new ProcessStartInfo {
                        CreateNoWindow         = false,
                        UseShellExecute        = false,
                        FileName               = "cmd",
                        WindowStyle            = ProcessWindowStyle.Hidden,
                        RedirectStandardError  = true,
                        RedirectStandardOutput = true,
                        Arguments              = args
                    };
                    using (var p = Process.Start(psi)) {
                        //p.WaitForExit();
                        p.Start();
                        var output = p.StandardOutput.ReadToEnd();
                        Console.WriteLine(output);
                        var err = p.StandardError.ReadToEnd();
                        Console.WriteLine(err);
                        p.WaitForExit();

                        //p.Start();
                        //while (!p.StandardOutput.EndOfStream) {
                        //  LogIt.D(p.StandardOutput.ReadLine());
                        //}
                    }
                } catch (Exception ex) {
                    LogIt.E(ex);
                    return(false);
                }

                //check
                var exists = Exists.Execute(
                    fileKey: destKey,
                    bucketName: destBucketName,
                    url: out destUrl, re: destRegionEndpoint,
                    AWSAccessKey: AWSAccessKey,
                    AWSSecretKey: AWSSecretKey
                    );
                return(exists);
            } catch (Exception ex) {
                LogIt.E(ex);
                throw;
            }
        }
예제 #2
0
        public static bool Execute(
            string AWSAccessKey,
            string AWSSecretKey,
            RegionEndpoint regionEndpoint,
            string sourceBucketName,
            string sourceKey,
            string destBucketName,
            string destKey
            //out long? fileLengthBytes
            )
        {
            //fileLengthBytes = null;
            try {
                //validate exists
                string sourceUrl;
                if (!Exists.Execute(
                        fileKey: sourceKey,
                        bucketName: sourceBucketName,
                        url: out sourceUrl,
                        re: regionEndpoint,
                        AWSAccessKey: AWSAccessKey,
                        AWSSecretKey: AWSSecretKey
                        ))
                {
                    LogIt.W("source did not exist:" + sourceBucketName + "|" + sourceKey);
                    return(false);
                }

                //validate dest doesn't already exist, fail if it does bc we aren't validating that it is different? maybe shar eeach in future?
                string destUrl;
                if (Exists.Execute(
                        fileKey: destKey,
                        bucketName: destBucketName,
                        url: out destUrl,
                        re: regionEndpoint,
                        AWSAccessKey: AWSAccessKey,
                        AWSSecretKey: AWSSecretKey
                        ))
                {
                    LogIt.W("dest existed already:" + sourceBucketName + "|" + sourceKey);
                    return(false);
                }

                //copy
                using (var s3c = new AmazonS3Client(
                           awsAccessKeyId: AWSAccessKey,
                           awsSecretAccessKey: AWSSecretKey,
                           region: regionEndpoint
                           )) {
                    var cor = s3c.CopyObject(new CopyObjectRequest {
                        SourceBucket      = sourceBucketName,
                        SourceKey         = sourceKey,
                        DestinationBucket = destBucketName,
                        DestinationKey    = destKey
                    });
                    //fileLengthBytes = cor.
                    return(cor.HttpStatusCode == System.Net.HttpStatusCode.OK);
                }
            } catch (Exception ex) {
                LogIt.E(ex);
                return(false);
            }
        }