Esempio n. 1
0
        public async Task <CloudAppendBlob> GetBlobReference(string shardingKey, string containerName, string streamId, CancellationToken token, StreamOptions streamOptions)
        {
            var client    = _hydra.CreateBlobClient(shardingKey);
            var account   = client.Credentials.AccountName;
            var container = client.GetContainerReference(containerName);
            var semaphore = GetSemaphore(account, containerName, streamId);

            if (await semaphore.WaitAsync(TimeSpan.FromSeconds(5), token))
            {
                try
                {
                    if (streamOptions.CreateContainer && !GetContainerExists(account, containerName))
                    {
                        await container.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Off, null, null, token);

                        SetContainerExists(account, containerName);
                    }

                    var blob = container.GetAppendBlobReference(streamId);

                    if (streamOptions.CreateBlob && !GetStreamExists(account, containerName, streamId) && !await blob.ExistsAsync(null, null, token))
                    {
                        await blob.CreateOrReplaceAsync(null, null, null, token);

                        SetStreamExists(account, containerName, streamId);
                    }

                    return(blob);
                }
                finally
                {
                    semaphore.Release();
                }
            }

            throw new TimeoutException("Unable to get blob reference");
        }
Esempio n. 2
0
        private async Task Copy(CloudBlobClient sourceClient, IHydra target, CancellationToken token)
        {
            var response = await sourceClient.ListContainersSegmentedAsync(null);

            do
            {
                foreach (var sourceItem in response.Results.Where(x => string.IsNullOrWhiteSpace(Options.Object) || x.Name.EqualsCi(Options.Object)).Where(x => !x.Name.StartsWith("azure-")))
                {
                    _logger.LogInformation($"Processing {sourceItem.GetType().Name} '{sourceItem.Name}'.");

                    var sourceResponse = await sourceItem.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, null, null);

                    do
                    {
                        foreach (var sourceEntity in sourceResponse.Results.Cast <CloudBlockBlob>())
                        {
                            _logger.LogInformation($"Processing {sourceEntity.GetType().Name} '{sourceEntity.Name}'.");

                            var targetName   = ApplyOverrides(sourceEntity);
                            var targetClient = target.CreateBlobClient(targetName.Split('/')[0]);
                            var targetItem   = targetClient.GetContainerReference(sourceItem.Name);
                            await _policyCreate.ExecuteAsync(async() => await targetItem.CreateIfNotExistsAsync());

                            var targetEntity = targetItem.GetBlockBlobReference(targetName);

                            if (sourceEntity.Properties.Length > 0)
                            {
                                var tempFile = Path.GetTempFileName();

                                await _policyDownload.ExecuteAsync(async() =>
                                {
                                    if (File.Exists(tempFile))
                                    {
                                        File.Delete(tempFile);
                                    }

                                    await sourceEntity.DownloadToFileAsync(tempFile, FileMode.CreateNew, null, null, null, new CommandProgress(_logger, sourceEntity.Properties.Length, $"Download: {sourceEntity.Name}."), token);
                                });

                                await _policyUpload.ExecuteAsync(async() =>
                                {
                                    await targetEntity.UploadFromFileAsync(tempFile, null, null, null, new CommandProgress(_logger, sourceEntity.Properties.Length, $"Upload: {targetEntity.Name}."), token);
                                });

                                File.Delete(tempFile);
                            }
                            else
                            {
                                await _policyUpload.ExecuteAsync(async() =>
                                {
                                    await targetEntity.UploadTextAsync(string.Empty);
                                });
                            }

                            await _policyUpload.ExecuteAsync(async() =>
                            {
                                targetEntity.Properties.ContentType = sourceEntity.Properties.ContentType;

                                await targetEntity.SetPropertiesAsync();
                            });
                        }

                        sourceResponse = await sourceItem.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, sourceResponse.ContinuationToken, null, null);
                    }while (sourceResponse.ContinuationToken != null && !token.IsCancellationRequested);
                }

                response = await sourceClient.ListContainersSegmentedAsync(response.ContinuationToken);
            }while (response.ContinuationToken != null && !token.IsCancellationRequested);
        }