Example #1
0
File: Deploy.cs Project: lacti/Lz
        protected static IEnumerable <string> UploadFile(string uploadPath, string bucketName, string awsAccessKey, string awsSecretKey, string cloudFrontDistributionId = null)
        {
            if (!uploadPath.EndsWith("\\"))
            {
                uploadPath += "\\";
            }

            using (var aws = new AwsHelper(awsAccessKey, awsSecretKey))
            {
                var uploadFileList = Directory.GetFiles(uploadPath, "*.*", SearchOption.AllDirectories).Select(e => e.Substring(uploadPath.Length)).ToList();
                var deleteFileList = new List <string>();

                var uploadFilesHashTableFile = Path.Combine(uploadPath, HashHelper.HashFileName);
                if (File.Exists(uploadFilesHashTableFile))
                {
                    var    localHashTable = HashHelper.ParseFilesHashTable(File.ReadAllText(uploadFilesHashTableFile));
                    string cloudHashTableContent;
                    if (aws.TryToGetObjectContent(bucketName, HashHelper.HashFileName, out cloudHashTableContent))
                    {
                        var cloudHashTable = HashHelper.ParseFilesHashTable(cloudHashTableContent);
                        var diff           = HashHelper.MakeDiff(localHashTable, cloudHashTable);
                        uploadFileList.RemoveAll(e => diff.SameFileList.Contains(e));
                        deleteFileList.AddRange(diff.DeletedFileList);
                    }
                }

                foreach (var file in uploadFileList)
                {
                    aws.UploadFile(bucketName, Path.Combine(uploadPath, file), AwsHelper.ToCloudPath(file));
                    yield return(file);
                }

                foreach (var file in deleteFileList)
                {
                    aws.DeleteFile(bucketName, AwsHelper.ToCloudPath(file));
                    yield return(file);
                }

                if (!string.IsNullOrWhiteSpace(cloudFrontDistributionId))
                {
                    aws.InvalidationCloudFront(cloudFrontDistributionId, uploadFileList.Concat(deleteFileList).Select(AwsHelper.ToCloudPath).ToList());
                }
            }
        }
Example #2
0
File: Deploy.cs Project: ktj007/Lz
        protected static IEnumerable<string> UploadFile(string uploadPath, string bucketName, string awsAccessKey, string awsSecretKey, string cloudFrontDistributionId = null)
        {
            if (!uploadPath.EndsWith("\\"))
                uploadPath += "\\";

            using (var aws = new AwsHelper(awsAccessKey, awsSecretKey))
            {
                var uploadFileList = Directory.GetFiles(uploadPath, "*.*", SearchOption.AllDirectories).Select(e => e.Substring(uploadPath.Length)).ToList();
                var deleteFileList = new List<string>();

                var uploadFilesHashTableFile = Path.Combine(uploadPath, HashHelper.HashFileName);
                if (File.Exists(uploadFilesHashTableFile))
                {
                    var localHashTable = HashHelper.ParseFilesHashTable(File.ReadAllText(uploadFilesHashTableFile));
                    string cloudHashTableContent;
                    if (aws.TryToGetObjectContent(bucketName, HashHelper.HashFileName, out cloudHashTableContent))
                    {
                        var cloudHashTable = HashHelper.ParseFilesHashTable(cloudHashTableContent);
                        var diff = HashHelper.MakeDiff(localHashTable, cloudHashTable);
                        uploadFileList.RemoveAll(e => diff.SameFileList.Contains(e));
                        deleteFileList.AddRange(diff.DeletedFileList);
                    }
                }

                foreach (var file in uploadFileList)
                {
                    aws.UploadFile(bucketName, Path.Combine(uploadPath, file), AwsHelper.ToCloudPath(file));
                    yield return file;
                }

                foreach (var file in deleteFileList)
                {
                    aws.DeleteFile(bucketName, AwsHelper.ToCloudPath(file));
                    yield return file;
                }

                if (!string.IsNullOrWhiteSpace(cloudFrontDistributionId))
                {
                    aws.InvalidationCloudFront(cloudFrontDistributionId, uploadFileList.Concat(deleteFileList).Select(AwsHelper.ToCloudPath).ToList());
                }
            }
        }