Skip to content

Commit 2bc738d

Browse files
author
EC2 Default User
committed
initial commit
0 parents  commit 2bc738d

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed

.gitignore

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
*.swp
2+
*.*~
3+
project.lock.json
4+
.DS_Store
5+
*.pyc
6+
nupkg/
7+
8+
# Visual Studio Code
9+
.vscode
10+
11+
# Rider
12+
.idea
13+
14+
# User-specific files
15+
*.suo
16+
*.user
17+
*.userosscache
18+
*.sln.docstates
19+
20+
# Build results
21+
[Dd]ebug/
22+
[Dd]ebugPublic/
23+
[Rr]elease/
24+
[Rr]eleases/
25+
x64/
26+
x86/
27+
build/
28+
bld/
29+
[Bb]in/
30+
[Oo]bj/
31+
[Oo]ut/
32+
msbuild.log
33+
msbuild.err
34+
msbuild.wrn
35+
36+
# Visual Studio 2015
37+
.vs/

MultiPartHelper.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Amazon;
2+
using Amazon.S3;
3+
using Amazon.S3.Transfer;
4+
using System;
5+
using System.IO;
6+
using System.Threading.Tasks;
7+
8+
namespace S3
9+
{
10+
public static class MultiPartHelper
11+
{
12+
// Specify your bucket region (an example region is shown).
13+
private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USEast1;
14+
private static IAmazonS3 s3Client;
15+
16+
17+
public static async Task UploadFileAsync(string bucketName, string keyName, string filePath)
18+
{
19+
try
20+
{
21+
var fileTransferUtility =
22+
new TransferUtility(s3Client);
23+
24+
// Option 1. Upload a file. The file name is used as the object key name.
25+
await fileTransferUtility.UploadAsync(filePath, bucketName);
26+
Console.WriteLine("Upload 1 completed");
27+
28+
// Option 2. Specify object key name explicitly.
29+
await fileTransferUtility.UploadAsync(filePath, bucketName, keyName);
30+
Console.WriteLine("Upload 2 completed");
31+
32+
// Option 3. Upload data from a type of System.IO.Stream.
33+
using (var fileToUpload =
34+
new FileStream(filePath, FileMode.Open, FileAccess.Read))
35+
{
36+
await fileTransferUtility.UploadAsync(fileToUpload,
37+
bucketName, keyName);
38+
}
39+
Console.WriteLine("Upload 3 completed");
40+
41+
// Option 4. Specify advanced settings.
42+
var fileTransferUtilityRequest = new TransferUtilityUploadRequest
43+
{
44+
BucketName = bucketName,
45+
FilePath = filePath,
46+
StorageClass = S3StorageClass.StandardInfrequentAccess,
47+
PartSize = 6291456, // 6 MB.
48+
Key = keyName,
49+
CannedACL = S3CannedACL.PublicRead
50+
};
51+
fileTransferUtilityRequest.Metadata.Add("param1", "Value1");
52+
fileTransferUtilityRequest.Metadata.Add("param2", "Value2");
53+
54+
await fileTransferUtility.UploadAsync(fileTransferUtilityRequest);
55+
Console.WriteLine("Upload 4 completed");
56+
}
57+
catch (AmazonS3Exception e)
58+
{
59+
Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
60+
}
61+
catch (Exception e)
62+
{
63+
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
64+
}
65+
66+
}
67+
}
68+
}

Program.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Amazon;
2+
using Amazon.S3;
3+
using Amazon.S3.Model;
4+
using Amazon.S3.Util;
5+
using System;
6+
using System.Threading.Tasks;
7+
8+
namespace S3
9+
{
10+
class Program
11+
{
12+
async static Task Main(string[] args)
13+
{
14+
if (args.Length < 3)
15+
{
16+
Console.WriteLine("Usage: <the AWS Region to use> <source bucket name> <target bucket name>");
17+
Console.WriteLine("Example: us-east-1 my-source-bucket my-target-bucket");
18+
return;
19+
}
20+
21+
if (args[0] != "us-east-1")
22+
{
23+
Console.WriteLine("Cannot continue. The only supported AWS Region ID is " +
24+
"'us-east-1'.");
25+
return;
26+
}
27+
28+
var bucketRegion = RegionEndpoint.USEast1;
29+
var source_bucket_name = args[1];
30+
var target_bucket_name = args[2];
31+
32+
using (var s3Client = new AmazonS3Client(bucketRegion))
33+
{
34+
35+
36+
}
37+
38+
}
39+
}
40+
}

Readme.md

Whitespace-only changes.

s3.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="AWSSDK.S3" Version="3.7.9.21" />
12+
</ItemGroup>
13+
14+
</Project>

0 commit comments

Comments
 (0)