예제 #1
0
        // Construct a data store by providing a name for the endpoint (replica) and
        // a file to which we'll persist the sync metadata (file)
        internal AzureBlobSyncProvider(
            string name,
            AzureBlobStore store
            )
        {
            _name  = name;
            _store = store;

            _replicaMetadataFile = MetadataDirectory + _name + ".Metadata";
            _replicaIdFile       = MetadataDirectory + _name + ".Replicaid";

            // Set ItemIdFormat and ReplicaIdFormat for using Guid ids.
            _idFormats = new SyncIdFormatGroup();
            _idFormats.ChangeUnitIdFormat.IsVariableLength = false;
            _idFormats.ChangeUnitIdFormat.Length           = 4;
            _idFormats.ItemIdFormat.IsVariableLength       = false;
            _idFormats.ItemIdFormat.Length = 24;
            _idFormats.ReplicaIdFormat.IsVariableLength = false;
            _idFormats.ReplicaIdFormat.Length           = 16;
        }
        static void Main(string[] args)
        {
            _pause = false;
            try
            {
                if (!ParseArgs(args))
                {
                    PrintUsage();
                    Environment.Exit(-1);
                }

                if (!Directory.Exists(_localPathName))
                {
                    Console.WriteLine("Please ensure that the local target directory exists.");
                    Environment.Exit(-1);
                }

                //
                // Setup Store and Provider
                //
                string accountName = ConfigurationManager.AppSettings.Get("AccountName");
                string accountKey  = ConfigurationManager.AppSettings.Get("AccountSharedKey");
                string storageURL  = ConfigurationManager.AppSettings.Get("AccountStorageURL");
                CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey(accountName, accountKey), true);

                AzureBlobStore blobStore = new AzureBlobStore(_containerName, storageAccount);
                Console.WriteLine("Successfully created/attached to container {0}.", _containerName);
                AzureBlobSyncProvider azureProvider = new AzureBlobSyncProvider(_containerName, blobStore);
                azureProvider.ApplyingChange += new EventHandler <ApplyingBlobEventArgs>(UploadingFile);

                FileSyncProvider fileSyncProvider = null;
                try
                {
                    fileSyncProvider = new FileSyncProvider(_localPathName);
                }
                catch (ArgumentException)
                {
                    fileSyncProvider = new FileSyncProvider(Guid.NewGuid(), _localPathName);
                }
                fileSyncProvider.ApplyingChange += new EventHandler <ApplyingChangeEventArgs>(AzureBlobSync.DownloadingFile);

                try
                {
                    SyncOrchestrator orchestrator = new SyncOrchestrator();
                    orchestrator.LocalProvider  = fileSyncProvider;
                    orchestrator.RemoteProvider = azureProvider;

                    orchestrator.Synchronize();
                }
                finally
                {
                    fileSyncProvider.Dispose();
                }

                Console.WriteLine("Synchronization Complete");
                if (_pause)
                {
                    Console.ReadLine();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }