Skip to main content

Abort Multipart Upload

You must stop the uploading process in order to release the storage reserved for this upload in your bucket if a multipart upload fails or if you decide not to keep uploading the parts.

Note: Only the upload that has started but not finished needs to be aborted; once the upload is finished and the final file is assembled from the parts, the process no longer needs to be stopped.

Please take note that any files that are still uploading should do so till they are finished.

Components

Object Name (Key)

Name of the object after uploading in the bucket

Sample Code to Abort Multipart Upload

The client receives a UploadId key when the multipart upload begins. If you don't have this key, you can still abort an incomplete upload by first getting a list of all multi-part uploads using the following code.

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

namespace ListMultipartUploadExample
{
class ListMultipartUpload
{
private const string bucketName = "<BUCKET_NAME>";
private static IAmazonS3 _s3Client;
public static void Main()
{
var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("<ACCESS-KEY>", "<SECRET-KEY>");
var config = new AmazonS3Config { ServiceURL = "<ENDPOINT>" };
_s3Client = new AmazonS3Client(awsCredentials, config);
ListMultipartUploadAsync().Wait();
}

private static async Task ListMultipartUploadAsync()
{
try
{
ListMultipartUploadsRequest request = new()
{
BucketName = bucketName,
};

ListMultipartUploadsResponse response = await _s3Client.ListMultipartUploadsAsync(request);

Console.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented));
}
catch (AmazonS3Exception amazonS3Exception)
{
Console.WriteLine("An AmazonS3Exception was thrown. Exception: " + amazonS3Exception.ToString());
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.ToString());
}
}
}
}


The following code sample will show you how to abort the upload(s) if the aforementioned code prints multipart upload values.

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0

namespace AbortMultipartUploadExample
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System.Reflection;

/// <summary>
/// This example shows how to configure your bucket to allow aborting
/// multipart upload. This example was
/// created using the AWS SDK for .NET 3.7 and .NET Core 5.0.
/// </summary>
public class AbortMultipartUpload
{
// Remember to change the bucket name to the name of an Amazon Simple
// Storage Service (Amazon S3) bucket that exists on your account.
private const string BucketName = "<BUCKET_NAME>";
private const string OBJECT_NAME = "<OBJECT_NAME>";

/// <summary>
/// The Main method creates the the bucket to be able to abort multipart
/// uploads.
/// </summary>
public static async Task Main()
{
var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("<ACCESS-KEY>", "<SECRET-KEY>");
var config = new AmazonS3Config { ServiceURL = "<ENDPOINT>" };
var _s3Client = new AmazonS3Client(awsCredentials, config);

await AbortMultipartUploadAsync(_s3Client);
}

/// <summary>
///
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
private static async Task AbortMultipartUploadAsync(AmazonS3Client client)
{
try
{
var uploadId = "<UPLOAD-ID>";
AbortMultipartUploadRequest request = new()
{
BucketName = BucketName,
Key = OBJECT_NAME,
UploadId = uploadId
};

AbortMultipartUploadResponse response = await client.AbortMultipartUploadAsync(request);

Console.WriteLine($"Multipart upload with ID {uploadId} aborted");
}
catch (AmazonS3Exception amazonS3Exception)
{
Console.WriteLine("An AmazonS3Exception was thrown. Exception: " + amazonS3Exception.ToString());
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.ToString());
}
}
}
}