Skip to content

Instantly share code, notes, and snippets.

@jamesdavidson
Last active May 27, 2024 07:17
Show Gist options
  • Save jamesdavidson/58adb94129fe5b1c3832eed9014923e8 to your computer and use it in GitHub Desktop.
Save jamesdavidson/58adb94129fe5b1c3832eed9014923e8 to your computer and use it in GitHub Desktop.
Alternative to getting started for Clojure CLR with a socket REPL and sending a SigV4 signed request using an extension method
(ns get_from_s3
(:import [Amazon.Runtime EnvironmentVariablesAWSCredentials SessionAWSCredentials]
[System.Net.Http HttpClient GetAsyncExtensions HttpCompletionOption]
[System Uri])
;(:use clojure.pprint clojure.repl clojure.reflect)
(:require [clojure.clr.io :as io]))
;(assembly-load "AWSSDK.Core")
;(assembly-load "AwsSignatureVersion4")
(def client (new HttpClient))
(def creds (new EnvironmentVariablesAWSCredentials)) ; or new SessionAWSCredentials(key,secret,token)
(def obj-uri (new Uri "https://batch-jobs-dev.s3.us-east-2.amazonaws.com/manifests/manifest-1716540014.csv.gz"))
(with-open [os (io/output-stream "output.csv.gz")]
(let [res (.Result (GetAsyncExtensions/GetAsync client obj-uri HttpCompletionOption/ResponseHeadersRead "us-east-2" "s3" creds))]
(-> res .Content (.CopyToAsync os) .Result)))
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AwsSignatureVersion4" Version="4.0.1" />
<PackageReference Include="Clojure" Version="1.12.0-alpha8" />
</ItemGroup>
</Project>
// dotnet --version
// 7.0.100
// dotnet restore
// dotnet run
// rlwrap nc localhost 5555
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using clojure.lang;
// based on https://github.com/Dangercoder/potion/blob/master/src/NRepl.cs
var REQUIRE = RT.var("clojure.core", "require");
var LOAD = RT.var("clojure.core", "load");
Console.WriteLine("Loading Clojure then socket REPL");
RT.Init();
string ns;
ns = "clojure.tools.reader.edn";
Assembly.Load("clojure.tools.reader");
REQUIRE.invoke(Symbol.intern(ns));
var fn = RT.var(ns, "read-string");
var p = fn.invoke("{:name \"repl\" :port 5555 :accept clojure.core.server/repl :server-daemon true}");
Console.WriteLine(p.ToString());
ns = "clojure.core.server";
REQUIRE.invoke(Symbol.intern(ns));
RT.var(ns, "start-server").invoke(p);
// https://stackoverflow.com/a/13899429/4305877
var exitEvent = new ManualResetEvent(false);
Console.CancelKeyPress += (sender, eventArgs) => {
eventArgs.Cancel = true;
exitEvent.Set();
};
exitEvent.WaitOne();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment