Complete Object's Multipart Upload
Components
- Credentials
- Bucket Name
- Object Name
- Parts
- Upload ID
- .NET
- PHP
- Python
- Javascript
- GO
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace CompleteMultipartUpload
{
class CompleteMultipartUpload
{
private const string bucketName = "<BUCKET_NAME>";
private const string objectName = "<OBJECT_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);
CompleteMultipartUploadAsync().Wait();
}
private static async Task CompleteMultipartUploadAsync()
{
try
{
CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest
{
BucketName = bucketName,
Key = objectName,
UploadId = "123",
PartETags = new List<PartETag>
{
new PartETag { ETag = "string", PartNumber = 123 },
}
};
CompleteMultipartUploadResponse compResponse = await _s3Client.CompleteMultipartUploadAsync(compRequest);
Console.WriteLine($"Multipart upload completed");
}
catch (AmazonS3Exception amazonS3Exception)
{
Console.WriteLine("An AmazonS3Exception was thrown. Exception: " + amazonS3Exception.ToString());
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.ToString());
}
}
}
}
<?php
require('client.php');
$bucket = '<BUCKET_NAME>';
try {
$result = $client->completeMultipartUpload([
'Bucket' => $bucket, // REQUIRED
'ExpectedBucketOwner' => '<string>',
'Key' => '<OBJECT_NAME>', // REQUIRED
'MultipartUpload' => [
'Parts' => [
[
'ETag' => '<string>',
'PartNumber' => 0,
],
],
],
'RequestPayer' => 'requester',
'UploadId' => '<string>', // REQUIRED
]);
var_dump($result);
} catch (AwsException $e) {
// Display error message
echo $e->getMessage();
echo "\n";
}
import boto3
import logging
from botocore.exceptions import ClientError
logging.basicConfig(level=logging.INFO)
try:
s3_client = boto3.client(
's3',
endpoint_url='<ENDPOINT>',
aws_access_key_id='<ACCESS-KEY>',
aws_secret_access_key='<SECRET-KEY>'
)
except Exception as exc:
logging.error(exc)
else:
try:
response = s3_client.complete_multipart_upload(
Bucket='<BUCKET_NAME>',
Key='<OBJECT_NAME>',
MultipartUpload={
'Parts': [
{
'ETag': 'string',
'PartNumber': 123
},
]
},
UploadId='string',
RequestPayer='requester',
# ExpectedBucketOwner='string'
)
logging.info(response)
except ClientError as exc:
logging.error(exc)
// Import required AWS SDK clients and commands for Node.js
const { S3Client, CompleteMultipartUploadCommand } = require("@aws-sdk/client-s3");
// Create an S3 client service object
const s3 = new S3Client({
region: "default",
endpoint: "<ENDPOINT_URL>",
credentials: {
accessKeyId: "<ACCESS_KEY>",
secretAccessKey:
"<SECRET_KEY>",
},
});
const BUCKET_NAME = "<BUCKET_NAME>";
const run = async () => {
try {
const response = await s3.send(
new CompleteMultipartUploadCommand({
Bucket: BUCKET_NAME,
Key: "<OBJECT_NAME>",
UploadId: "ID",
MultipartUpload: {
Parts: [
{
ETag: "string",
PartNumber: 123,
},
],
},
})
);
console.log("Success", response);
} catch (err) {
console.log("Error", err);
}
};
run();
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"fmt"
"os"
)
// Completes multipart upload
//
// Usage:
// go run s3_complete_multipart_upload.go BUCKET OBJECT
func main() {
if len(os.Args) != 3 {
exitErrorf("Bucket and object names required\nUsage: go run", os.Args[0], "BUCKET OBJECT")
}
bucket := os.Args[1]
key := os.Args[2]
// Initialize a session in us-west-2 that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
sess, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials("<ACCESS-KEY>", "<SECRET-KEY>", ""),
})
svc := s3.New(sess, &aws.Config{
Region: aws.String("default"),
Endpoint: aws.String("<ENDPOINT>"),
})
params := &s3.CompleteMultipartUploadInput{
Bucket: &bucket,
Key: &key,
MultipartUpload: &s3.CompletedMultipartUpload{
Parts: []*s3.CompletedPart{
{
ETag: aws.String("\"d8c2eafd90c266e19ab9dcacc479f8af\""),
PartNumber: aws.Int64(1),
},
{
ETag: aws.String("\"d8c2eafd90c266e19ab9dcacc479f8af\""),
PartNumber: aws.Int64(2),
},
},
},
UploadId: aws.String("example7YPBOJuoFiQ9cz4P3Pe6FIZwO4f7wN93uHsNBEw97pl5eNwzExg0LAT2dUN91cOmrEQHDsP3WA60CEg--"),
}
// Complete multipart upload
result, err := svc.CompleteMultipartUpload(params)
if err != nil {
exitErrorf(err.Error())
}
fmt.Println(result)
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
The following command can be used to execute the aforementioned code, presuming the code file is called s3_complete_multipart_upload.go:
go run s3_complete_multipart_upload.go BUCKET_NAME OBJECT_NAME