AWS Code Sample
Catalog

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

DeleteObjectVersion.cs

DeleteObjectVersion.cs deletes an object from a versioned bucket. It deletes a specific version of the object by specifying the object key name and version ID.

/* ** Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * http://aws.amazon.com/apache2.0/ * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ using Amazon.S3; using Amazon.S3.Model; using System; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { class DeleteObjectVersion { private const string bucketName = "*** versioning-enabled bucket name ***"; private const string keyName = "*** Object Key Name ***"; // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 client; public static void Main() { client = new AmazonS3Client(bucketRegion); CreateAndDeleteObjectVersionAsync().Wait(); } private static async Task CreateAndDeleteObjectVersionAsync() { try { // Add a sample object. string versionID = await PutAnObject(keyName); // Delete the object by specifying an object key and a version ID. DeleteObjectRequest request = new DeleteObjectRequest { BucketName = bucketName, Key = keyName, VersionId = versionID }; Console.WriteLine("Deleting an object"); await client.DeleteObjectAsync(request); } 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); } } static async Task<string> PutAnObject(string objectKey) { PutObjectRequest request = new PutObjectRequest { BucketName = bucketName, Key = objectKey, ContentBody = "This is the content body!" }; PutObjectResponse response = await client.PutObjectAsync(request); return response.VersionId; } } }

Sample Details

Service: s3

Last tested: 2018-04-30

Author: AWS

Type: full-example

On this page: