コード例 #1
0
ファイル: BackLog.cs プロジェクト: tianma3798/MySql_Back_Tool
        /// <summary>
        /// 静态内容初始化
        /// </summary>
        static BackLog()
        {
            //将 日志记录放在当前DLL的目录下
            string fullname = LocalPathHelper.GetCurrentDLLSub("log") + "\\MySqlBack_Log.txt";

            _log = new LogHelper.LogHelper(fullname, true);
        }
コード例 #2
0
        /// <summary>
        /// 获取文件存储位置
        /// </summary>
        /// <returns></returns>
        public string GetFileName()
        {
            string filename = LocalPathHelper.GetCurrentDLLSub("data") + string.Format("\\{0}.xml", _Character);

            if (File.Exists(filename) == false)
            {
                File.Create(filename).Close();
            }
            return(filename);
        }
コード例 #3
0
        /// <summary>
        /// 获取 文件存储位置
        /// </summary>
        /// <returns></returns>
        public string GetFileName()
        {
            string filename = LocalPathHelper.GetCurrentDLLSub("data") + "\\hostinfo.xml";

            if (File.Exists(filename) == false)
            {
                File.Create(filename).Close();
            }

            return(filename);
        }
コード例 #4
0
        public async Task CopyAsync(string source, string destination)
        {
            if (!ResourceHelper.TryParseResource(source, out (string?bucket, string resource, LocationType locationType, ResourceType resourceType)parsedSource))
            {
                throw new CommandException(ErrorType.Argument, $"Failed to parse source: {source}");
            }

            if (!ResourceHelper.TryParseResource(destination, out (string?bucket, string resource, LocationType locationType, ResourceType resourceType)parsedDestination))
            {
                throw new CommandException(ErrorType.Argument, $"Failed to parse destination: {destination}");
            }

            if (parsedSource.bucket == null)
            {
                throw new S3Exception("Unable to parse bucket in source");
            }

            if (parsedDestination.bucket == null)
            {
                throw new S3Exception("Unable to parse bucket in destination");
            }

            if (parsedSource.locationType == LocationType.Local && parsedDestination.locationType == LocationType.Remote)
            {
                switch (parsedSource.resourceType)
                {
                case ResourceType.File:
                {
                    string objectKey;

                    switch (parsedDestination.resourceType)
                    {
                    //Source: Local file - Destination: remote file
                    case ResourceType.File:
                        objectKey = parsedDestination.resource;
                        break;

                    //Source: Local file - Destination: remote directory
                    case ResourceType.Directory:
                        objectKey = RemotePathHelper.Combine(parsedDestination.resource, LocalPathHelper.GetFileName(parsedSource.resource));
                        break;

                    //We don't support expand on the destination
                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    using (FileStream fs = File.OpenRead(parsedSource.resource))
                        await RequestHelper.ExecuteRequestAsync(_client, c => c.PutObjectAsync(parsedDestination.bucket, objectKey, fs)).ConfigureAwait(false);

                    return;
                }

                case ResourceType.Directory:
                {
                    switch (parsedDestination.resourceType)
                    {
                    //Source: Local directory - Destination: remote directory
                    case ResourceType.Directory:
                        foreach (string file in Directory.GetFiles(parsedSource.resource))
                        {
                            string?directory = LocalPathHelper.GetDirectoryName(file);
                            string name      = LocalPathHelper.GetFileName(file);
                            string objectKey = RemotePathHelper.Combine(parsedDestination.resource, directory, name);

                            using (FileStream fs = File.OpenRead(file))
                                await RequestHelper.ExecuteRequestAsync(_client, c => c.PutObjectAsync(parsedDestination.bucket, objectKey, fs)).ConfigureAwait(false);
                        }

                        return;

                    //We don't support files or expand on the destination
                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            if (parsedSource.locationType == LocationType.Remote && parsedDestination.locationType == LocationType.Local)
            {
                switch (parsedSource.resourceType)
                {
                case ResourceType.File:
                {
                    string localFile;

                    switch (parsedDestination.resourceType)
                    {
                    //Source: remote file - Destination: local file
                    case ResourceType.File:
                        localFile = parsedDestination.resource;
                        break;

                    //Source: remote file - Destination: local directory
                    case ResourceType.Directory:
                        localFile = LocalPathHelper.Combine(parsedDestination.resource, RemotePathHelper.GetFileName(parsedSource.resource));
                        break;

                    //We don't support expand on the destination
                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    GetObjectResponse resp = await RequestHelper.ExecuteRequestAsync(_client, c => c.GetObjectAsync(parsedSource.bucket, parsedSource.resource)).ConfigureAwait(false);

                    using (Stream s = resp.Content)
                        using (FileStream fs = File.OpenWrite(localFile))
                            await s.CopyToAsync(fs).ConfigureAwait(false);

                    return;
                }

                case ResourceType.Directory:
                {
                    switch (parsedDestination.resourceType)
                    {
                    //Source: remote directory - Destination: local directory
                    case ResourceType.Directory:
                        await foreach (S3Object s3Object in RequestHelper.ExecuteAsyncEnumerable(_client, c => c.ListAllObjectsAsync(parsedSource.bucket, config: req =>
                            {
                                req.Prefix = parsedSource.resource;
                            })))
                        {
                            string destFolder = parsedDestination.resource;
                            string destFile   = LocalPathHelper.Combine(destFolder, parsedDestination.resource, s3Object.ObjectKey);

                            GetObjectResponse resp = await RequestHelper.ExecuteRequestAsync(_client, c => c.GetObjectAsync(parsedSource.bucket, s3Object.ObjectKey)).ConfigureAwait(false);

                            await resp.Content.CopyToFileAsync(destFile).ConfigureAwait(false);
                        }

                        return;

                    //We don't support file or expand on the destination
                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }