예제 #1
0
        static int Main(string[] args)
        {
            IAmazonS3 client = null;

            Amazon.Runtime.AnonymousAWSCredentials testan = new Amazon.Runtime.AnonymousAWSCredentials();
            try
            {
                using (client = new AmazonS3Client(testan, Amazon.RegionEndpoint.EUWest1)) // リージョンは今後変わる可能性有り。
                {
                    var bucket = args[0];
                    var file   = args[1];
                    try
                    {
                        var request = new PutObjectRequest
                        {
                            BucketName = bucket,
                            FilePath   = file,
                            CannedACL  = S3CannedACL.BucketOwnerFullControl,
                        };
                        var response = client.PutObject(request);
                        Console.WriteLine("send success.");
                        return(0);
                    }
                    catch (AmazonS3Exception amazonS3Excetion)
                    {
                        if (amazonS3Excetion.ErrorCode != null &&
                            (amazonS3Excetion.ErrorCode.Equals("AccessDenied")))
                        {
                            Console.WriteLine("access denied.");
                        }
                        else if (amazonS3Excetion.ErrorCode != null &&
                                 (amazonS3Excetion.ErrorCode.Equals("InvalidAccessKeyId") ||
                                  amazonS3Excetion.ErrorCode.Equals("InvalidSeculity")))
                        {
                            Console.WriteLine("server connection error.");
                        }
                        else
                        {
                            Console.WriteLine("send error.");
                        }
                        return(1);
                    }
                };
            }
            catch (Amazon.Runtime.AmazonServiceException ex)
            {
                Console.WriteLine("server connection error.");
                return(1);
            }
        }
예제 #2
0
        private void ThreadProcSafe()
        {
            try {
                var webRequest   = WebRequest.Create("https://hiraokatakuya.github.io/aperygenerateteacher/version.txt");
                var webResponse  = webRequest.GetResponse();
                var webStream    = webResponse.GetResponseStream();
                var streamReader = new StreamReader(webStream, Encoding.UTF8);
                var htmlSource   = streamReader.ReadToEnd();
                streamReader.Close();
                webStream.Close();
                webResponse.Close();
                if (htmlSource == version + "\n")
                {
                    // 最新のバージョン
                    // nop
                }
                else
                {
                    // バージョンが更新されている。
                    this.SetText("最新バージョンをダウンロードしてからご協力頂けますと幸いです。");
                    isOlderVersion = true;
                    SetButton(true);
                    return;
                }
            }
            catch (WebException ex)
            {
                // バージョン確認にアクセス出来なかった。
                // 取りあえず、そのまま通常通りデータ生成を行う。
                // nop
            }
            // send test aws s3
            // テスト用ファイル作成
            String testfile = "test.txt";

            using (System.IO.FileStream stream = System.IO.File.Create(testfile))
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
            testPassed = false;
            Amazon.Runtime.AnonymousAWSCredentials testan = new Amazon.Runtime.AnonymousAWSCredentials();
            try
            {
                using (client = new AmazonS3Client(testan, Amazon.RegionEndpoint.EUWest1)) // リージョンは今後変わる可能性有り。
                {
                    WriteContent(testfile, true);
                };
            }
            catch (Amazon.Runtime.AmazonServiceException ex)
            {
                this.SetText("サーバー接続に失敗しました。");
                SetButton(true);
                return;
            }
            if (testPassed)
            {
                this.SetLoopText("0");
                for (long i = 1; i <= loops; ++i)
                {
                    process = new Process();
                    ProcessStartInfo startInfo = new ProcessStartInfo("apery.exe");
                    startInfo.CreateNoWindow         = true;
                    startInfo.RedirectStandardInput  = true;
                    startInfo.RedirectStandardOutput = true;
                    startInfo.UseShellExecute        = false;

                    process.StartInfo = startInfo;
                    process.Start();

                    String outfile = "out_" + RandomString(20) + ".hcpe";
                    String line    = null;
                    String cmd     = "make_teacher roots.hcp " + outfile + " " + threads + " " + teacherNodes;
                    process.StandardInput.WriteLine(cmd);
                    while ((line = process.StandardOutput.ReadLine()) != null)
                    {
                        this.SetText(line);
                        if (System.Text.RegularExpressions.Regex.IsMatch(line, "^Made"))
                        {
                            break;
                        }
                    }
                    process.StandardInput.WriteLine("quit");
                    while ((line = process.StandardOutput.ReadLine()) != null)
                    {
                        ;
                    }
                    if (!process.HasExited)
                    {
                        process.Kill();
                    }
                    process.Close();

                    this.SetText("教師データをシャッフルしています。");
                    String shufOutfile = "shuf" + outfile;
                    startInfo                        = new ProcessStartInfo("shuffle_hcpe.exe");
                    startInfo.Arguments              = outfile + " " + shufOutfile;
                    startInfo.CreateNoWindow         = true;
                    startInfo.RedirectStandardInput  = true;
                    startInfo.RedirectStandardOutput = true;
                    startInfo.UseShellExecute        = false;

                    process.StartInfo = startInfo;
                    process.Start();
                    process.WaitForExit();
                    File.Delete(outfile);
                    this.SetText("教師データのシャッフルが完了しました。");
#if false
                    this.SetText("教師データ圧縮中");
                    String outCompressedFile = shufOutfile + ".7z";
                    CompressFile(shufOutfile, outCompressedFile);
                    this.SetText("教師データ圧縮完了");
                    File.Delete(shufOutfile);
#else
                    String outCompressedFile = shufOutfile;
#endif
                    // send aws s3
                    Amazon.Runtime.AnonymousAWSCredentials an = new Amazon.Runtime.AnonymousAWSCredentials();
                    try
                    {
                        using (client = new AmazonS3Client(an, Amazon.RegionEndpoint.EUWest1)) // リージョンは今後変わる可能性有り。
                        {
                            WriteContent(outCompressedFile, false);
                        };
                    }
                    catch (Amazon.Runtime.AmazonServiceException ex)
                    {
                        this.SetText("サーバー接続に失敗しました。");
                        SetButton(true);
                        return;
                    }
                    this.SetLoopText(i.ToString());
                }
            }
            SetButton(true);
        }