Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaFinkelshteyn committed Jan 15, 2019
1 parent df2cc05 commit 6305e4f
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 0 deletions.
25 changes: 25 additions & 0 deletions S3Download.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2026
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "S3Download", "S3Download\S3Download.csproj", "{F040D9DE-C2D0-4242-B3C5-6CA7A50817F6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F040D9DE-C2D0-4242-B3C5-6CA7A50817F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F040D9DE-C2D0-4242-B3C5-6CA7A50817F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F040D9DE-C2D0-4242-B3C5-6CA7A50817F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F040D9DE-C2D0-4242-B3C5-6CA7A50817F6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {24750F92-F815-41B1-A95B-7CA2567EFF58}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions S3Download/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
122 changes: 122 additions & 0 deletions S3Download/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace S3Download
{
class TrackMPUUsingHighLevelAPITest
{
private static IAmazonS3 s3Client;

private static ConcurrentDictionary<string, string> progressList = new ConcurrentDictionary<string, string>();
private static int _cursorTop = 0;

public static void Main(string[] args)
{
if (args == null || args.Length != 6)
{
Console.WriteLine("Usage: S3Download.exe <bucketName> <comma-separated-source-files-list-or-single-file> " +
"<comma-separated-target-folders-list-or-single-folder>" +
" <accessKeyId> <secretAccessKey> <serviceURL>");
return;
}

_cursorTop = Console.CursorTop;

string bucketName = args[0];
var files = args[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToArray();
var folders = args[2].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToArray();
string accessKeyId = args[3];
string secretAccessKey = args[4];
string serviceURL = args[5];

AWSCredentials сredentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);

AmazonS3Config config = new AmazonS3Config()
{
ServiceURL = serviceURL,
UseHttp = true,
MaxErrorRetry = 5
};

s3Client = new AmazonS3Client(сredentials, config);

var watch = new Stopwatch();
watch.Start();

List<Task> TaskList = new List<Task>();
foreach (var file in files)
{
foreach (var folder in folders)
{
var target = Path.Combine(folder, file);
if (File.Exists(target))
{
Console.WriteLine(target + "already exists, skipping...");
_cursorTop = Console.CursorTop;
}
else
{
TaskList.Add(DownloadAsync(bucketName, file, Path.Combine(folder, file)));
}
}
}

Task.WaitAll(TaskList.ToArray());
watch.Stop();
Console.CursorTop = _cursorTop + TaskList.Count() + 1;
Console.WriteLine($"Completed in {watch.Elapsed.Hours:D2}:{watch.Elapsed.Minutes:D2}:{watch.Elapsed.Seconds:D2}");
}

private static async Task DownloadAsync(string bucketName, string keyName, string filePath)
{
try
{
var fileTransferUtility = new TransferUtility(s3Client);

var downloadRequest =
new TransferUtilityDownloadRequest
{
BucketName = bucketName,
FilePath = filePath,
Key = keyName
};

downloadRequest.WriteObjectProgressEvent += new EventHandler<WriteObjectProgressArgs>(DownloadPartProgressEvent);
await fileTransferUtility.DownloadAsync(downloadRequest);
Console.WriteLine("Upload completed");
}
catch (AmazonS3Exception e)
{
Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
}
}

static void DownloadPartProgressEvent(object sender, WriteObjectProgressArgs e)
{
int cursorTop = _cursorTop;
progressList[e.FilePath] = e.IsCompleted ? "Completed".PadRight(60) : $"{e.TransferredBytes:N}/{e.TotalBytes:N} bytes ({e.PercentDone.ToString()}% done)";
foreach (var key in progressList.Keys)
{
Console.CursorTop = cursorTop;
Console.CursorVisible = false;
Console.WriteLine($"{key}: {progressList[key]}");
cursorTop++;
}
}
}
}
36 changes: 36 additions & 0 deletions S3Download/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("S3Download")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("S3Download")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("f040d9de-c2d0-4242-b3c5-6ca7a50817f6")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
63 changes: 63 additions & 0 deletions S3Download/S3Download.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F040D9DE-C2D0-4242-B3C5-6CA7A50817F6}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>S3Download</RootNamespace>
<AssemblyName>S3Download</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<UseVSHostingProcess>true</UseVSHostingProcess>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AWSSDK.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
<HintPath>..\packages\AWSSDK.Core.3.3.29.12\lib\net45\AWSSDK.Core.dll</HintPath>
</Reference>
<Reference Include="AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
<HintPath>..\packages\AWSSDK.S3.3.3.29\lib\net45\AWSSDK.S3.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\AWSSDK.S3.3.3.29\analyzers\dotnet\cs\AWSSDK.S3.CodeAnalysis.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
5 changes: 5 additions & 0 deletions S3Download/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AWSSDK.Core" version="3.3.29.12" targetFramework="net461" />
<package id="AWSSDK.S3" version="3.3.29" targetFramework="net461" />
</packages>
15 changes: 15 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 1.0.{build}
skip_tags: true
image: Visual Studio 2017
configuration: Release
before_build:
- cmd: nuget restore
build:
verbosity: minimal
artifacts:
- path: S3Download\bin\$(configuration)
name: S3Download
deploy:
- provider: GitHub
auth_token:
secure: KmYWY9jz5FpXpKkxpQvthnDkZiMCi0tmh5PCs/83mu4vm0QsEThuEqhEgMViCbCJ

0 comments on commit 6305e4f

Please sign in to comment.