当前位置: 首页>>代码示例>>C#>>正文


C# IAmazonS3.DeleteObjects方法代码示例

本文整理汇总了C#中IAmazonS3.DeleteObjects方法的典型用法代码示例。如果您正苦于以下问题:C# IAmazonS3.DeleteObjects方法的具体用法?C# IAmazonS3.DeleteObjects怎么用?C# IAmazonS3.DeleteObjects使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IAmazonS3的用法示例。


在下文中一共展示了IAmazonS3.DeleteObjects方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DeleteS3BucketWithObjectsInternal

        /// <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="asyncCancelableResult">An IAsyncCancelableResult that can be used to poll or wait for results, or both; 
        /// this value is also needed when invoking EndDeleteS3BucketWithObjects. IAsyncCancelableResult can also 
        /// be used to cancel the operation while it's in progress.</param>
        private static void DeleteS3BucketWithObjectsInternal(IAmazonS3 s3Client, string bucketName, 
            S3DeleteBucketWithObjectsOptions deleteOptions, Action<S3DeleteBucketWithObjectsUpdate> updateCallback,
            AsyncCancelableResult asyncCancelableResult)
        {
            // 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 (asyncCancelableResult.IsCancelRequested)
                {
                    // Signal that the operation is canceled.
                    asyncCancelableResult.SignalWaitHandleOnCanceled();
                    return;
                }

                // List all the versions of all the objects in the bucket.
                listVersionsResponse = s3Client.ListVersions(listVersionsRequest);

                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 =s3Client.DeleteObjects(new DeleteObjectsRequest
                    {
                        BucketName = bucketName,
                        Objects = keyVersionList,
                        Quiet = deleteOptions.QuietMode
                    });

                    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
//.........这里部分代码省略.........
开发者ID:aws,项目名称:aws-sdk-net,代码行数:101,代码来源:AmazonS3Util.bcl35.cs

示例2: DeleteImageArtifacts

        /// <summary>
        /// Deletes the artifacts associated with an import task using the bucket name
        /// and key prefix to the artifacts in Amazon S3. No check is performed to
        /// determine whether the associated conversion task is in progress.
        /// </summary>
        /// <param name="s3Client">
        /// An Amazon S3 client for the operation to use. This should have been constructed
        /// using credentials that have access to the bucket containing the image file
        /// artifacts and be scoped to the region containing the bucket.
        /// </param>
        /// <param name="bucketName">The name of the bucket containing the artifacts</param>
        /// <param name="keyPrefix">The common key prefix of the artifacts</param>
        /// <param name="progressCallback">Optional progress callback</param>
        public static void DeleteImageArtifacts(IAmazonS3 s3Client, 
                                                string bucketName, 
                                                string keyPrefix,
                                                CleanupProgressCallback progressCallback)
        {
            // build the full collection of keys to be deleted so that any progress
            // indicator managed by the caller is accurate
            SendProgressNotification(progressCallback, "Collating keys to image file artifacts", null);

            var artifactKeys = new List<string>();
            string marker = null;
            do
            {
                var listResponse = s3Client.ListObjects(new ListObjectsRequest
                {
                    BucketName = bucketName,
                    Prefix = keyPrefix,
                    Marker = marker
                });
                artifactKeys.AddRange(listResponse.S3Objects.Select(o => o.Key));
                marker = listResponse.NextMarker;
            } while (!string.IsNullOrEmpty(marker));

            if (artifactKeys.Count == 0)
            {
                var msg = string.Format(CultureInfo.InvariantCulture,
                                        "Found no image file artifacts with key prefix '{0}' to delete in bucket '{1}'.",
                                        keyPrefix,      
                                        bucketName);
                SendProgressNotification(progressCallback, msg, null);
                return;
            }

            var deletionMsg = string.Format(CultureInfo.InvariantCulture,
                                            "Deleting {0} image file artifacts (key prefix '{1}', bucket '{2}').", 
                                            artifactKeys.Count,
                                            keyPrefix,
                                            bucketName);
            SendProgressNotification(progressCallback, deletionMsg, 0);

            var index = 0;
            var processed = 0;
            do
            {
                var request = new DeleteObjectsRequest { BucketName = bucketName };
                while (request.Objects.Count < 1000 && index < artifactKeys.Count)
                {
                    request.Objects.Add(new KeyVersion { Key = artifactKeys[index++] });
                }

                s3Client.DeleteObjects(request);
                processed += request.Objects.Count;

                SendProgressNotification(progressCallback, deletionMsg, processed/artifactKeys.Count * 100);

            } while (processed < artifactKeys.Count);
        }
开发者ID:wmatveyenko,项目名称:aws-sdk-net,代码行数:70,代码来源:ImportCleanup.cs


注:本文中的IAmazonS3.DeleteObjects方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。