public async Task ZipIt([ActivityTrigger] BlobModel input, Binder binder) { SemaphoreSlim semaphore = GetSemaphoreForContainer(input.Analysis.Category); await semaphore.WaitAsync(); try { string name = $"{input.Analysis.Category}.zip"; using Stream zipStream = await binder.BindAsync <Stream>(FunctionUtils.GetBindingAttributes("zip-collection", name)); using var archive = new ZipArchive(zipStream, ZipArchiveMode.Create, true); BlobContainerClient containerClient = GetCloudBlobContainer(input.Analysis.Category); containerClient.DeleteBlobIfExists(name); Pageable <BlobItem> blobs = containerClient.GetBlobs(); foreach (BlobItem blob in blobs) { BlobClient client = containerClient.GetBlobClient(blob.Name); using var blobStream = new MemoryStream(); client.DownloadTo(blobStream); using Stream entryStream = archive.CreateEntry(blob.Name).Open(); blobStream.Seek(0, SeekOrigin.Begin); blobStream.CopyTo(entryStream); } } finally { semaphore.Release(); } }
/// <inheritdoc/> public async Task <bool> DeleteDataBlobs(Instance instance) { BlobContainerClient container = await CreateBlobClient(instance.Org); if (container == null) { _logger.LogError($"BlobSerivce // DeleteDataBlobs // Could not connect to blob container."); return(false); } try { await foreach (BlobItem item in container.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"{instance.AppId}/{instance.Id}", CancellationToken.None)) { container.DeleteBlobIfExists(item.Name, DeleteSnapshotsOption.IncludeSnapshots); } } catch (Exception e) { _sasTokenProvider.InvalidateSasToken(instance.Org); _logger.LogError(e, $"BlobSerivce // DeleteDataBlobs // Org: {instance.Org} // Exeption: {e.Message}"); return(false); } return(true); }
public async Task UploadFile(string filePath, string fileName) { _container.DeleteBlobIfExists(fileName); BlobClient blob = _container.GetBlobClient(fileName); await blob.UploadAsync(filePath); }
public async Task <bool> DeleteBlobsExceptSelected(List <string> guidList, string blobContainerReference) { try { //https://stackoverflow.com/questions/23485514/getting-list-of-names-of-azure-blob-files-in-a-container //https://stackoverflow.com/questions/36497399/how-to-delete-files-from-blob-container #region .net4.6 / .net core //var container = _cloudStorageClient.GetContainerReference(blobContainerReference); //var blobList = container.ListBlobs(); //List<string> blobNames = blobList.OfType<CloudBlockBlob>().Select(b => b.Name).ToList(); //foreach (var item in blobList.OfType<CloudBlockBlob>().ToList()) //{ // if (!guidList.Contains(item.Name)) // { // await item.DeleteIfExistsAsync(); // } //} #endregion #region .net standard //TODO: implement //var container = _cloudStorageClient.GetContainerReference(blobContainerReference); //var blobList = await container.ListBlobsSegmentedAsync(null); //List<string> blobNames = blobList.Results.OfType<CloudBlockBlob>().Select(b => b.Name).ToList(); //foreach (var item in blobList.Results.OfType<CloudBlockBlob>().ToList()) //{ // if (!guidList.Contains(item.Name)) // { // await item.DeleteIfExistsAsync(); // } //} var blobList = _blobContainerClient.GetBlobsAsync(); await foreach (var item in blobList) { if (!guidList.Contains(item.Name)) { _blobContainerClient.DeleteBlobIfExists(item.Name, DeleteSnapshotsOption.IncludeSnapshots); } } #endregion return(true); } catch (Exception ex) { return(false); } }
public static void Run([TimerTrigger("0 0 */ * *")] TimerInfo myTimer, ILogger log) {//"0 0 */ * *" List <BlobInfo> lst = new List <BlobInfo>(); BlobInfo item; try { using (SqlConnection conn = new SqlConnection(Environment.GetEnvironmentVariable("SQLDB"))) { using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = conn; cmd.CommandText = "sp_CodeCdt_GetBlobDeleteList"; cmd.CommandType = CommandType.StoredProcedure; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { item = new BlobInfo(); item.id = dr.GetInt32(0); item.BlobFileName = dr.GetString(1); item.CreatedDate = dr.GetDateTime(2); lst.Add(item); } } } //if(lst.Count > 0) // latestBlobCreatedDate = lst.Max(x => x.CreatedDate); BlobServiceClient blobServiceClient = new BlobServiceClient(Environment.GetEnvironmentVariable("AzureWebJobsStorage")); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("eportal-codecdt"); using (SqlConnection conn = new SqlConnection(Environment.GetEnvironmentVariable("SQLDB"))) { using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = conn; cmd.CommandText = "sp_CodeCdt_ClearBlobDeleteList"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@id", SqlDbType.Int); conn.Open(); foreach (BlobInfo s in lst) { cmd.Parameters[0].Value = s.id; containerClient.DeleteBlobIfExists("CodeCdtAttachments/" + s.BlobFileName, DeleteSnapshotsOption.IncludeSnapshots); cmd.ExecuteNonQuery(); } } } } catch (Exception ex) { log.LogError(ex.Message); } }
public void deleteFromStorage(JobApplication application) { string fileName = "cv" + application.FirstName.ToString() + application.LastName.ToString() + application.JobOfferId.ToString(); string connectionString = _configuration["AzureBlob"]; BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); var section = _configuration.GetSection("Azure"); string containerName = section.GetValue <string>("ContainerName"); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName); containerClient.DeleteBlobIfExists(fileName); }
public void DeleteBlobs(List <string> blobsToDelete) { if (blobsToDelete == null || blobsToDelete.Count == 0) { return; } foreach (string blobName in blobsToDelete) { _client.DeleteBlobIfExists(blobName, cancellationToken: _cancellationToken); } }
public static void CreateSampleBlobFile() { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.development.json", optional: false, reloadOnChange: true) .Build(); BlobContainerClient containerClient = new BlobContainerClient(configuration.GetSection("AzureBlobConfiguration").GetSection("ConnectionString").Value, "contractevents"); containerClient.CreateIfNotExists(); UTF8Encoding encoding = new UTF8Encoding(); using MemoryStream memoryStream = new MemoryStream(encoding.GetBytes(BlobSampleContent)); containerClient.DeleteBlobIfExists(BlobName); containerClient.UploadBlob(BlobName, memoryStream); }
/// <summary> /// /// <para>DeleteFile:</para> /// /// <para>Deletes a file from File Service, caller thread will be blocked before it is done</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.DeleteFile"/> for detailed documentation</para> /// /// </summary> public bool DeleteFile(string _BucketName, string _KeyInBucket, Action <string> _ErrorMessageAction = null) { try { BlobContainerClient ContainerClient = AServiceClient.GetBlobContainerClient(_BucketName); ContainerClient.DeleteBlobIfExists(_KeyInBucket); return(true); } catch (Exception ex) { _ErrorMessageAction?.Invoke($"BFileServiceAZ -> DeleteFile : {ex.Message}\n{ex.StackTrace}"); return(false); } }
private void CreateSampleBlobFile() { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.development.json", optional: false, reloadOnChange: true) .Build(); var sampleBlobFileContent = "<contract xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='urn:sfa:schemas:contract'></contract>"; BlobContainerClient containerClient = new BlobContainerClient(configuration.GetSection("AzureBlobConfiguration").GetSection("ConnectionString").Value, "contractevents"); containerClient.CreateIfNotExists(); UTF8Encoding encoding = new UTF8Encoding(); using MemoryStream memoryStream = new MemoryStream(encoding.GetBytes(sampleBlobFileContent)); containerClient.DeleteBlobIfExists(_blobName); containerClient.UploadBlob(_blobName, memoryStream); }
/// <inheritdoc/> public async Task <bool> DeleteDataBackup(string instanceGuid) { BlobContainerClient container = await CreateBackupBlobClient(); try { await foreach (BlobItem item in container.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"dataElements/{instanceGuid}", CancellationToken.None)) { container.DeleteBlobIfExists(item.Name, DeleteSnapshotsOption.IncludeSnapshots); } } catch (Exception e) { _logger.LogError(e, "BackupBlobService // DeleteInstanceEventsBackup // Instance: {InstanceGuid} // Exeption: {Exception}", instanceGuid, e); return(false); } return(true); }
/// <summary> /// /// <para>DeleteFolder:</para> /// /// <para>Deletes a folder from File Service, caller thread will be blocked before it is done</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.DeleteFile"/> for detailed documentation</para> /// /// </summary> public bool DeleteFolder(string _BucketName, string _Folder, Action <string> _ErrorMessageAction = null) { try { BlobContainerClient ContainerClient = AServiceClient.GetBlobContainerClient(_BucketName); Pageable <BlobHierarchyItem> BlobsResponse = ContainerClient.GetBlobsByHierarchy(BlobTraits.None, BlobStates.None, null, _Folder); foreach (BlobHierarchyItem Item in BlobsResponse) { if (Item.IsBlob) { ContainerClient.DeleteBlobIfExists(Item.Blob.Name); } } return(true); } catch (Exception ex) { _ErrorMessageAction?.Invoke($"BFileServiceAZ -> DeleteFolder : {ex.Message}\n{ex.StackTrace}"); return(false); } }
public void DeleteFile(string blobName) { var container = new BlobContainerClient(Config.ConnectionString, Config.ContainerName); container.DeleteBlobIfExists(blobName); }
public void DeleteFile(string name) { containerClient.DeleteBlobIfExists(name); }