protected override async Task DeleteSingleAsync(string fullPath, CancellationToken cancellationToken)
        {
            try
            {
                await _client.DeleteObjectAsync(_bucketName, NormalisePath(fullPath), cancellationToken : cancellationToken).ConfigureAwait(false);
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
            {
                //when not found, just ignore

                //try delete everything recursively
                IReadOnlyCollection <Blob> childObjects = await ListAsync(new ListOptions { Recurse = true }, cancellationToken).ConfigureAwait(false);

                foreach (Blob blob in childObjects)
                {
                    try
                    {
                        await _client.DeleteObjectAsync(_bucketName, NormalisePath(blob.FullPath), cancellationToken : cancellationToken).ConfigureAwait(false);
                    }
                    catch (GoogleApiException exc) when(exc.HttpStatusCode == HttpStatusCode.NotFound)
                    {
                    }
                }
            }
        }
Beispiel #2
0
        public async Task DeleteByPrefixAsync(string prefix,
                                              CancellationToken ct = default)
        {
            var name = GetFileName(prefix, nameof(prefix));

            try
            {
                var items = storageClient.ListObjectsAsync(bucketName, name);

                await foreach (var item in items.WithCancellation(ct))
                {
                    try
                    {
                        await storageClient.DeleteObjectAsync(item.Bucket, item.Name, cancellationToken : ct);
                    }
                    catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
                    {
                        continue;
                    }
                }
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
            {
                return;
            }
        }
        public async Task <IUnixFileSystemEntry> MoveAsync(IUnixDirectoryEntry parent, IUnixFileSystemEntry source, IUnixDirectoryEntry target, string fileName, CancellationToken cancellationToken)
        {
            var targetDirEntry = (GcsDirectoryEntry)target;
            var targetName     = string.Join("/", new string[] { targetDirEntry.FullName.Trim('/'), fileName }).Trim('/');

            if (source is GcsFileEntry sourceFileEntry)
            {
                var sourceDirEntry = (GcsDirectoryEntry)parent;
                var sourceName     = string.Join("/", new string[] { sourceDirEntry.FullName.Trim('/'), sourceFileEntry.Name }).Trim('/');
                try
                {
                    var newObj = await _storageClient.CopyObjectAsync(_bucketName, sourceName,
                                                                      _bucketName, targetName,
                                                                      null, cancellationToken);

                    if (newObj != null)
                    {
                        await _storageClient.DeleteObjectAsync(_bucketName, sourceName, null, cancellationToken);
                    }
                }
                catch { }
            }
            else
            {
                throw new NotSupportedException();
            }

            return(new GcsDirectoryEntry(this)
            {
                Name = targetName
            });
        }
 public async Task DeleteAsync(string name)
 {
     try
     {
         await storageClient.DeleteObjectAsync(bucketName, name);
     }
     catch (GoogleApiException ex)
     {
         if (ex.HttpStatusCode != HttpStatusCode.NotFound)
         {
             throw;
         }
     }
 }
        public async Task DeleteOldBackupsAsync(DateTime removeThreshold)
        {
            var objects = _storageClient.ListObjectsAsync(_bucketName);


            var objectsToDelete = new List <Object>();
            await objects.ForEachAsync(obj =>
            {
                if (obj.TimeCreated.Value.ToUniversalTime() <= removeThreshold)
                {
                    objectsToDelete.Add(obj);
                }
            });

            if (objectsToDelete.Count == 0)
            {
                Console.WriteLine("Nothing to delete.");
            }

            foreach (var objToDelete in objectsToDelete)
            {
                Console.WriteLine($"Deleting {objToDelete.Name}, because it TimeCreated: {objToDelete.TimeCreated.Value.ToUniversalTime()} is older then threshold: {removeThreshold}");
                await _storageClient.DeleteObjectAsync(_bucketName, objToDelete.Name);
            }
        }
 public async Task DeleteFileAsync(string bucket, string objectName)
 {
     using (StorageClient client = await StorageClient.CreateAsync(_credential))
     {
         await client.DeleteObjectAsync(bucket, objectName);
     }
 }
Beispiel #7
0
        private static async Task DeleteObject(StorageClient client, string bucket, string name, string generation)
        {
            long?generationOption = generation == null ? default(long?) : long.Parse(generation);
            await client.DeleteObjectAsync(bucket, name, new DeleteObjectOptions { Generation = generationOption }, CancellationToken.None);

            Console.WriteLine($"Deleted object {bucket}/{name}");
        }
 /// <summary>
 /// Deletes the attachment with the given ID
 /// </summary>
 /// <param name="id">ID of the data object to delete</param>
 public async Task Delete(string id)
 {
     await RetryCatchAsync(id, async objectName =>
     {
         await _storageClient.DeleteObjectAsync(_options.BucketName, objectName);
         return(true);
     });
 }
 public async Task DeleteFile(string identificator)
 {
     var task = _storageClient.DeleteObjectAsync(
         bucket: _bucketName,
         objectName: identificator
         );
     await Task.WhenAny(task);
 }
Beispiel #10
0
 public ConfiguredTaskAwaitable DeleteObjectAsync(string fileName)
 {
     return(_client.DeleteObjectAsync(
                _bucketName,
                fileName,
                null,
                CancellationToken
                ).ConfigureAwait(false));
 }
Beispiel #11
0
        private static async Task DeleteDocumentObject(StorageClient storage, string bucket, string remoteFileName)
        {
            using (new Timing($"Delete {bucket}:{remoteFileName}"))
            {
                var document = await storage.GetObjectAsync(bucket, remoteFileName);

                await storage.DeleteObjectAsync(document);
            }
        }
Beispiel #12
0
 public Task DeleteObjectAsync(string fileName)
 {
     return(_client.DeleteObjectAsync(
                _bucketName,
                fileName,
                null,
                CancellationToken
                ));
 }
        public string DeleteFile(string fileurl)
        {
            Regex  search     = new Regex(@"[a-zA-Z0-9]{16}?");
            string objectName = search.Match(fileurl).ToString();

            storageClient.DeleteObjectAsync(bucketName, objectName);

            return(objectName);
        }
Beispiel #14
0
        /// <summary>
        /// Unregister a subscriber for a topic
        /// </summary>
        /// <param name="topic">Topic to unregister the subscriber for</param>
        /// <param name="subscriberAddress">Address of the subscriber</param>
        public async Task UnregisterSubscriber(string topic, string subscriberAddress)
        {
            // Create the name of the object to store the subscription under
            topic = $"{topic}/{subscriberAddress}";

            await RetryCatchAsync(topic, async objectName =>
            {
                await _storageClient.DeleteObjectAsync(_options.BucketName, objectName);
                return(true);
            });
        }
Beispiel #15
0
 public async Task DeleteBlobAsync(string containerName, string blobName)
 {
     try
     {
         await _client.DeleteObjectAsync(_bucket, ObjectName(containerName, blobName));
     }
     catch (GoogleApiException gae)
     {
         throw Error(gae);
     }
 }
Beispiel #16
0
 //delete image
 public async Task DeleteFileAsync(string fileNameForStorage)
 {
     try
     {
         await _storageClient.DeleteObjectAsync(_bucketName, fileNameForStorage);
     }
     catch
     {
         // ignored
     }
 }
Beispiel #17
0
 private async Task DeleteCoreAsync(string objectName)
 {
     try
     {
         await storageClient.DeleteObjectAsync(bucketName, objectName);
     }
     catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
     {
         return;
     }
 }
Beispiel #18
0
        public async Task <IActionResult> UploadVideo([FromForm] IFormFile file, [FromForm] string Title, [FromForm] string Username)
        {
            if (String.IsNullOrWhiteSpace(Title) || String.IsNullOrWhiteSpace(Username))
            {
                return(BadRequest());
            }

            Guid vidId = Guid.NewGuid();

            var safeLinkURL = Microsoft.AspNetCore.WebUtilities.WebEncoders.Base64UrlEncode(vidId.ToByteArray());
            var videoRecord = new Video
            {
                VideoId   = vidId,
                Title     = Title,
                Username  = Username,
                PostedAt  = DateTime.UtcNow,
                BucketURL = GenerateBucketURL(vidId, file.FileName),
                LinkURL   = safeLinkURL
            };

            try
            {
                using (var memoryStream = new MemoryStream())
                {
                    await file.CopyToAsync(memoryStream);

                    await _storageClient.UploadObjectAsync(_bucketName, videoRecord.BucketURL, null, memoryStream);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(StatusCode(500));
            }


            try
            {
                _videoDbContext.Videos.Add(videoRecord);
                _videoDbContext.SaveChanges();
            }catch (Exception e)
            {
                Console.WriteLine(e.ToString());

                //Video record saved to fail so we need to remove the video file in the GCP Storage so it's not left stranded
                await _storageClient.DeleteObjectAsync(_bucketName, videoRecord.BucketURL);

                return(StatusCode(500));
            }


            return(Ok(videoRecord));
        }
Beispiel #19
0
        private static async Task DeleteDocumentObjects(StorageClient storage, string bucket, string remotePrefix)
        {
            using (new Timing($"Delete All {bucket}:{remotePrefix}"))
            {
                var documents = await GetAllDocumentObjects(storage, bucket, remotePrefix);

                foreach (var doc in documents)
                {
                    await storage.DeleteObjectAsync(doc);
                }
            }
        }
        public async Task DeleteFileAsync(string imageUrl)
        {
            var filename = imageUrl.Substring(imageUrl.LastIndexOf("/") + 1);

            try
            {
                await _storageClient.DeleteObjectAsync(_bucketName, filename);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        public async Task DeleteAsync(string fileName)
        {
            Guard.NotNullOrEmpty(fileName);

            try
            {
                await storageClient.DeleteObjectAsync(bucketName, fileName);
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
            {
                return;
            }
        }
        public async Task <Result <string> > DeleteFileAsync(string fileName)
        {
            try
            {
                await storageClient.DeleteObjectAsync(googleCloudSettings.GoogleCloudStorageBucket, fileName);

                return(new SuccessResult <string>($"Deleted {fileName}"));
            }
            catch (Exception ex)
            {
                return(new UnexpectedResult <string>(ex.Message));
            }
        }
        public async Task <IActionResult> OnPostDeleteBucketObject(string FileToDelete)
        {
            var user = await _userManager.GetUserAsync(HttpContext.User);

            var TempDataList = TempData.GetKeep <InputModel>("FileList");
            var ToRemove     = TempDataList.FileList.SingleOrDefault(a => a.FileName == FileToDelete);

            _options.ObjectName = MakeFileName(FileToDelete, user.SenderId);
            try
            {
                IOrderedEnumerable <Google.Apis.Storage.v1.Data.Object> bucketItems = _storage.ListObjects(_options.BucketName).OrderBy(x => x.TimeCreated);
                if (bucketItems.Any(x => x.Name == _options.ObjectName))
                {
                    await _storage.DeleteObjectAsync(_options.BucketName, _options.ObjectName);
                }
                else
                {
                    _logger.LogError("File does not exist: " + _options.BucketName, _options.ObjectName + " : " + "Please contact help desk for further advice.", string.Empty, "");
                }

                FileUploadInfo fileToRemove = _context.FileUploadInfo.Where(x => x.FileName == FileToDelete || x.FileName == _options.ObjectName).LastOrDefault();
                if (fileToRemove != null)
                {
                    TempDataList.FileList.Remove(ToRemove);
                    _context.FileUploadInfo.Remove(fileToRemove);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    string errorMessage = "An error occured while removing the selected file " + FileToDelete + " , please contact help desk for further help";
                    ModelState.AddModelError("fileToRemove.FileName", errorMessage.ToString());
                    _logger.LogError("Removing file error: " + _options.BucketName, _options.ObjectName + " : " + "An error while removing the file, either not found or null parameter passed", string.Empty, "");

                    TempDataList.FileList.ForEach(x => Input.FileList.Add(x));

                    return(Page());
                }
            }
            catch (Exception e)
            {
                ModelState.AddModelError("CustomError", e.ToString());
                _logger.LogError("Removing file error: " + _options.BucketName, _options.ObjectName + " : " + e.Message, e.InnerException != null ? e.InnerException.Message : string.Empty, e.StackTrace);
                TempData.Put("FileList", TempDataList);

                return(Page());
            }

            TempData.Put("FileList", TempDataList);

            return(RedirectToPage());
        }
Beispiel #24
0
 public async Task RemoveImage(string fileName)
 {
     try
     {
         await storageClient.DeleteObjectAsync(bucketName, fileName);
     }
     catch (Google.GoogleApiException exception)
     {
         if (exception.Error.Code != 404)
         {
             throw;
         }
     }
 }
Beispiel #25
0
        public static async Task Delete(
            int projectId,
            string version,
            string branch,
            StorageClient client,
            upwardContext upwardDb,
            accountsContext accountsDb)
        {
            var ver = version.Split(".");

            int major = int.Parse(ver[0]);
            int minor = int.Parse(ver[1]);
            int patch = int.Parse(ver[2]);

            var pkg = await upwardDb.Pkgfile
                      .Where(r => r.Project == projectId && r.Major == major && r.Minor == minor && r.Patch == patch && r.Branch == branch)
                      .FirstOrDefaultAsync();

            var project = await accountsDb.Project
                          .Where(r => r.Id == projectId)
                          .FirstOrDefaultAsync();

            var user = await accountsDb.Userprofile
                       .Where(r => r.Id == project.Userid)
                       .FirstOrDefaultAsync();

            user.Size -= pkg.Size;
            upwardDb.Pkgfile.Remove(pkg);

            if (pkg.Filename.Count() == 0)
            {
                await accountsDb.SaveChangesAsync();

                await upwardDb.SaveChangesAsync();

                return;
            }
            else
            {
                foreach (string name in pkg.Filename)
                {
                    await client.DeleteObjectAsync("upward-test", $"pkg/{projectId}/{branch}/{version}/{name}");
                }

                await accountsDb.SaveChangesAsync();

                await upwardDb.SaveChangesAsync();
            }
        }
        // [END uploadimage]

        public async Task DeleteUploadedImage(long id)
        {
            try
            {
                await _storageClient.DeleteObjectAsync(_bucketName, id.ToString());
            }
            catch (Google.GoogleApiException exception)
            {
                // A 404 error is ok.  The image is not stored in cloud storage.
                if (exception.Error.Code != 404)
                {
                    throw;
                }
            }
        }
        public async Task Delete(string path, CancellationToken cancellationToken)
        {
            path = normalizePath(path);
            try {
                await _storageClient.DeleteObjectAsync(_configuration.Bucket, path, cancellationToken : cancellationToken);
            } catch (GoogleApiException ex) {
                if (ex.Error.Code == 404)
                {
                    // Already gone
                    return;
                }

                throw ex;
            }
        }
        public async Task <OperationResult> DeleteBlob(Uri key)
        {
            try
            {
                string bucket = GetBucketName(key);
                string blob   = GetObjectName(key);
                await _client.DeleteObjectAsync(bucket, blob);

                return(new OperationResult(true, "", HttpStatusCode.OK));
            }
            catch (GoogleApiException e)
            {
                return(new OperationResult(false, e.Message, (HttpStatusCode)e.Error.Code));
            }
        }
 public async Task DeleteFile(string filename, string folder)
 {
     try
     {
         var objNameToDelete = Util.Util.GenerateDataStoreObjectName(filename);
         await _storageClient.DeleteObjectAsync(BUCKET, $"{folder}/{objNameToDelete}");
     }
     catch (Google.GoogleApiException ex)
     {
         // ignore 404, the image is already deleted or never existed
         if (ex.Error.Code != 404)
         {
             throw ex;
         }
     }
 }
Beispiel #30
0
        public async Task CleanAsync(CancellationToken cancellationToken)
        {
            var deadlineAt = DateTime.UtcNow.AddDays(-7);
            var result     = storageClient.ListObjectsAsync(options.CloudStorageBackupBucketName);
            var enumerator = result.GetEnumerator();

            while (await enumerator.MoveNext(cancellationToken))
            {
                // 1週間より前のバックアップは削除する
                var obj = enumerator.Current;
                if (obj.TimeCreated < deadlineAt)
                {
                    await storageClient.DeleteObjectAsync(obj, cancellationToken : cancellationToken);

                    logger.LogInformation($"{obj.Name} を削除しました。");
                }
            }
        }