Mehedi

s3 bucket delete
mail@pastecode.io avatar
unknown
csharp
2 years ago
749 B
2
Indexable
Never
public async Task DeleteFileAsync(string? filename, string? bucketName = null, string? bucketFolder = null)
		{
			if (bucketName is null)
				bucketName = _bucketCredentials.BucketName;

			if (bucketFolder is null)
				bucketFolder = _bucketCredentials.BucketFolderPath;
			
			string pathDivider = bucketFolder is null ? string.Empty : "/";

			var request = new DeleteObjectRequest()
			{
				BucketName = bucketName,
				Key = $"{bucketFolder}{pathDivider}{filename}"
			};
			try
			{
				await _s3Client.DeleteObjectAsync(request);
			}
			catch (AmazonS3Exception e)
			{
				string message = $"Error encountered on server. Message:{e.Message} when downloading an object";
				throw new Exception(message);
			}

		}