public static void DeleteObjects() { const string accessKeyId = "<your access key id>"; const string accessKeySecret = "<your access key secret>"; const string endpoint = "<valid host name>"; const string bucketName = "<your bucket name>"; var client = new OssClient(endpoint, accessKeyId, accessKeySecret); try { var keys = new List<string>(); var listResult = client.ListObjects(bucketName); foreach (var summary in listResult.ObjectSummaries) { Console.WriteLine(summary.Key); keys.Add(summary.Key); } var request = new DeleteObjectsRequest(bucketName, keys, false); client.DeleteObjects(request); } catch (OssException ex) { Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}", ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId); } catch (Exception ex) { Console.WriteLine("Failed with error info: {0}", ex.Message); } }
public static void DeleteObjects(string bucketName) { try { var keys = new List<string>(); var listResult = client.ListObjects(bucketName); foreach (var summary in listResult.ObjectSummaries) { keys.Add(summary.Key); break; } var request = new DeleteObjectsRequest(bucketName, keys, false); client.DeleteObjects(request); Console.WriteLine("Delete objects succeeded"); } catch (OssException ex) { Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}", ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId); } catch (Exception ex) { Console.WriteLine("Failed with error info: {0}", ex.Message); } }
public static async Task <bool> DirectoryAsync(IAccount account, Ess3Bucket bucket, Ess3Directory directory, bool failOnHasItems, CancellationToken token) { if (BucketHasItems(bucket) && failOnHasItems) { return(false); } DeleteObjectsRequest request = CreateDirectoryDeletionRequest(bucket, directory); using IAmazonS3 client = new AmazonS3Client(account.GetCredentials(), bucket.RegionEndpoint); DeleteObjectsResponse?response = await Helpers .RunWithCatchAsync <DeleteObjectsRequest, DeleteObjectsResponse>(client.DeleteObjectsAsync, request, token) .ConfigureAwait(false); return((response?.HttpStatusCode ?? HttpStatusCode.BadRequest) == HttpStatusCode.OK); // I don't know which status code translates to success }
public async Task RemoveAsync(int quantity) { if (quantity < 0) { throw new ArgumentException(nameof(quantity)); } var getRequest = new ListObjectsV2Request() { BucketName = Bucket, }; var getResponse = await Client.ListObjectsV2Async(getRequest).ConfigureAwait(continueOnCapturedContext: false); if (getResponse.S3Objects.Count > 0) { var oldestDay = getResponse.S3Objects.Max(o => o.LastModified.Day) - quantity; var keys = getResponse.S3Objects.Where(o => o.LastModified.Day <= oldestDay) .Select(o => new KeyVersion() { Key = o.Key }).ToList(); if (keys.Count > 0) { var deleteRequest = new DeleteObjectsRequest() { BucketName = Bucket, Objects = keys }; await Client.DeleteObjectsAsync(deleteRequest) .ConfigureAwait(continueOnCapturedContext: false); } else { await Task.CompletedTask; } } else { await Task.CompletedTask; } }
public void DeleteFiles(IEnumerable <string> filePaths) { var batches = filePaths .BatchesOf(MultiObjectLimit); foreach (var batch in batches) { var request = new DeleteObjectsRequest { BucketName = BucketName, }; foreach (var filePath in batch) { request.AddKey(filePath); } AmazonS3.DeleteObjects(request); } }
public virtual void DeleteBucket(AmazonS3Client s3Client, string bucketName) { // First, try to delete the bucket. var deleteBucketRequest = new DeleteBucketRequest { BucketName = bucketName }; try { s3Client.DeleteBucket(deleteBucketRequest); // If we get here, no error was generated so we'll assume the bucket was deleted and return. return; } catch (AmazonS3Exception ex) { if (!ex.ErrorCode.Equals("BucketNotEmpty")) { // We got an unanticipated error. Just rethrow. throw; } } // If we got here, then our bucket isn't empty so we need to delete the items in it first. DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest { BucketName = bucketName }; foreach (S3Object obj in s3Client.ListObjects(new ListObjectsRequest { BucketName = bucketName }).S3Objects) { // Add keys for the objects to the delete request deleteObjectsRequest.AddKey(obj.Key, null); } // Submit the request s3Client.DeleteObjects(deleteObjectsRequest); // The bucket is empty now, so delete the bucket. s3Client.DeleteBucket(deleteBucketRequest); }
/// <summary> /// Removes all objects from the bucket /// </summary> /// <param name="prefix">Only delete objects that begin with the specified prefix.</param> /// <param name="lastModified">Only delete objects that where modified prior to this date.</param> /// <param name="settings">The <see cref="S3Settings"/> required to delete from Amazon S3.</param> public IList <string> DeleteAll(string prefix, DateTimeOffset lastModified, S3Settings settings) { //Get S3 Objects IList <S3Object> objects = this.GetObjects(prefix, settings); List <string> list = new List <string>(); foreach (S3Object obj in objects) { if ((lastModified == DateTimeOffset.MinValue) && (obj.LastModified < lastModified)) { list.Add(obj.Key); } } //Delete AmazonS3Client client = this.GetClient(settings); while (list.Count > 0) { int max = list.Count; if (max > 1000) { max = 1000; } DeleteObjectsRequest request = new DeleteObjectsRequest(); request.BucketName = settings.BucketName; for (int index = 0; index < max; index++) { request.AddKey(list[index]); } client.DeleteObjects(request); _Log.Verbose("Deleting {0} objects from bucket {1}...", max, settings.BucketName); list.RemoveRange(0, max); } return(list); }
public void Dispose() { var objectsRequest = new ListObjectsRequest { BucketName = Config["Bucket"], Prefix = ContainerPrefix, MaxKeys = 100000 }; var keys = new List <KeyVersion>(); do { var objectsResponse = _client.ListObjectsAsync(objectsRequest).Result; keys.AddRange(objectsResponse.S3Objects .Select(x => new KeyVersion() { Key = x.Key, VersionId = null })); // If response is truncated, set the marker to get the next set of keys. if (objectsResponse.IsTruncated) { objectsRequest.Marker = objectsResponse.NextMarker; } else { objectsRequest = null; } } while (objectsRequest != null); if (keys.Count > 0) { var objectsDeleteRequest = new DeleteObjectsRequest() { BucketName = Config["Bucket"], Objects = keys }; _client.DeleteObjectsAsync(objectsDeleteRequest).Wait(); } }
private async Task RemoveFiles(string[] files) { var request = new DeleteObjectsRequest { BucketName = _bucketName }; request.Objects.AddRange(files.Select(f => new KeyVersion { Key = f })); try { _ = await _s3Client.DeleteObjectsAsync(request, _cancellationToken).ConfigureAwait(false); } catch (DeleteObjectsException) { //suppress it, we are not interested at the moment why some files could not be removed (permissions or something else) } }
public override async Task <PurgeResult> PurgeAsync() { Log.Information($"Purge to s3 removeThreshold: {RemoveThreshold}"); if (RemoveThreshold == null || RemoveThreshold.Value > DateTime.Now) { return(new PurgeResult()); } using (var s3Client = new AmazonS3Client(this.accessKeyId, this.accessKeySecret, RegionEndpoint.GetBySystemName(this.region))) { var objectListing = s3Client.ListObjectsAsync(this.bucket, this.PathPrefix); await objectListing; var deleteRequest = new DeleteObjectsRequest(); deleteRequest.BucketName = this.bucket; foreach (var s3Object in objectListing.Result.S3Objects) { if (s3Object.LastModified.ToUniversalTime() <= RemoveThreshold.Value) { deleteRequest.AddKey(s3Object.Key); } } if (deleteRequest.Objects.Count == 0) { Log.Information("Nothing to purge."); } else { deleteRequest.Objects.ForEach(item => { Log.Information($"Prepare to purge: {item.Key}"); }); await s3Client.DeleteObjectsAsync(deleteRequest); Log.Information("S3 purge done."); } return(new PurgeResult()); } }
public void DeleteBookData(string bucketName, string key) { if (BookUpload.IsDryRun) { return; } var listMatchingObjectsRequest = new ListObjectsRequest() { BucketName = bucketName, Prefix = key }; ListObjectsResponse matchingFilesResponse; do { // Note: ListObjects can only return 1,000 objects at a time, // and DeleteObjects can only delete 1,000 objects at a time. // So a loop is needed if the book contains 1,001+ objects. matchingFilesResponse = GetAmazonS3(bucketName).ListObjects(listMatchingObjectsRequest); if (matchingFilesResponse.S3Objects.Count == 0) { return; } var deleteObjectsRequest = new DeleteObjectsRequest() { BucketName = bucketName, Objects = matchingFilesResponse.S3Objects.Select(s3Object => new KeyVersion() { Key = s3Object.Key }).ToList() }; var response = GetAmazonS3(bucketName).DeleteObjects(deleteObjectsRequest); Debug.Assert(response.DeleteErrors.Count == 0); // Prep the next request (if needed) listMatchingObjectsRequest.Marker = matchingFilesResponse.NextMarker; }while (matchingFilesResponse.IsTruncated); // Returns true if haven't reached the end yet }
public async Task EmptyContainer(AmazonS3BlobContainer container) { var bucket = container?.Id; if (string.IsNullOrWhiteSpace(bucket)) { throw new ArgumentNullException(nameof(bucket)); } try { _logger.LogInformation("Trying get objects from {@Bucket}", bucket); var request = new ListObjectsV2Request { BucketName = bucket, MaxKeys = int.MaxValue }; var objectsResponse = await _client.ListObjectsV2Async(request).ConfigureAwait(false); _logger.LogDebug("Got {@Count} object key(s) in {@Bucket}", objectsResponse?.KeyCount, bucket); if (objectsResponse is null || objectsResponse.KeyCount == 0) { return; } var objectsToRemove = objectsResponse.S3Objects.Select(o => new KeyVersion { Key = o.Key }).ToList(); _logger.LogInformation("Trying delete {@Count} objects from {@Bucket}", objectsToRemove.Count, bucket); var deleteRequest = new DeleteObjectsRequest { BucketName = bucket, Objects = objectsToRemove }; var deleteResponse = await _client.DeleteObjectsAsync(deleteRequest).ConfigureAwait(false); _logger.LogInformation("Removed {@Count} objects from {@Bucket}", deleteResponse?.DeletedObjects.Count, bucket); } catch (Exception ex) { _logger.LogError(ex, "Could not remove objects from bucket {@Bucket}", bucket); throw new BlobStorageException($"Could not remove objects from bucket {bucket}", ex); } }
public void DeleteAllOnFolder(string folderName) { try { if (IscdnPropertyValid()) { using (var cloudFrontClient = CreateClient()) { do { ListObjectsV2Request request = new ListObjectsV2Request() { BucketName = cdnPropertiesModel.S3BucketName, Prefix = folderName }; var urls = cloudFrontClient.ListObjectsV2(request); var keys = urls?.S3Objects?.Select(s => new KeyVersion() { Key = s.Key })?.Distinct()?.ToList(); if (keys == null || !keys.Any()) { break; } DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = cdnPropertiesModel.S3BucketName, Objects = keys }; cloudFrontClient.DeleteObjects(multiObjectDeleteRequest); } while (true); } } } catch (Exception ex) { Log.Error(Helper.Constants.Message.DeleteAllOnFolderException + ex.Message, typeof(AwsS3CdnServerHandler)); SheerResponse.ShowError(ex); } }
/// <summary> /// Deletes all of the S3Objects in the provided folder asset. /// </summary> /// <param name="client">The client.</param> /// <param name="assetStorageProvider">The asset storage Provider.</param> /// <param name="asset">The asset.</param> /// <returns></returns> private bool MultipleObjectDelete(AmazonS3Client client, AssetStorageProvider assetStorageProvider, Asset asset) { // The list of keys that will be passed into the multiple delete request List <KeyVersion> keys = new List <KeyVersion>(); // Amazon only accepts 1000 keys per request, use this to keep track of how many already sent int keyIndex = 0; try { // Get a list of objest with prefix var assetDeleteList = ListObjects(assetStorageProvider, asset, true); // Create the list of keys foreach (var assetDelete in assetDeleteList) { keys.Add(new KeyVersion { Key = assetDelete.Key }); } while (keyIndex < keys.Count()) { int range = keys.Count() - keyIndex < 1000 ? keys.Count() - keyIndex : 1000; var deleteObjectsRequest = new DeleteObjectsRequest { BucketName = GetAttributeValue(assetStorageProvider, AttributeKeys.Bucket), Objects = keys.GetRange(keyIndex, range) }; DeleteObjectsResponse response = client.DeleteObjects(deleteObjectsRequest); keyIndex += range; } return(true); } catch (Exception ex) { ExceptionLogService.LogException(ex); throw; } }
private static void DeleteObjects() { try { DeleteObjectsRequest request = new DeleteObjectsRequest(); request.BucketName = bucketName; request.Quiet = true; request.AddKey(objectName); request.AddKey(destobjectName); DeleteObjectsResponse response = client.DeleteObjects(request); Console.WriteLine("Delete objects response: {0}", response.StatusCode); } catch (ObsException ex) { Console.WriteLine("Exception errorcode: {0}, when delete objects.", ex.ErrorCode); Console.WriteLine("Exception errormessage: {0}", ex.ErrorMessage); } }
private static void MultiObjectDelete(List <KeyVersion> keys) { // a. multi-object delete by specifying the key names and version IDs. DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = "takatanet-test-bucket", Objects = keys // This includes the object keys and null version IDs. }; multiObjectDeleteRequest.AddKey("AWSSDKcopy2.dll", null); try { DeleteObjectsResponse response = s3Client.DeleteObjects(multiObjectDeleteRequest); //Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count); } catch (DeleteObjectsException e) { //PrintDeletionReport(e); } }
/// <summary> /// Delete multiple objects from a version-enabled bucket. /// </summary> /// <param name="client">The initialized S3 client object used to call /// DeleteObjectVersionsAsync, DeleteObjectsAsync, and /// RemoveDeleteMarkersAsync.</param> /// <param name="bucketName">The name of the bucket from which to delete /// objects.</param> /// <param name="keys">A list of key names for the objects to delete.</param> static async Task VersionedDeleteAsync(IAmazonS3 client, string bucketName, List <KeyVersion> keys) { var multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keys, // This includes the object keys and specific version IDs. }; try { Console.WriteLine("Executing VersionedDelete..."); DeleteObjectsResponse response = await client.DeleteObjectsAsync(multiObjectDeleteRequest); Console.WriteLine($"Successfully deleted all the {response.DeletedObjects.Count} items"); } catch (DeleteObjectsException ex) { DisplayDeletionErrors(ex); } }
private async Task DeleteObjects(List <S3Object> objects) // does not work, "Access denied" exception { if (objects is null) { throw new ArgumentNullException(nameof(objects)); } await Initialize().ConfigureAwait(false); var deleteRequest = new DeleteObjectsRequest { BucketName = _bucketName }; foreach (var obj in objects) { deleteRequest.AddKey(obj.Key); } var deleteResponse = await Client.DeleteObjectsAsync(deleteRequest).ConfigureAwait(false); }
public bool DeleteBucketObjects() { try { var keys = new List <string>(); var listResult = client.ListObjects(bucketName); foreach (var summary in listResult.ObjectSummaries) { keys.Add(summary.Key); } var request = new DeleteObjectsRequest(bucketName, keys, false); client.DeleteObjects(request); return(true); } catch (OssException ex) { lastError = ex; return(false); } }
private async Task ClearBucket(string bucketName, string folderName) { try { //Old delete codes //var deleteObjectRequest = new DeleteObjectRequest //{ // BucketName = bucketName, // Key = folderName + "/" //}; //Console.WriteLine("Deleting an object"); //S3Client.DeleteObjectAsync(deleteObjectRequest); //New delete codes DeleteObjectsRequest request2 = new DeleteObjectsRequest(); ListObjectsRequest request = new ListObjectsRequest { BucketName = bucketName, Prefix = folderName }; ListObjectsResponse response = await S3Client.ListObjectsAsync(request); // Process response. foreach (S3Object entry in response.S3Objects) { request2.AddKey(entry.Key); } request2.BucketName = bucketName; DeleteObjectsResponse response2 = await S3Client.DeleteObjectsAsync(request2); } catch (AmazonS3Exception e) { //Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message); } catch (Exception e) { //Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message); } }
public async Task <ActionResult <MediaLinkModel> > Delete(Guid id) { MediaLinkModel model = await _context.MediaLinks.Include(x => x.Group).FirstOrDefaultAsync(x => x.Id == id); if (model == null) { return(NotFound($"Image with Id {id} not found.")); } string modelKey = $"{model.Group.NormalizedName}/{model.Name}"; string thumbnailKey = $"{Constants.ThumbnailGroup}/{modelKey}"; DeleteObjectsRequest request = new DeleteObjectsRequest { BucketName = _config["aws:bucket"], Objects = new List <KeyVersion>() { new KeyVersion() { Key = modelKey }, new KeyVersion() { Key = thumbnailKey } } }; try { await _s3Client.DeleteObjectsAsync(request); _context.MediaLinks.Remove(model); await _context.SaveChangesAsync(); return(Ok(model)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
/// <summary> /// Delete a folder /// </summary> /// <param name="prefix">prefix</param> public void DeleteFolder(string prefix) { // Get all object with specified prefix var listRequest = new ListObjectsRequest() { BucketName = _bucketName, Prefix = prefix }; var deleteRequest = new DeleteObjectsRequest { BucketName = _bucketName }; do { var listResponse = _client.ListObjects(listRequest); // Add all object with specified prefix to delete request. foreach (var entry in listResponse.S3Objects) { deleteRequest.AddKey(entry.Key); } if (listResponse.IsTruncated) { listRequest.Marker = listResponse.NextMarker; } else { listRequest = null; } }while (listRequest != null); // Delete all the object with specified prefix. if (deleteRequest.Objects.Count > 0) { var deleteResponse = _client.DeleteObjects(deleteRequest); deleteResponse.DisposeIfDisposable(); } }
public void delete(string bucket_name, params string[] key_names) { try { // the "DeleteObjectsRequest" has a 1000 element limit, per: // https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TDeleteObjectsRequest.html for (int i = 0; i < key_names.Length; i += 900) // using 900 instead of 1000 { int keys_to_add = key_names.Length - i; if (keys_to_add > 900) { keys_to_add = 900; } var request = new DeleteObjectsRequest(); request.BucketName = bucket_name; for (int j = 0; j < keys_to_add; j++) { request.AddKey(key_names[i + j], null); // version ID is null. } var response = client.DeleteObjects(request); // As of Jan 28, 2018 // Deleting objects "works" even if the objects don't exist // No error will be reported. if (response.DeleteErrors.Count > 0) { throw new Exception(response.DeleteErrors.Count + " error(s) while deleting objects from the AWS bucket \"" + bucket_name + "\"."); } } } catch (Exception ex) { throw new Exception("Error while trying to delete from AWS \"" + bucket_name + "\". AWS error message: " + ex.Message); } }
public void DeleteObjectsWithNewLinesTest(string objectKey, string expectedEscapedKey) { var request = new DeleteObjectsRequest { BucketName = "foo", Objects = new System.Collections.Generic.List <KeyVersion> { new KeyVersion { Key = objectKey } } }; var internalRequest = S3ArnTestUtils.RunMockRequest(request, DeleteObjectsRequestMarshaller.Instance, new AmazonS3Config { RegionEndpoint = RegionEndpoint.USEast1 }); var content = System.Text.Encoding.UTF8.GetString(internalRequest.Content); Assert.IsFalse(content.Contains(objectKey)); Assert.IsTrue(content.Contains(expectedEscapedKey)); }
static async Task VersionedDeleteAsync(List <KeyVersion> keys) { // a. Perform a multi-object delete by specifying the key names and version IDs. var multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keys // This includes the object keys and specific version IDs. }; try { Console.WriteLine("Executing VersionedDelete..."); DeleteObjectsResponse response = await s3Client.DeleteObjectsAsync(multiObjectDeleteRequest); Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count); } catch (DeleteObjectsException e) { PrintDeletionReport(e); } }
public override async Task <BatchDeleteProcessor> RemovePrefixAsync(string bucketName, string prefix, int chunkSize, CancellationToken cancellationToken = default) { ValidateInstance(); var processor = new BatchDeleteProcessor(async(IEnumerable <string> keys) => { var deleteRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keys.Select(k => new KeyVersion { Key = k }).ToList() }; await _client.DeleteObjectsAsync(deleteRequest, cancellationToken).ConfigureAwait(false); }); ListObjectsV2Response response; var prefixToFilter = (prefix.EndsWith("/") ? prefix : prefix + "/"); ListObjectsV2Request request = new ListObjectsV2Request { BucketName = bucketName, Prefix = prefixToFilter, MaxKeys = chunkSize }; do { response = await _client.ListObjectsV2Async(request); if (!response.S3Objects.Any()) { continue; } processor.EnqueueChunk(response.S3Objects.Select(o => o.Key)); request.ContinuationToken = response.NextContinuationToken; } while (response.IsTruncated); return(processor); }
protected override PurgeResult Purge() { if (RemoveThreshold == null || RemoveThreshold.Value > DateTime.Now) { return(new PurgeResult()); } Log.Information($"Purge to aliyun removeThreshold: {RemoveThreshold}"); OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret); var objectListing = client.ListObjects(bucketName, this.PathPrefix); var objectsToDelete = new List <string>(); foreach (var summary in objectListing.ObjectSummaries) { if (summary.LastModified.ToUniversalTime() <= RemoveThreshold.Value) { objectsToDelete.Add(summary.Key); } } if (objectsToDelete.Count == 0) { Log.Information("Nothing to purge."); } else { objectsToDelete.ForEach(item => { Log.Information($"Prepare to purge: {item}"); }); DeleteObjectsRequest request = new DeleteObjectsRequest(bucketName, objectsToDelete); client.DeleteObjects(request); Log.Information("Aliyun oss purge done."); } return(new PurgeResult()); }
public void DeleteObjectsWithHiddenCharacters() { char[] buffer = new char[2]; buffer[0] = Convert.ToChar(0x1c); buffer[1] = Convert.ToChar(0x1a); var newKey1 = _keyName + (new string(buffer)) + ".1.cd"; var newKey2 = _keyName + (new string(buffer)) + ".2.cd"; var newKey3 = _keyName + ".3.cd"; try { _ossClient.PutObject(_bucketName, newKey1, Config.UploadTestFile); _ossClient.PutObject(_bucketName, newKey2, Config.UploadTestFile); _ossClient.PutObject(_bucketName, newKey3, Config.UploadTestFile); Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, newKey1)); Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, newKey2)); Assert.IsTrue(OssTestUtils.ObjectExists(_ossClient, _bucketName, newKey3)); var keys = new List <string>(); keys.Add(newKey1); keys.Add(newKey2); keys.Add(newKey3); var request = new DeleteObjectsRequest(_bucketName, keys); _ossClient.DeleteObjects(request); Assert.IsFalse(OssTestUtils.ObjectExists(_ossClient, _bucketName, newKey1)); Assert.IsFalse(OssTestUtils.ObjectExists(_ossClient, _bucketName, newKey2)); Assert.IsFalse(OssTestUtils.ObjectExists(_ossClient, _bucketName, newKey3)); } finally { _ossClient.DeleteObject(_bucketName, newKey1); _ossClient.DeleteObject(_bucketName, newKey2); _ossClient.DeleteObject(_bucketName, newKey3); } }
/// <summary> /// Deletes all of the supplied keys in the specified bucket /// </summary> /// <param name="keys"></param> /// <param name="bucket"></param> /// <returns></returns> private static async Task <int> DeleteObjectsAsync(IEnumerable <KeyVersion> keys, string bucket) { int Counter = 0; // Delete all of the files, 1000 at a time, that have already been delivered for this billing period, // but are not part of this CUR's chunk set (we filtered out the GUID earlier) foreach (List <KeyVersion> Chunk in ChunkList(keys, 1000)) { DeleteObjectsRequest DeleteRequest = new DeleteObjectsRequest() { BucketName = bucket, Objects = Chunk }; while (true) { DeleteObjectsResponse DeleteResponse = await _S3Client.DeleteObjectsAsync(DeleteRequest); if (DeleteResponse.HttpStatusCode == HttpStatusCode.OK) { Counter += DeleteResponse.DeletedObjects.Count; break; } else if (DeleteResponse.HttpStatusCode == HttpStatusCode.ServiceUnavailable) { // Use linear backoff Thread.Sleep(2000); } else { string Message = String.Join("\n", DeleteResponse.DeleteErrors.Select(x => $"{x.Key} = {x.Code} : {x.Message}")); throw new Exception($"Could not delete objects from S3 with status {(int)DeleteResponse.HttpStatusCode} and errors:\n{Message}"); } } } return(Counter); }
public async Task <bool> RemoveObjectAsync(string bucketName, List <string> objectNames) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } if (objectNames == null || objectNames.Count == 0) { throw new ArgumentNullException(nameof(objectNames)); } var quietMode = false; // DeleteObjectsRequest的第三个参数指定返回模式。 var request = new DeleteObjectsRequest(bucketName, objectNames, quietMode); // 删除多个文件。 var result = _client.DeleteObjects(request); if ((!quietMode) && (result.Keys != null)) { if (result.Keys.Count() == objectNames.Count) { return(await Task.FromResult(true)); } else { throw new Exception("Some file delete failed."); } } else { if (result != null) { return(await Task.FromResult(true)); } else { return(await Task.FromResult(true)); } } }
public void DeleteObjectsTest() { var accessPointArn = "arn:aws:s3:us-east-1:000011112222:accesspoint/testpoint"; var request = new DeleteObjectsRequest { BucketName = accessPointArn, Objects = new List <KeyVersion> { new KeyVersion { Key = "foo1.txt" }, new KeyVersion { Key = "foo2.txt" } } }; var internalRequest = S3ArnTestUtils.RunMockRequest(request, DeleteObjectsRequestMarshaller.Instance); Assert.AreEqual(new Uri("https://testpoint-000011112222.s3-accesspoint.us-east-1.amazonaws.com"), internalRequest.Endpoint); }
public void DeleteFolder(string remotePath) { if (remotePath[remotePath.Length - 1] != '/') { remotePath += "/"; } var request = new ListObjectsRequest(); ListObjectsRequest req = new ListObjectsRequest(); req.BucketName = CurrentBucketName; req.Prefix = remotePath; do { ListObjectsResponse resp = _transfer.S3Client.ListObjects(req); if (resp.IsTruncated) { req.Marker = resp.NextMarker; } else { req = null; } DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(); deleteRequest.BucketName = CurrentBucketName; if (resp.S3Objects.Count == 0) { break; } foreach (var item in resp.S3Objects) { deleteRequest.AddKey(item.Key); } DeleteObjectsResponse deleteResponse = _transfer.S3Client.DeleteObjects(deleteRequest); }while (req != null); }