پرش به مطلب اصلی

حذف آبجکت از صندوقچه

با استفاده از این قابلیت شما می‌توانید نسخه‌ی فعلی یک آبجکت را حذف کنید.

مولفه‌ها

  • نام صندوقچه
  • نام آبجکت
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache - 2.0

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Threading.Tasks;
using System.Reflection;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace DeleteObject
{
class DeleteObject
{
private static IAmazonS3 _s3Client;

private const string BUCKET_NAME = "<BUCKET_NAME>";
private const string OBJECT_NAME = "<OBJECT_NAME>";

static void Main()
{
var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("<ACCESS-KEY>", "<SECRET-KEY>");
var config = new AmazonS3Config { ServiceURL = "<ENDPOINT>" };
_s3Client = new AmazonS3Client(awsCredentials, config);

DeleteObjectHelper().Wait();
}

private static async Task DeleteObjectHelper()
{
try
{
// Create the request
DeleteObjectsRequest request = new ()
{
BucketName = BUCKET_NAME,
Objects = new List<KeyVersion> { new KeyVersion() { Key = OBJECT_NAME, VersionId = null } }
};

// Submit the request
DeleteObjectsResponse response = await _s3Client.DeleteObjectsAsync(request);

Console.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented));
Console.WriteLine($"Object {OBJECT_NAME} successfully deleted from {BUCKET_NAME} bucket");
}
catch (AmazonS3Exception amazonS3Exception)
{
Console.WriteLine("An AmazonS3Exception was thrown. Exception: " + amazonS3Exception.ToString());
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.ToString());
}
}
}
}