/// <summary> /// Delete all containers that are older than the input threshold date. /// </summary> /// <param name="thresholdDateTimeUtc">The specified date threshold</param> /// <returns></returns> public async Task DeleteExpiredContainersAsync(DateTime thresholdDateTimeUtc) { IEnumerable <CloudBlobContainer> containers = await ListContainers(); var tasks = containers.Where(container => BlobStorageClientHelper.IsContainerExpired(container.Name, thresholdDateTimeUtc)).ToList().Select(container => container.DeleteIfExistsAsync()); await Task.WhenAll(tasks); }
/// <summary> /// Upload the stream into the blob storage using the specified key. /// </summary> /// <param name="key">The key to uniquely locate and access the blob</param> /// <param name="stream">The stream to be uploaded</param> /// <returns></returns> public async Task UploadStreamBlobAsync(string key, Stream stream) { BlobStorageClientHelper.ParseKey(key, out string containerNameSuffix, out string blobName); ICloudBlob cloudBlob = await GetCloudBlockBlobReferenceAsync(containerNameSuffix, blobName); await cloudBlob.UploadFromStreamAsync(stream); }
/// <summary> /// Create a blob storage access key based on the orchestrationInstance. /// This key will be used to save and load the stream message in external storage when it is too large. /// </summary> /// <param name="orchestrationInstance">The orchestration instance.</param> /// <param name="messageFireTime">The message fire time.</param> /// <returns>The created blob key.</returns> public string BuildMessageBlobKey(OrchestrationInstance orchestrationInstance, DateTime messageFireTime) { return(BlobStorageClientHelper.BuildMessageBlobKey( orchestrationInstance != null ? orchestrationInstance.InstanceId : "null", orchestrationInstance != null ? orchestrationInstance.ExecutionId : "null", messageFireTime)); }
async Task <ICloudBlob> GetCloudBlockBlobReferenceAsync(string containerNameSuffix, string blobName) { string containerName = BlobStorageClientHelper.BuildContainerName(this.containerNamePrefix, containerNameSuffix); var cloudBlobContainer = this.blobClient.GetContainerReference(containerName); await cloudBlobContainer.CreateIfNotExistsAsync(); return(cloudBlobContainer.GetBlockBlobReference(blobName)); }
/// <summary> /// Download the blob from the storage using key. /// </summary> /// <param name="key">The key to uniquely locate and access the blob</param> /// <returns>A downloaded stream</returns> public async Task <Stream> DownloadStreamAsync(string key) { BlobStorageClientHelper.ParseKey(key, out string containerNameSuffix, out string blobName); ICloudBlob cloudBlob = await GetCloudBlockBlobReferenceAsync(containerNameSuffix, blobName); Stream targetStream = new MemoryStream(); await cloudBlob.DownloadToStreamAsync(targetStream); targetStream.Position = 0; return(targetStream); }
/// <summary> /// Construct a blob storage client instance with hub name and cloud storage account /// </summary> /// <param name="hubName">The hub name</param> /// <param name="cloudStorageAccount">The Cloud Storage Account</param> public BlobStorageClient(string hubName, CloudStorageAccount cloudStorageAccount) { if (string.IsNullOrWhiteSpace(hubName)) { throw new ArgumentException("Invalid hub name", nameof(hubName)); } if (cloudStorageAccount == null) { throw new ArgumentException("Invalid cloud storage acount", nameof(cloudStorageAccount)); } this.blobClient = CreateBlobClient(cloudStorageAccount); // make the hub name lower case since it will be used as part of the prefix of the container name, // which only allows lower case letters this.containerNamePrefix = BlobStorageClientHelper.BuildContainerNamePrefix(hubName.ToLower()); }
/// <summary> /// Construct a blob storage client instance with hub name and connection string /// </summary> /// <param name="hubName">The hub name</param> /// <param name="connectionString">The connection string</param> public BlobStorageClient(string hubName, string connectionString) { if (string.IsNullOrWhiteSpace(connectionString)) { throw new ArgumentException("Invalid connection string", nameof(connectionString)); } if (string.IsNullOrWhiteSpace(hubName)) { throw new ArgumentException("Invalid hub name", nameof(hubName)); } this.blobClient = CloudStorageAccount.Parse(connectionString).CreateCloudBlobClient(); this.blobClient.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(DeltaBackOff, MaxRetries); this.blobClient.DefaultRequestOptions.MaximumExecutionTime = MaximumExecutionTime; // make the hub name lower case since it will be used as part of the prefix of the container name, // which only allows lower case letters this.containerNamePrefix = BlobStorageClientHelper.BuildContainerNamePrefix(hubName.ToLower()); }
/// <summary> /// Create a blob storage access key based on message session. /// This key will be used to save and load the stream in external storage when it is too large. /// </summary> /// <param name="sessionId">The message session Id.</param> /// <returns>A blob key.</returns> public string BuildSessionBlobKey(string sessionId) { return(BlobStorageClientHelper.BuildSessionBlobKey(sessionId)); }