コード例 #1
0
ファイル: Uploader.cs プロジェクト: doverhill/BlobSync
        public static async Task Sync(string connectionString, string connectionUrl, string containerName, string localPath, SyncSettings settings, bool verbose)
        {
            var contentTypeMappings = new Dictionary <string, string>
            {
                { ".html", "text/html" },
                { ".css", "text/css" },
                { ".js", "text/javascript" },
                { ".wasm", "application/wasm" }
            };

            var sync     = new BlobSyncCore(connectionString, connectionUrl, containerName, localPath);
            var syncInfo = await sync.GetSyncInfoAsync(verbose);

            BlobServiceClient client;

            if (connectionUrl != null)
            {
                client = new BlobServiceClient(new Uri(connectionUrl));
            }
            else
            {
                client = new BlobServiceClient(connectionString);
            }
            var container = client.GetBlobContainerClient(containerName);

            if (settings.HasFlag(SyncSettings.Delete))
            {
                foreach (var onlyRemote in syncInfo.OnlyRemote)
                {
                    // this file exists only as a blob, delete the blob
                    Console.WriteLine($"Deleting remote {onlyRemote.Name}...");
                    var blob = container.GetBlobClient(onlyRemote.Name);
                    await blob.DeleteIfExistsAsync();
                }
            }

            foreach (var differs in syncInfo.Differs)
            {
                // this file exists also as a blob but differs, upload and overwrite
                await PushFile("Updating remote", localPath, contentTypeMappings, differs.Blob);
            }

            foreach (var onlyLocal in syncInfo.OnlyLocal)
            {
                // this file exists only locally so upload it as a blob
                await PushFile("Uploading", localPath, contentTypeMappings, container, onlyLocal);
            }

            if (settings.HasFlag(SyncSettings.Force))
            {
                foreach (var identical in syncInfo.Identical)
                {
                    // this file exists also as a blob and is identical, don't do anything
                    await PushFile("Force updating remote", localPath, contentTypeMappings, identical.Blob);
                }
            }
        }
コード例 #2
0
        public static async Task <bool> Sync(string connectionString, string connectionUrl, string containerName, string localPath, SyncSettings settings, bool verbose)
        {
            var sync     = new BlobSyncCore(connectionString, connectionUrl, containerName, localPath);
            var syncInfo = await sync.GetSyncInfoAsync(verbose);

            BlobServiceClient client;

            if (connectionUrl != null)
            {
                client = new BlobServiceClient(new Uri(connectionUrl));
            }
            else
            {
                client = new BlobServiceClient(connectionString);
            }
            var container = client.GetBlobContainerClient(containerName);

            foreach (var onlyRemote in syncInfo.OnlyRemote)
            {
                Console.WriteLine($"Downloading {onlyRemote.Name}...");
                var blob = container.GetBlobClient(onlyRemote.Name);
                var path = Path.Combine(localPath, onlyRemote.Name);
                Directory.CreateDirectory(Path.GetDirectoryName(path));

                using (var file = File.OpenWrite(path))
                {
                    await blob.DownloadToAsync(file);
                }
            }

            foreach (var differs in syncInfo.Differs)
            {
                Console.WriteLine($"Updating local {differs.Blob.Name}...");
                var blob = container.GetBlobClient(differs.Blob.Name);
                var path = Path.Combine(localPath, differs.Blob.Name);

                File.Delete(path);
                using (var file = File.OpenWrite(path))
                {
                    await blob.DownloadToAsync(file);
                }
            }

            if ((settings & SyncSettings.Delete) != 0)
            {
                foreach (var onlyLocal in syncInfo.OnlyLocal)
                {
                    Console.WriteLine($"Deleting local {onlyLocal}...");
                    File.Delete(Path.Combine(localPath, onlyLocal.Name));
                }
            }

            return(sync.Verify(syncInfo, verbose));
        }