-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Updated six S3 examples. #1922
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
dotnetv3/S3/AbortMPUExample/AbortMPUExample/AbortMPUExample.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
34 changes: 34 additions & 0 deletions
34
dotnetv3/S3/SSECLowLevelMPUcopyObjectExample/SSECLowLevelMPUcopyObjectExample.sln
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
141 changes: 141 additions & 0 deletions
141
...owLevelMPUcopyObjectExample/SSECLowLevelMPUcopyObjectExample/SSECLowLevelMPUcopyObject.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...opyObjectExample/SSECLowLevelMPUcopyObjectExample/SSECLowLevelMPUcopyObjectExample.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
34 changes: 34 additions & 0 deletions
34
dotnetv3/S3/SSEClientEncryptionExample/SSEClientEncryptionExample.sln
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.