Skip to content

Commit c298279

Browse files
authored
Merge pull request #1922 from irenepsmith/update-six-s3-examples
Updated six S3 examples.
2 parents 74bc078 + aee887d commit c298279

File tree

18 files changed

+1005
-0
lines changed

18 files changed

+1005
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30114.105
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbortMPUExample", "AbortMPUExample\AbortMPUExample.csproj", "{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Debug|x64.ActiveCfg = Debug|Any CPU
24+
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Debug|x64.Build.0 = Debug|Any CPU
25+
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Debug|x86.ActiveCfg = Debug|Any CPU
26+
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Debug|x86.Build.0 = Debug|Any CPU
27+
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Release|x64.ActiveCfg = Release|Any CPU
30+
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Release|x64.Build.0 = Release|Any CPU
31+
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Release|x86.ActiveCfg = Release|Any CPU
32+
{D2CDB230-35BD-4079-A45F-A090D6B3CC6C}.Release|x86.Build.0 = Release|Any CPU
33+
EndGlobalSection
34+
EndGlobal
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
namespace AbortMPUExample
5+
{
6+
using System;
7+
using System.Threading.Tasks;
8+
using Amazon.S3;
9+
using Amazon.S3.Transfer;
10+
11+
/// <summary>
12+
/// This example shows how to use the Amazon Simple Storage Service
13+
/// (Amazon S3) to stop a multi-part upload process using the Amazon S3
14+
/// TransferUtility. The example was created using the AWS SDK for .NET
15+
/// version 3.7 and .NET Core 5.0.
16+
/// </summary>
17+
public class AbortMPU
18+
{
19+
public static async Task Main()
20+
{
21+
string bucketName = "doc-example-bucket";
22+
23+
// If the AWS Region defined for your default user is different
24+
// from the Region where your Amazon S3 bucket is located,
25+
// pass the Region name to the S3 client object's constructor.
26+
// For example: RegionEndpoint.USWest2.
27+
IAmazonS3 client = new AmazonS3Client();
28+
29+
await AbortMPUAsync(client, bucketName);
30+
}
31+
32+
/// <summary>
33+
/// Cancels the multi-part copy process.
34+
/// </summary>
35+
/// <param name="client">The initialized client object used to create
36+
/// the TransferUtility object.</param>
37+
/// <param name="bucketName">The name of the S3 bucket where the
38+
/// multi-part copy operation is in progress.</param>
39+
public static async Task AbortMPUAsync(IAmazonS3 client, string bucketName)
40+
{
41+
try
42+
{
43+
var transferUtility = new TransferUtility(client);
44+
45+
// Cancel all in-progress uploads initiated before the specified date.
46+
await transferUtility.AbortMultipartUploadsAsync(
47+
bucketName, DateTime.Now.AddDays(-7));
48+
}
49+
catch (AmazonS3Exception e)
50+
{
51+
Console.WriteLine($"Error: {e.Message}");
52+
}
53+
}
54+
55+
}
56+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="AWSSDK.Core" Version="3.7.0.44" />
10+
<PackageReference Include="AWSSDK.S3" Version="3.7.1.14" />
11+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
<PrivateAssets>all</PrivateAssets>
14+
</PackageReference>
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30114.105
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SSECLowLevelMPUcopyObjectExample", "SSECLowLevelMPUcopyObjectExample\SSECLowLevelMPUcopyObjectExample.csproj", "{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Debug|x64.ActiveCfg = Debug|Any CPU
24+
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Debug|x64.Build.0 = Debug|Any CPU
25+
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Debug|x86.ActiveCfg = Debug|Any CPU
26+
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Debug|x86.Build.0 = Debug|Any CPU
27+
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Release|x64.ActiveCfg = Release|Any CPU
30+
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Release|x64.Build.0 = Release|Any CPU
31+
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Release|x86.ActiveCfg = Release|Any CPU
32+
{F04B7F93-4FD8-4F6B-B72E-01C12E5F8344}.Release|x86.Build.0 = Release|Any CPU
33+
EndGlobalSection
34+
EndGlobal
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
namespace SSECLowLevelMPUcopyObjectExample
5+
{
6+
using System;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
using System.Security.Cryptography;
10+
using System.Threading.Tasks;
11+
using Amazon.S3;
12+
using Amazon.S3.Model;
13+
14+
/// <summary>
15+
/// Uses the Amazon Simple Storage Service (Amazon S3) low level API to
16+
/// perform a multi-part upload to an Amazon S3 bucket. The example was
17+
/// created using the AWS SDK for .NET version 3.7 and .NET Core 5.0.
18+
/// </summary>
19+
public class SSECLowLevelMPUcopyObject
20+
{
21+
public static async Task Main()
22+
{
23+
string existingBucketName = "doc-example-bucket";
24+
string sourceKeyName = "sample_file.txt";
25+
string targetKeyName = "sample_file_copy.txt";
26+
string filePath = $"sample\\{targetKeyName}";
27+
28+
// If the AWS Region defined for your default user is different
29+
// from the Region where your Amazon S3 bucket is located,
30+
// pass the Region name to the S3 client object's constructor.
31+
// For example: RegionEndpoint.USWest2.
32+
IAmazonS3 client = new AmazonS3Client();
33+
34+
// Create the encryption key.
35+
var base64Key = CreateEncryptionKey();
36+
37+
await CreateSampleObjUsingClientEncryptionKeyAsync(
38+
client, existingBucketName,
39+
sourceKeyName, filePath, base64Key);
40+
}
41+
42+
/// <summary>
43+
/// Creates the encryption key to use with the multi-part upload.
44+
/// </summary>
45+
/// <returns>A string containing the base64-encoded key for encrypting
46+
/// the multi-part upload.</returns>
47+
public static string CreateEncryptionKey()
48+
{
49+
Aes aesEncryption = Aes.Create();
50+
aesEncryption.KeySize = 256;
51+
aesEncryption.GenerateKey();
52+
string base64Key = Convert.ToBase64String(aesEncryption.Key);
53+
return base64Key;
54+
}
55+
56+
/// <summary>
57+
/// Creates and uploads an object using a multi-part upload.
58+
/// </summary>
59+
/// <param name="client">The initialized Amazon S3 object used to
60+
/// initialize and perform the multi-part upload.</param>
61+
/// <param name="existingBucketName">The name of the bucket to which
62+
/// the object will be uploaded.</param>
63+
/// <param name="sourceKeyName">The source object name.</param>
64+
/// <param name="filePath">The location of the source object.</param>
65+
/// <param name="base64Key">The encryption key to use with the upload.</param>
66+
public static async Task CreateSampleObjUsingClientEncryptionKeyAsync(
67+
IAmazonS3 client,
68+
string existingBucketName,
69+
string sourceKeyName,
70+
string filePath,
71+
string base64Key)
72+
{
73+
List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>();
74+
75+
InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest
76+
{
77+
BucketName = existingBucketName,
78+
Key = sourceKeyName,
79+
ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256,
80+
ServerSideEncryptionCustomerProvidedKey = base64Key,
81+
};
82+
83+
InitiateMultipartUploadResponse initResponse =
84+
await client.InitiateMultipartUploadAsync(initiateRequest);
85+
86+
long contentLength = new FileInfo(filePath).Length;
87+
long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB
88+
89+
try
90+
{
91+
long filePosition = 0;
92+
for (int i = 1; filePosition < contentLength; i++)
93+
{
94+
UploadPartRequest uploadRequest = new UploadPartRequest
95+
{
96+
BucketName = existingBucketName,
97+
Key = sourceKeyName,
98+
UploadId = initResponse.UploadId,
99+
PartNumber = i,
100+
PartSize = partSize,
101+
FilePosition = filePosition,
102+
FilePath = filePath,
103+
ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256,
104+
ServerSideEncryptionCustomerProvidedKey = base64Key,
105+
};
106+
107+
// Upload part and add response to our list.
108+
uploadResponses.Add(await client.UploadPartAsync(uploadRequest));
109+
110+
filePosition += partSize;
111+
}
112+
113+
CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest
114+
{
115+
BucketName = existingBucketName,
116+
Key = sourceKeyName,
117+
UploadId = initResponse.UploadId,
118+
119+
};
120+
completeRequest.AddPartETags(uploadResponses);
121+
122+
CompleteMultipartUploadResponse completeUploadResponse =
123+
await client.CompleteMultipartUploadAsync(completeRequest);
124+
}
125+
catch (Exception exception)
126+
{
127+
Console.WriteLine($"Exception occurred: {exception.Message}");
128+
129+
// If there was an error, abort the multi-part upload.
130+
AbortMultipartUploadRequest abortMPURequest = new AbortMultipartUploadRequest
131+
{
132+
BucketName = existingBucketName,
133+
Key = sourceKeyName,
134+
UploadId = initResponse.UploadId,
135+
};
136+
137+
await client.AbortMultipartUploadAsync(abortMPURequest);
138+
}
139+
}
140+
}
141+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="AWSSDK.Core" Version="3.7.0.40" />
10+
<PackageReference Include="AWSSDK.S3" Version="3.7.1.10" />
11+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
<PrivateAssets>all</PrivateAssets>
14+
</PackageReference>
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30114.105
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SSEClientEncryptionExample", "SSEClientEncryptionExample\SSEClientEncryptionExample.csproj", "{47C05345-FF1B-43B6-9264-53B5CEE72BC2}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Debug|x64.ActiveCfg = Debug|Any CPU
24+
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Debug|x64.Build.0 = Debug|Any CPU
25+
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Debug|x86.ActiveCfg = Debug|Any CPU
26+
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Debug|x86.Build.0 = Debug|Any CPU
27+
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Release|x64.ActiveCfg = Release|Any CPU
30+
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Release|x64.Build.0 = Release|Any CPU
31+
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Release|x86.ActiveCfg = Release|Any CPU
32+
{47C05345-FF1B-43B6-9264-53B5CEE72BC2}.Release|x86.Build.0 = Release|Any CPU
33+
EndGlobalSection
34+
EndGlobal

0 commit comments

Comments
 (0)