public async Task SaveFileAsync(string folderName, string fileName, string contentType, Stream file, bool overwrite = true) { if (contentType == null) { throw new ArgumentNullException(nameof(contentType)); } ICloudBlobContainer container = await GetContainerAsync(folderName); var blob = container.GetBlobReference(fileName); try { await blob.UploadFromStreamAsync(file, overwrite); } catch (StorageException ex) when(ex.IsFileAlreadyExistsException()) { throw new FileAlreadyExistsException( string.Format( CultureInfo.CurrentCulture, "There is already a blob with name {0} in container {1}.", fileName, folderName), ex); } blob.Properties.ContentType = contentType; blob.Properties.CacheControl = GetCacheControl(folderName); await blob.SetPropertiesAsync(); }
public async Task <IFileReference> GetFileReferenceAsync(string folderName, string fileName, string ifNoneMatch = null) { if (String.IsNullOrWhiteSpace(folderName)) { throw new ArgumentNullException(nameof(folderName)); } if (String.IsNullOrWhiteSpace(fileName)) { throw new ArgumentNullException(nameof(fileName)); } ICloudBlobContainer container = await GetContainerAsync(folderName); var blob = container.GetBlobReference(fileName); var result = await GetBlobContentAsync(folderName, fileName, ifNoneMatch); if (result.StatusCode == HttpStatusCode.NotModified) { return(CloudFileReference.NotModified(ifNoneMatch)); } else if (result.StatusCode == HttpStatusCode.OK) { if (await blob.ExistsAsync()) { await blob.FetchAttributesAsync(); } return(CloudFileReference.Modified(blob, result.Data)); } else { // Not found return(null); } }
public async Task SaveFileAsync(string folderName, string fileName, Stream file, IAccessCondition accessConditions) { ICloudBlobContainer container = await GetContainerAsync(folderName); var blob = container.GetBlobReference(fileName); accessConditions = accessConditions ?? AccessConditionWrapper.GenerateIfNotExistsCondition(); var mappedAccessCondition = new AccessCondition { IfNoneMatchETag = accessConditions.IfNoneMatchETag, IfMatchETag = accessConditions.IfMatchETag, }; try { await blob.UploadFromStreamAsync(file, mappedAccessCondition); } catch (StorageException ex) when(ex.IsFileAlreadyExistsException()) { throw new FileAlreadyExistsException( String.Format( CultureInfo.CurrentCulture, "There is already a blob with name {0} in container {1}.", fileName, folderName), ex); } blob.Properties.ContentType = GetContentType(folderName); await blob.SetPropertiesAsync(); }
public async Task DeleteFileAsync(string folderName, string fileName) { ICloudBlobContainer container = await GetContainerAsync(folderName); var blob = container.GetBlobReference(fileName); await blob.DeleteIfExistsAsync(); }
public async Task <Uri> GetFileReadUriAsync(string folderName, string fileName, DateTimeOffset?endOfAccess) { folderName = folderName ?? throw new ArgumentNullException(nameof(folderName)); fileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); if (endOfAccess.HasValue && endOfAccess < DateTimeOffset.UtcNow) { throw new ArgumentOutOfRangeException(nameof(endOfAccess), $"{nameof(endOfAccess)} is in the past"); } bool isPublicFolder = IsPublicContainer(folderName); if (!isPublicFolder && endOfAccess == null) { throw new ArgumentNullException(nameof(endOfAccess), $"{nameof(endOfAccess)} must not be null for non-public containers"); } ICloudBlobContainer container = await GetContainerAsync(folderName); var blob = container.GetBlobReference(fileName); if (isPublicFolder) { return(blob.Uri); } return(new Uri(blob.Uri, blob.GetSharedReadSignature(endOfAccess))); }
public async Task <bool> FileExistsAsync(string folderName, string fileName) { ICloudBlobContainer container = await GetContainerAsync(folderName); var blob = container.GetBlobReference(fileName); return(await blob.ExistsAsync()); }
private async Task <ISimpleCloudBlob> GetBlobForUriAsync(string folderName, string fileName) { folderName = folderName ?? throw new ArgumentNullException(nameof(folderName)); fileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); ICloudBlobContainer container = await GetContainerAsync(folderName); return(container.GetBlobReference(fileName)); }
public void OnlyPackageAuditRecordsWillBeSaved(AuditRecord record, bool expectedResult) { // Arrange ICloudBlobContainer nullBlobContainer = null; var service = new CloudAuditingService(() => nullBlobContainer, AuditActor.GetCurrentMachineActorAsync); // Act + Assert Assert.Equal <bool>(expectedResult, service.RecordWillBePersisted(record)); }
public AzureHttpMessageStore(ICloudTable table, ICloudBlobContainer blobContainer, bool useCompression) { Guard.ArgumentNotNull(table, "table"); Guard.ArgumentNotNull(blobContainer, "blobContainer"); _table = table; _blobContainer = blobContainer; _useCompression = useCompression; }
public SharedAccessSignatureServiceCloudBlob(ICloudBlobContainer container, ISharedAccessSignatureServiceClient sasService) : base(container) { if (sasService == null) { throw new ArgumentNullException("sasService", "The Shared Access Signature service client cannot be null."); } this.sasService = sasService; }
public async Task <ActionResult> CreateDownloadFileActionResultAsync(Uri requestUrl, string folderName, string fileName) { ICloudBlobContainer container = await GetContainer(folderName); var blob = container.GetBlobReference(fileName); var redirectUri = GetRedirectUri(requestUrl, blob.Uri); return(new RedirectResult(redirectUri.OriginalString, false)); }
//string connection, string container public AzureFileStorage(ICloudBlobContainer container, Func <T, byte[]> getBytes) { _container = container; _container.CreateIfNotExistsAsync(); _getBytes = getBytes; if (!CheckValid()) { throw new ArgumentException($"Type {typeof(T)} not valid "); } }
public async Task SaveFileAsync(string folderName, string fileName, Stream packageFile) { ICloudBlobContainer container = await GetContainer(folderName); var blob = container.GetBlobReference(fileName); await blob.DeleteIfExistsAsync(); await blob.UploadFromStreamAsync(packageFile); blob.Properties.ContentType = GetContentType(folderName); await blob.SetPropertiesAsync(); }
public async Task <Stream> GetFileAsync(string folderName, string fileName) { if (String.IsNullOrWhiteSpace(folderName)) { throw new ArgumentNullException("folderName"); } if (String.IsNullOrWhiteSpace(fileName)) { throw new ArgumentNullException("fileName"); } ICloudBlobContainer container = await GetContainer(folderName); var blob = container.GetBlobReference(fileName); var stream = new MemoryStream(); try { await blob.DownloadToStreamAsync(stream); } catch (StorageException ex) { stream.Dispose(); if (ex.RequestInformation.ExtendedErrorInformation.ErrorCode == BlobErrorCodeStrings.BlobNotFound) { return(null); } throw; } catch (TestableStorageClientException ex) { // This is for unit test only, because we can't construct an // StorageException object with the required ErrorCode stream.Dispose(); if (ex.ErrorCode == BlobErrorCodeStrings.BlobNotFound) { return(null); } throw; } stream.Position = 0; return(stream); }
private async Task <StorageResult> GetBlobContentAsync(string folderName, string fileName, string ifNoneMatch = null) { ICloudBlobContainer container = await GetContainerAsync(folderName); var blob = container.GetBlobReference(fileName); var stream = new MemoryStream(); try { await blob.DownloadToStreamAsync( stream, accessCondition : ifNoneMatch == null? null : AccessCondition.GenerateIfNoneMatchCondition(ifNoneMatch)); } catch (StorageException ex) { stream.Dispose(); if (ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.NotModified) { return(new StorageResult(HttpStatusCode.NotModified, null)); } else if (ex.RequestInformation.ExtendedErrorInformation?.ErrorCode == BlobErrorCodeStrings.BlobNotFound) { return(new StorageResult(HttpStatusCode.NotFound, null)); } throw; } catch (TestableStorageClientException ex) { // This is for unit test only, because we can't construct an // StorageException object with the required ErrorCode stream.Dispose(); if (ex.ErrorCode == BlobErrorCodeStrings.BlobNotFound) { return(new StorageResult(HttpStatusCode.NotFound, null)); } throw; } stream.Position = 0; return(new StorageResult(HttpStatusCode.OK, stream)); }
public EventStreamConsumingSession( string streamName, EventStreamReaderId consumerId, TimeSpan leaseTimeout, ICloudBlobContainer blobContainer) { Require.NotEmpty(streamName, "streamName"); Require.NotNull(consumerId, "consumerId"); Require.NotNull(blobContainer, "blobContainer"); m_streamName = streamName; m_consumerId = consumerId; m_leaseTimeout = leaseTimeout; m_blobContainer = blobContainer; }
public EventStreamConsumingSession( string streamName, string consumerId, TimeSpan leaseTimeout, ICloudBlobContainer blobContainer) { Require.NotEmpty(streamName, "streamName"); Require.NotEmpty(consumerId, "consumerId"); Require.NotNull(blobContainer, "blobContainer"); m_streamName = streamName; m_consumerId = consumerId; m_leaseTimeout = leaseTimeout; m_blobContainer = blobContainer; }
private async Task CopyUrlAsync(ICloudBlobContainer container, string oldBaseUrl, string newBaseUrl, string oldUrl, bool gzip) { await Throttle.WaitAsync(); try { _logger.LogInformation("Copying {OldUrl}...", oldUrl); var result = await _simpleHttpClient.DeserializeUrlAsync <JToken>(oldUrl); var json = result.GetResultOrThrow(); var fixedJson = FixUrls(oldBaseUrl, newBaseUrl, json); if (!TryGetPath(oldBaseUrl, oldUrl, out var path)) { throw new InvalidOperationException("The URL does not start with the base URL."); } var blob = container.GetBlobReference(path); blob.Properties.ContentType = "application/json"; var jsonString = fixedJson.ToString(Formatting.None); var bytes = Encoding.UTF8.GetBytes(jsonString); if (gzip) { blob.Properties.ContentEncoding = "gzip"; using (var compressedStream = new MemoryStream()) { using (var gzipStream = new GZipStream(compressedStream, CompressionLevel.Optimal, leaveOpen: true)) { gzipStream.Write(bytes, 0, bytes.Length); } bytes = compressedStream.ToArray(); } } using (var memoryStream = new MemoryStream(bytes)) { await blob.UploadFromStreamAsync(memoryStream, overwrite : true); } } finally { Throttle.Release(); } }
protected CloudBlobBase(Uri uri, string name, ICloudBlobContainer container) { if (uri == null) throw new ArgumentNullException("uri", "The blob uri cannot be null."); if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("The blob name cannot be null, empty or white space.", "name"); if (container == null) throw new ArgumentNullException("container", "The container cannot be null."); this.uri = uri; this.Name = name; this.Container = container; this.Metadata = new Dictionary<string, string>(); this.Properties = new BlobProperties(); }
private void GetItemsCache( ICloudBlobContainer container, Dictionary <ThumbnailDefinition, ThumbnailSetItemData> data, IEnumerable <ThumbnailDefinition> items) { if (items == null) { return; } foreach (var item in items) { var blockBlob = container.GetBlockBlobReference(item.OutputBlobName); data.Add(item, new ThumbnailSetItemData(blockBlob)); this.GetItemsCache(container, data, item.Children); } }
private async Task <ISimpleCloudBlob> GetBlobForUriAsync(string folderName, string fileName, DateTimeOffset?endOfAccess) { folderName = folderName ?? throw new ArgumentNullException(nameof(folderName)); fileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); if (endOfAccess.HasValue && endOfAccess < DateTimeOffset.UtcNow) { throw new ArgumentOutOfRangeException(nameof(endOfAccess), $"{nameof(endOfAccess)} is in the past"); } if (!IsPublicContainer(folderName) && endOfAccess == null) { throw new ArgumentNullException(nameof(endOfAccess), $"{nameof(endOfAccess)} must not be null for non-public containers"); } ICloudBlobContainer container = await GetContainerAsync(folderName); return(container.GetBlobReference(fileName)); }
public async Task SaveFileAsync(string folderName, string fileName, Stream packageFile, bool overwrite = true) { ICloudBlobContainer container = await GetContainer(folderName); var blob = container.GetBlobReference(fileName); if (overwrite) { await blob.DeleteIfExistsAsync(); } else if (await blob.ExistsAsync()) { throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, "There is already a blob with name {0} in container {1}.", fileName, folderName)); } await blob.UploadFromStreamAsync(packageFile); blob.Properties.ContentType = GetContentType(folderName); await blob.SetPropertiesAsync(); }
public void CloudAuditServiceObfuscateAuditRecord() { // Arrange ICloudBlobContainer nullBlobContainer = null; var service = new CloudAuditingService(() => nullBlobContainer, AuditActor.GetCurrentMachineActorAsync); AuditActor onBehalfOf = new AuditActor("machineName", "3.3.3.3", "userName1", "NoAuthentication", "someKey", DateTime.Now, null); AuditActor auditActor = new AuditActor("machineName", "2.2.2.2", "userName1", "NoAuthentication", "someKey", DateTime.Now, onBehalfOf); Package p = new Package() { User = new User("userName"), UserKey = 1, PackageRegistration = new PackageRegistration() { Id = "regId" } }; PackageAuditRecord packageAuditRecord = new PackageAuditRecord(p, AuditedPackageAction.Create); // Act var auditEntry = service.RenderAuditEntry(new AuditEntry(packageAuditRecord, auditActor)); // Assert var entry = (JObject)JsonConvert.DeserializeObject(auditEntry); var record = entry["Record"]; var actor = entry["Actor"]; Assert.Equal("-1", record["PackageRecord"]["UserKey"].ToString()); Assert.Equal(string.Empty, record["PackageRecord"]["FlattenedAuthors"].ToString()); Assert.Equal("ObfuscatedUserName", actor["UserName"].ToString()); Assert.Equal("2.2.2.0", actor["MachineIP"].ToString()); Assert.Equal("ObfuscatedUserName", actor["OnBehalfOf"]["UserName"].ToString()); Assert.Equal("3.3.3.0", actor["OnBehalfOf"]["MachineIP"].ToString()); }
public async Task SaveFileAsync(string folderName, string fileName, Stream packageFile, bool overwrite = true) { ICloudBlobContainer container = await GetContainerAsync(folderName); var blob = container.GetBlobReference(fileName); try { await blob.UploadFromStreamAsync(packageFile, overwrite); } catch (StorageException ex) when(ex.RequestInformation?.HttpStatusCode == (int?)HttpStatusCode.Conflict) { throw new FileAlreadyExistsException( String.Format( CultureInfo.CurrentCulture, "There is already a blob with name {0} in container {1}.", fileName, folderName), ex); } blob.Properties.ContentType = GetContentType(folderName); await blob.SetPropertiesAsync(); }
private static ICloudBlockBlob GetCloudBlockBlob(ICloudBlobContainer cloudBlobContainer, Guid uuid) { var fileName = $"{uuid}.csv"; return(cloudBlobContainer.GetBlockBlobReference(fileName)); }
public CloudBlobBase(ICloudBlobContainer container) { this.Container = container; this.Metadata = new WebHeaderCollection(); }
public async Task CopyAsync(ICloudBlobContainer container, string oldBaseUrl, string newBaseUrl, string id, bool gzip) { var normalizedOldBaseUrl = oldBaseUrl.TrimEnd('/'); var normalizedNewBaseUrl = newBaseUrl.TrimEnd('/'); var indexUrl = RegistrationUrlBuilder.GetIndexUrl(normalizedOldBaseUrl, id); _logger.LogInformation("Downloading index {IndexUrl}...", indexUrl); await Throttle.WaitAsync(); RegistrationIndex index; try { index = await _registrationClient.GetIndexOrNullAsync(indexUrl); } finally { Throttle.Release(); } if (index == null) { return; } var pageUrls = new List <string>(); var itemUrls = new List <string>(); var pageTasks = index .Items .Select(async pageItem => { await Task.Yield(); await Throttle.WaitAsync(); try { List <RegistrationLeafItem> leafItems; if (pageItem.Items != null) { leafItems = pageItem.Items; } else { pageUrls.Add(pageItem.Url); _logger.LogInformation("Downloading page {PageUrl}...", pageItem.Url); var page = await _registrationClient.GetPageAsync(pageItem.Url); leafItems = page.Items; } foreach (var leafItem in leafItems) { itemUrls.Add(leafItem.Url); } } finally { Throttle.Release(); } }) .ToList(); await Task.WhenAll(pageTasks); var copyTasks = itemUrls .Concat(pageUrls) .Concat(new[] { indexUrl }) .Select(async url => { await Task.Yield(); await CopyUrlAsync(container, normalizedOldBaseUrl, normalizedNewBaseUrl, url, gzip); }) .ToList(); await Task.WhenAll(copyTasks); }
public EventStreamConsumingSessionFactory(ICloudBlobContainer sessionsBlob) { Require.NotNull(sessionsBlob, "sessionsBlob"); m_sessionsBlob = sessionsBlob; }
private ISimpleCloudBlob GetBlobReference(ICloudBlobContainer container, string path) { var blob = container.GetBlobReference(Uri.UnescapeDataString(path)); return(blob); }
public StorageAccountCloudBlob(ICloudBlobContainer container, IStorageCredentials credentials) : base(container) { this.Credentials = credentials; }