本文整理汇总了C#中IAmazonS3.DeleteObjectsAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IAmazonS3.DeleteObjectsAsync方法的具体用法?C# IAmazonS3.DeleteObjectsAsync怎么用?C# IAmazonS3.DeleteObjectsAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IAmazonS3
的用法示例。
在下文中一共展示了IAmazonS3.DeleteObjectsAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DeleteBucketWithObjects
public static void DeleteBucketWithObjects(IAmazonS3 s3Client, string bucketName)
{
// Validations.
if (s3Client == null)
{
throw new ArgumentNullException("s3Client", "The s3Client cannot be null!");
}
if (string.IsNullOrEmpty(bucketName))
{
throw new ArgumentNullException("bucketName", "The bucketName cannot be null or empty string!");
}
var listVersionsRequest = new ListVersionsRequest
{
BucketName = bucketName
};
ListVersionsResponse listVersionsResponse = null;
string lastRequestId = null;
var exception = new Exception();
var mre = new AutoResetEvent(false);
// Iterate through the objects in the bucket and delete them.
do
{
// List all the versions of all the objects in the bucket.
s3Client.ListVersionsAsync(listVersionsRequest, (result) =>
{
exception = result.Exception;
listVersionsResponse = result.Response;
mre.Set();
}, new AsyncOptions() { ExecuteCallbackOnMainThread = false });
mre.WaitOne();
Utils.AssertExceptionIsNull(exception);
lastRequestId = listVersionsResponse.ResponseMetadata.RequestId;
if (listVersionsResponse.Versions.Count == 0)
{
// If the bucket has no objects break the loop.
break;
}
var keyVersionList = new List<KeyVersion>(listVersionsResponse.Versions.Count);
for (int index = 0; index < listVersionsResponse.Versions.Count; index++)
{
keyVersionList.Add(new KeyVersion
{
Key = listVersionsResponse.Versions[index].Key,
VersionId = listVersionsResponse.Versions[index].VersionId
});
}
var deleteObjectsResponse = new DeleteObjectsResponse();
// Delete the current set of objects.
s3Client.DeleteObjectsAsync(new DeleteObjectsRequest
{
BucketName = bucketName,
Objects = keyVersionList,
Quiet = true
}, (result) =>
{
deleteObjectsResponse = result.Response;
exception = result.Exception;
mre.Set();
}, new AsyncOptions() { ExecuteCallbackOnMainThread = false });
mre.WaitOne();
Utils.AssertExceptionIsNull(exception);
// Set the markers to get next set of objects from the bucket.
listVersionsRequest.KeyMarker = listVersionsResponse.NextKeyMarker;
listVersionsRequest.VersionIdMarker = listVersionsResponse.NextVersionIdMarker;
}
// Continue listing objects and deleting them until the bucket is empty.
while (listVersionsResponse.IsTruncated);
// Bucket is empty, delete the bucket.
s3Client.DeleteBucketAsync(new DeleteBucketRequest
{
BucketName = bucketName
}, (result) =>
{
exception = result.Exception;
mre.Set();
}, new AsyncOptions() { ExecuteCallbackOnMainThread = false });
mre.WaitOne();
Utils.AssertExceptionIsNull(exception);
}
示例2: DeleteS3BucketWithObjectsInternalAsync
/// <summary>
/// Deletes an S3 bucket which contains objects.
/// An S3 bucket which contains objects cannot be deleted until all the objects
/// in it are deleted. The function deletes all the objects in the specified
/// bucket and then deletes the bucket itself.
/// </summary>
/// <param name="bucketName">The bucket to be deleted.</param>
/// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param>
/// <param name="deleteOptions">Options to control the behavior of the delete operation.</param>
/// <param name="updateCallback">The callback which is used to send updates about the delete operation.</param>
/// <param name="token">token to check if the operation has been request to cancel.</param>
private static async Task DeleteS3BucketWithObjectsInternalAsync(IAmazonS3 s3Client, string bucketName,
S3DeleteBucketWithObjectsOptions deleteOptions, Action<S3DeleteBucketWithObjectsUpdate> updateCallback,
CancellationToken token)
{
// Validations.
if (s3Client == null)
{
throw new ArgumentNullException("s3Client", "The s3Client cannot be null!");
}
if (string.IsNullOrEmpty(bucketName))
{
throw new ArgumentNullException("bucketName", "The bucketName cannot be null or empty string!");
}
var listVersionsRequest = new ListVersionsRequest
{
BucketName = bucketName
};
ListVersionsResponse listVersionsResponse;
// Iterate through the objects in the bucket and delete them.
do
{
// Check if the operation has been canceled.
if (token.IsCancellationRequested)
{
// Signal that the operation is canceled.
return;
}
// List all the versions of all the objects in the bucket.
listVersionsResponse = await s3Client.ListVersionsAsync(listVersionsRequest,token).ConfigureAwait(false);
if (listVersionsResponse.Versions.Count == 0)
{
// If the bucket has no objects break the loop.
break;
}
var keyVersionList = new List<KeyVersion>(listVersionsResponse.Versions.Count);
for (int index = 0; index < listVersionsResponse.Versions.Count; index++)
{
keyVersionList.Add(new KeyVersion
{
Key = listVersionsResponse.Versions[index].Key,
VersionId = listVersionsResponse.Versions[index].VersionId
});
}
try
{
// Delete the current set of objects.
var deleteObjectsResponse = await s3Client.DeleteObjectsAsync(new DeleteObjectsRequest
{
BucketName = bucketName,
Objects = keyVersionList,
Quiet = deleteOptions.QuietMode
},token).ConfigureAwait(false);
if (!deleteOptions.QuietMode)
{
// If quiet mode is not set, update the client with list of deleted objects.
InvokeS3DeleteBucketWithObjectsUpdateCallback(
updateCallback,
new S3DeleteBucketWithObjectsUpdate
{
DeletedObjects = deleteObjectsResponse.DeletedObjects
}
);
}
}
catch (DeleteObjectsException deleteObjectsException)
{
if (deleteOptions.ContinueOnError)
{
// Continue the delete operation if an error was encountered.
// Update the client with the list of objects that were deleted and the
// list of objects on which the delete failed.
InvokeS3DeleteBucketWithObjectsUpdateCallback(
updateCallback,
new S3DeleteBucketWithObjectsUpdate
{
DeletedObjects = deleteObjectsException.Response.DeletedObjects,
DeleteErrors = deleteObjectsException.Response.DeleteErrors
}
);
}
//.........这里部分代码省略.........