Skip to content

Updated six S3 examples. #1922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions dotnetv3/S3/AbortMPUExample/AbortMPUExample.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbortMPUExample", "AbortMPUExample\AbortMPUExample.csproj", "{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Debug|x64.ActiveCfg = Debug|Any CPU
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Debug|x64.Build.0 = Debug|Any CPU
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Debug|x86.ActiveCfg = Debug|Any CPU
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Debug|x86.Build.0 = Debug|Any CPU
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Release|Any CPU.Build.0 = Release|Any CPU
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Release|x64.ActiveCfg = Release|Any CPU
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Release|x64.Build.0 = Release|Any CPU
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Release|x86.ActiveCfg = Release|Any CPU
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
56 changes: 56 additions & 0 deletions dotnetv3/S3/AbortMPUExample/AbortMPUExample/AbortMPU.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

namespace AbortMPUExample
{
using System;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Transfer;

/// <summary>
/// This example shows how to use the Amazon Simple Storage Service
/// (Amazon S3) to stop a multi-part upload process using the Amazon S3
/// TransferUtility. The example was created using the AWS SDK for .NET
/// version 3.7 and .NET Core 5.0.
/// </summary>
public class AbortMPU
{
public static async Task Main()
{
string bucketName = "doc-example-bucket";

// If the AWS Region defined for your default user is different
// from the Region where your Amazon S3 bucket is located,
// pass the Region name to the S3 client object's constructor.
// For example: RegionEndpoint.USWest2.
IAmazonS3 client = new AmazonS3Client();

await AbortMPUAsync(client, bucketName);
}

/// <summary>
/// Cancels the multi-part copy process.
/// </summary>
/// <param name="client">The initialized client object used to create
/// the TransferUtility object.</param>
/// <param name="bucketName">The name of the S3 bucket where the
/// multi-part copy operation is in progress.</param>
public static async Task AbortMPUAsync(IAmazonS3 client, string bucketName)
{
try
{
var transferUtility = new TransferUtility(client);

// Cancel all in-progress uploads initiated before the specified date.
await transferUtility.AbortMultipartUploadsAsync(
bucketName, DateTime.Now.AddDays(-7));
}
catch (AmazonS3Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
}

}
}
17 changes: 17 additions & 0 deletions dotnetv3/S3/AbortMPUExample/AbortMPUExample/AbortMPUExample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="3.7.0.44" />
<PackageReference Include="AWSSDK.S3" Version="3.7.1.14" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SSECLowLevelMPUcopyObjectExample", "SSECLowLevelMPUcopyObjectExample\SSECLowLevelMPUcopyObjectExample.csproj", "{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Debug|x64.ActiveCfg = Debug|Any CPU
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Debug|x64.Build.0 = Debug|Any CPU
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Debug|x86.ActiveCfg = Debug|Any CPU
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Debug|x86.Build.0 = Debug|Any CPU
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Release|Any CPU.Build.0 = Release|Any CPU
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Release|x64.ActiveCfg = Release|Any CPU
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Release|x64.Build.0 = Release|Any CPU
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Release|x86.ActiveCfg = Release|Any CPU
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

namespace SSECLowLevelMPUcopyObjectExample
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Model;

/// <summary>
/// Uses the Amazon Simple Storage Service (Amazon S3) low level API to
/// perform a multi-part upload to an Amazon S3 bucket. The example was
/// created using the AWS SDK for .NET version 3.7 and .NET Core 5.0.
/// </summary>
public class SSECLowLevelMPUcopyObject
{
public static async Task Main()
{
string existingBucketName = "doc-example-bucket";
string sourceKeyName = "sample_file.txt";
string targetKeyName = "sample_file_copy.txt";
string filePath = $"sample\\{targetKeyName}";

// If the AWS Region defined for your default user is different
// from the Region where your Amazon S3 bucket is located,
// pass the Region name to the S3 client object's constructor.
// For example: RegionEndpoint.USWest2.
IAmazonS3 client = new AmazonS3Client();

// Create the encryption key.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't add value.

var base64Key = CreateEncryptionKey();

await CreateSampleObjUsingClientEncryptionKeyAsync(
client, existingBucketName,
sourceKeyName, filePath, base64Key);
}

/// <summary>
/// Creates the encryption key to use with the multi-part upload.
/// </summary>
/// <returns>A string containing the base64-encoded key for encrypting
/// the multi-part upload.</returns>
public static string CreateEncryptionKey()
{
Aes aesEncryption = Aes.Create();
aesEncryption.KeySize = 256;
aesEncryption.GenerateKey();
string base64Key = Convert.ToBase64String(aesEncryption.Key);
return base64Key;
}

/// <summary>
/// Creates and uploads an object using a multi-part upload.
/// </summary>
/// <param name="client">The initialized Amazon S3 object used to
/// initialize and perform the multi-part upload.</param>
/// <param name="existingBucketName">The name of the bucket to which
/// the object will be uploaded.</param>
/// <param name="sourceKeyName">The source object name.</param>
/// <param name="filePath">The location of the source object.</param>
/// <param name="base64Key">The encryption key to use with the upload.</param>
public static async Task CreateSampleObjUsingClientEncryptionKeyAsync(
IAmazonS3 client,
string existingBucketName,
string sourceKeyName,
string filePath,
string base64Key)
{
List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>();

InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest
{
BucketName = existingBucketName,
Key = sourceKeyName,
ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256,
ServerSideEncryptionCustomerProvidedKey = base64Key,
};

InitiateMultipartUploadResponse initResponse =
await client.InitiateMultipartUploadAsync(initiateRequest);

long contentLength = new FileInfo(filePath).Length;
long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB

try
{
long filePosition = 0;
for (int i = 1; filePosition < contentLength; i++)
{
UploadPartRequest uploadRequest = new UploadPartRequest
{
BucketName = existingBucketName,
Key = sourceKeyName,
UploadId = initResponse.UploadId,
PartNumber = i,
PartSize = partSize,
FilePosition = filePosition,
FilePath = filePath,
ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256,
ServerSideEncryptionCustomerProvidedKey = base64Key,
};

// Upload part and add response to our list.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the list

uploadResponses.Add(await client.UploadPartAsync(uploadRequest));

filePosition += partSize;
}

CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest
{
BucketName = existingBucketName,
Key = sourceKeyName,
UploadId = initResponse.UploadId,

};
completeRequest.AddPartETags(uploadResponses);

CompleteMultipartUploadResponse completeUploadResponse =
await client.CompleteMultipartUploadAsync(completeRequest);
}
catch (Exception exception)
{
Console.WriteLine($"Exception occurred: {exception.Message}");

// If there was an error, abort the multi-part upload.
AbortMultipartUploadRequest abortMPURequest = new AbortMultipartUploadRequest
{
BucketName = existingBucketName,
Key = sourceKeyName,
UploadId = initResponse.UploadId,
};

await client.AbortMultipartUploadAsync(abortMPURequest);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="3.7.0.40" />
<PackageReference Include="AWSSDK.S3" Version="3.7.1.10" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SSEClientEncryptionExample", "SSEClientEncryptionExample\SSEClientEncryptionExample.csproj", "{47C05345-FF1B-43B6-9264-53B5CEE72BC2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Debug|x64.ActiveCfg = Debug|Any CPU
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Debug|x64.Build.0 = Debug|Any CPU
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Debug|x86.ActiveCfg = Debug|Any CPU
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Debug|x86.Build.0 = Debug|Any CPU
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Release|Any CPU.Build.0 = Release|Any CPU
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Release|x64.ActiveCfg = Release|Any CPU
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Release|x64.Build.0 = Release|Any CPU
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Release|x86.ActiveCfg = Release|Any CPU
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Loading