Exemplo n.º 1
0
        //------------------------------- Shared ------------------------------
        /// <summary>
        /// Deletes file and thumbnail from azure storage
        /// </summary>
        /// <param name="fileName"> file to delete </param>
        /// <returns> no return </returns>
        public async Task DeleteBlobImage(string fileName)
        {
            BlobContainerClient container = new BlobContainerClient(Configuration["ImageBlob"], "images");
            BlobClient          blob      = container.GetBlobClient(fileName);
            await blob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, default);

            blob = container.GetBlobClient($"{fileName}_thumb");
            await blob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, default);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Permamently delete job application and all connected resources.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <IActionResult> Delete(int?id)
        {
            if (!id.HasValue)
            {
                return(BadRequest("id cannot be null"));
            }

            var jobApplication = await _context.JobApplications.FirstOrDefaultAsync(x => x.Id == id);

            string connectionString = _config.GetValue <string>("AzureBlob:ConnectionString");

            // Get a reference to a container
            BlobContainerClient container = new BlobContainerClient(connectionString, "applications");

            // Get a reference to a blob
            BlobClient blob = container.GetBlobClient(jobApplication.CvUrl);

            // Remove from AzureBlob
            _ = blob.DeleteIfExistsAsync();

            // Remove from database
            _context.Remove(jobApplication);
            await _context.SaveChangesAsync();

            return(RedirectToAction("Index", "JobApplication", new { Area = "User" }));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> DeleteFile(int id)
        {
            var fileDetails = await _context.Files.FindAsync(id);

            if (fileDetails == null)
            {
                return(NotFound());
            }

            _context.Files.Remove(fileDetails);
            await _context.SaveChangesAsync();

            try
            {
                string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");

                BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
                string            containerName     = "files";

                BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

                BlobClient blobClient = containerClient.GetBlobClient(fileDetails.Uri.Substring(fileDetails.Uri.LastIndexOf("/") + 1));

                await blobClient.DeleteIfExistsAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                return(StatusCode(500, $"Internal sserver error: {ex}"));
            }
        }
        public async Task <bool> DeleteBlobIfExistsAsync(string containerName, string blobName)
        {
            var blobClient = new BlobClient(this.ConnectionString, containerName, blobName);
            var deleteInfo = await blobClient.DeleteIfExistsAsync();

            return(deleteInfo.Value);
        }
Exemplo n.º 5
0
        //TODO : Change this for filestream version
        public async Task <PackageUploadResult> UploadPackageAsync(string packageName, Stream fileStream, bool deleteIfExists = false)
        {
            string connectionString = ConnectionString;

            BlobContainerClient container = new BlobContainerClient(connectionString, "packagecontainer");
            await container.CreateIfNotExistsAsync();

            string blobName = packageName + ".zip";

            BlobClient blob = container.GetBlobClient(blobName);

            if (!deleteIfExists && await blob.ExistsAsync())
            {
                return(new PackageUploadResult
                {
                    IsSuccess = false,
                    ErrorMessage = $"Package name, {packageName} is already taken.",
                });
            }

            if (deleteIfExists)
            {
                await blob.DeleteIfExistsAsync();
            }

            await blob.UploadAsync(fileStream);

            return(new PackageUploadResult
            {
                IsSuccess = true,
                ErrorMessage = "",
            });
        }
Exemplo n.º 6
0
        protected virtual async Task DeleteAsync(string fullPath, CancellationToken cancellationToken)
        {
            (BlobContainerClient container, string path) = await GetPartsAsync(fullPath, false).ConfigureAwait(false);

            if (StoragePath.IsRootPath(path))
            {
                //deleting the entire container / filesystem
                await container.DeleteIfExistsAsync(cancellationToken : cancellationToken).ConfigureAwait(false);
            }
            else
            {
                BlockBlobClient blob = string.IsNullOrEmpty(path)
               ? null
               : container.GetBlockBlobClient(StoragePath.Normalize(path));
                if (blob != null)
                {
                    try
                    {
                        await blob.DeleteAsync(
                            DeleteSnapshotsOption.IncludeSnapshots, cancellationToken : cancellationToken).ConfigureAwait(false);
                    }
                    catch (RequestFailedException ex) when(ex.ErrorCode == "BlobNotFound")
                    {
                        //this might be a folder reference, just try it

                        await foreach (BlobItem recursedFile in
                                       container.GetBlobsAsync(prefix: path, cancellationToken: cancellationToken).ConfigureAwait(false))
                        {
                            BlobClient client = container.GetBlobClient(recursedFile.Name);
                            await client.DeleteIfExistsAsync(cancellationToken : cancellationToken).ConfigureAwait(false);
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        public async Task <bool> DeletePhotoAsync(string fileName)
        {
            var client = new BlobClient(_settings.ConnectionString, _settings.ContainerName, fileName);
            var result = await client.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots);

            return(result.Value);
        }
Exemplo n.º 8
0
 public static async Task DeleteBlobAsync(string connectionString,
                                          string containerName,
                                          string name)
 {
     var blob = new BlobClient(connectionString, containerName, name);
     await blob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots);
 }
Exemplo n.º 9
0
        public async Task <bool> DeleteFileFromStorageContainer(string path)
        {
            BlobContainerClient containerClient = GetFileShareContainer();
            BlobClient          blobClient      = containerClient.GetBlobClient(path);

            return(await blobClient.DeleteIfExistsAsync());
        }
Exemplo n.º 10
0
        public async Task GlobalTearDown()
        {
            if (Mode != RecordedTestMode.Playback)
            {
                var configuration   = TestConfigurations.DefaultTargetOAuthTenant;
                var containerClient = new BlobServiceClient(
                    new Uri(Tenants.TestConfigOAuth.BlobServiceEndpoint),
                    new StorageSharedKeyCredential(Tenants.TestConfigOAuth.AccountName,
                                                   Tenants.TestConfigOAuth.AccountKey))
                                      .GetBlobContainerClient(_containerName);
                await foreach (BlobItem blobItem in containerClient.GetBlobsAsync(BlobTraits.ImmutabilityPolicy | BlobTraits.LegalHold))
                {
                    BlobClient blobClient = containerClient.GetBlobClient(blobItem.Name);

                    if (blobItem.Properties.HasLegalHold)
                    {
                        await blobClient.SetLegalHoldAsync(false);
                    }

                    if (blobItem.Properties.ImmutabilityPolicy.ExpiresOn != null)
                    {
                        await blobClient.DeleteImmutabilityPolicyAsync();
                    }

                    await blobClient.DeleteIfExistsAsync();
                }

                await _storageManagementClient.BlobContainers.DeleteAsync(
                    resourceGroupName : configuration.ResourceGroupName,
                    accountName : configuration.AccountName,
                    containerName : _containerName);
            }
        }
Exemplo n.º 11
0
        private async Task <bool> DeleteIfExistsAsync(string org, string fileName)
        {
            BlobClient blockBlob = await CreateBlobClient(org, fileName);

            bool result = await blockBlob.DeleteIfExistsAsync();

            return(result);
        }
        public Task DeleteFile(string fileName)
        {
            NLogManager.LogInfo("Delete " + fileName);
            ReNameFile(fileName);
            BlobClient blob = _blobContainer.GetBlobClient(fileName);

            return(blob.DeleteIfExistsAsync());
        }
Exemplo n.º 13
0
        public async Task <bool> DeleteFile(string blobName, bool isPublic = false)
        {
            BlobContainerClient container  = this.GetContainer(isPublic);
            BlobClient          blobClient = container.GetBlobClient(blobName);
            await blobClient.DeleteIfExistsAsync();

            return(true);
        }
Exemplo n.º 14
0
        public async Task <IActionResult> DeleteItemAsync(string id)
        {
            _logger.LogInformation("Delete content for id {id}", id);

            string tableName               = _appSettings.TableName;
            string tableConnectionString   = _appSettings.TableConnectionString;
            string containerName           = _appSettings.MediaStorageContainer;
            string storageConnectionString = _appSettings.MediaStorageConnectionString;


            // Initialize table client
            CloudStorageAccount storageAccount;

            storageAccount = CloudStorageAccount.Parse(tableConnectionString);
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());
            CloudTable       table       = tableClient.GetTableReference(tableName);

            // Initialize blob container client
            BlobContainerClient blobContainerClient = new BlobContainerClient(storageConnectionString, containerName);

            string         rowKey            = id;
            TableOperation retrieveOperation = TableOperation.Retrieve <ImageEntity>(TransferPartitionKey, rowKey);
            TableResult    result            = await table.ExecuteAsync(retrieveOperation);

            ImageEntity entity = result.Result as ImageEntity;

            if (entity == null)
            {
                _logger.LogWarning("Item id {id} not found", id);
                return(NotFound());
            }

            var        blobUriBuilder = new BlobUriBuilder(new Uri(entity.FileURL));
            BlobClient blobClient     = blobContainerClient.GetBlobClient(blobUriBuilder.BlobName);
            await blobClient.DeleteIfExistsAsync();

            blobUriBuilder = new BlobUriBuilder(new Uri(entity.ThumbnailURL));
            blobClient     = blobContainerClient.GetBlobClient(blobUriBuilder.BlobName);
            await blobClient.DeleteIfExistsAsync();

            TableOperation deleteOperation = TableOperation.Delete(entity);

            result = await table.ExecuteAsync(deleteOperation);

            return(NoContent());
        }
Exemplo n.º 15
0
        public async Task <bool> DeleteBlob(string blobRef)
        {
            BlobContainerClient container = await GetBlobContainerClient();

            BlobClient blob = container.GetBlobClient(blobRef);

            return(await blob.DeleteIfExistsAsync());
        }
        public async Task <bool> DeleteAsync(string name)
        {
            BlobClient client = this.containerClient.GetBlobClient(name);

            Response <bool> response = await client.DeleteIfExistsAsync();

            return(response.Value);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Delete blob by Uri
        /// </summary>
        /// <param name="id">The Blob Uri</param>
        /// <returns></returns>
        public async Task DeleteBlob(string id)
        {
            var blobClient    = new BlobClient(new Uri(id));
            var containerName = blobClient.BlobContainerName;
            var blobName      = blobClient.Name;

            blobClient = new BlobClient(_connectionString, containerName, blobName);
            await blobClient.DeleteIfExistsAsync();
        }
Exemplo n.º 18
0
        public async Task <bool> DeleteModelBlob(string fileName)
        {
            string              con             = _config["BlobConnection"];
            BlobServiceClient   blobService     = new BlobServiceClient(con);
            BlobContainerClient containerClient = blobService.GetBlobContainerClient("models");
            BlobClient          blobClient      = containerClient.GetBlobClient(fileName);

            return(await blobClient.DeleteIfExistsAsync());
        }
        public async Task <bool> DeleteAsync(string path, CancellationToken cancellationToken = default)
        {
            var container = GetContainerName(path);
            var blobPath  = GetPathWithoutContainer(path);
            var client    = new BlobClient(_connectionString, container, blobPath);
            var res       = await client.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, cancellationToken);

            return(res.Value);
        }
Exemplo n.º 20
0
        public async Task <bool> Handle(DeleteBlobCommandModel request, CancellationToken cancellationToken)
        {
            BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient(request.ContainerName.ToLower());

            BlobClient blobClient = containerClient.GetBlobClient(request.Name);

            Response <bool> isDeleted = await blobClient.DeleteIfExistsAsync(cancellationToken : cancellationToken);

            return(isDeleted);
        }
Exemplo n.º 21
0
        private static async Task <bool> DeleteIfExistsAsync(Uri blobUri, StorageSharedKeyCredential storageCredentials)
        {
            // Create the blob client.
            BlobClient blobClient = new BlobClient(blobUri, storageCredentials);

            // Delete the file
            var response = await blobClient.DeleteIfExistsAsync();

            return(await Task.FromResult(response.Value));
        }
Exemplo n.º 22
0
        public async Task <bool> Delete(string blobName)
        {
            BlobContainerClient container = new BlobContainerClient(settings.ConnectionString, settings.ContainerName);

            container.CreateIfNotExists(PublicAccessType.BlobContainer);
            BlobClient blobClient = container.GetBlobClient(blobName);
            await blobClient.DeleteIfExistsAsync();

            return(true);
        }
Exemplo n.º 23
0
        public async Task <bool> DeleteBlob(string connectionString, string containerName, string blobName)
        {
            BlobContainerClient container = new BlobContainerClient(connectionString, containerName);

            // Get a reference to a blob named "sample-file" in a container named "sample-container"
            BlobClient blob = container.GetBlobClient(blobName);

            // Upload local file
            return(await blob.DeleteIfExistsAsync());
        }
Exemplo n.º 24
0
        public static async Task UploadBlobAsync(string connectionString,
                                                 string containerName,
                                                 string fileName)
        {
            var file = new FileInfo(fileName);
            var blob = new BlobClient(connectionString, containerName, file.Name);

            await blob.DeleteIfExistsAsync();

            await blob.UploadAsync(fileName);
        }
        public static async Task DeleteBlobAsync(string connectionString,
                                                 string containerName,
                                                 string fileName)
        {
            CheckIsNotNullOrWhitespace(nameof(connectionString), connectionString);
            CheckIsNotNullOrWhitespace(nameof(containerName), containerName);
            CheckIsNotNullOrWhitespace(nameof(fileName), fileName);

            var blob = new BlobClient(connectionString, containerName, fileName);
            await blob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots);
        }
        private async Task ClearHostIdInfoAsync()
        {
            string     blobPath   = string.Format(HostIdValidator.BlobPathFormat, _testHostId);
            BlobClient blobClient = _blobContainerClient.GetBlobClient(blobPath);

            await blobClient.DeleteIfExistsAsync();

            await TestHelpers.Await(async() =>
            {
                bool exists = await blobClient.ExistsAsync();
                return(!exists);
            });
        }
        public async Task <bool> DeleteAsync(string fileName)
        {
            BlobClient blobClient = containerClient.GetBlobClient(fileName);

            if (blobClient == null)
            {
                return(false);
            }

            await blobClient.DeleteIfExistsAsync();

            return(true);
        }
        private async Task fDeleteFromBlobStorageAsync(string blobName)
        {
            var storageConn = _config.GetValue <string>("AppSettings:MyAzureStorageConnectionKey1");

            // Get a reference to a Container
            BlobContainerClient blobContainerClient = new BlobContainerClient(storageConn, BlobContainerNAME);

            // Delete the file
            BlobClient blobClient = blobContainerClient.GetBlobClient(blobName);
            await blobClient.DeleteIfExistsAsync();


            Console.WriteLine(blobName + ": file deleted successfully from azure blob storage...");
        }
Exemplo n.º 29
0
        public async Task <bool> DeleteFile(string containerName, string fileName)
        {
            // Create the container if not exists and return a container client object
            BlobContainerClient containerClient = new BlobContainerClient(_connectionString, containerName);
            await containerClient.CreateIfNotExistsAsync(PublicAccessType.Blob);

            // Get a reference to a blob
            BlobClient blobClient = containerClient.GetBlobClient(fileName);

            // Delete the blob
            var response = await blobClient.DeleteIfExistsAsync();

            return(response.Value);
        }
Exemplo n.º 30
0
        public async Task DeleteFileAsync(string containerName, string fileName)
        {
            BlobContainerClient containerClient;

            try {
                containerClient = await BlobServiceClient.CreateBlobContainerAsync(containerName);
            } catch {
                containerClient = BlobServiceClient.GetBlobContainerClient(containerName);
            }

            BlobClient blobClient = containerClient.GetBlobClient(fileName);

            await blobClient.DeleteIfExistsAsync();
        }