Skip to content

Instantly share code, notes, and snippets.

@Varriount
Forked from Gooseus/s3_get_bucket_obj.nim
Last active October 25, 2017 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Varriount/e8f0f4fb02d8596b7caa2596c66ffc62 to your computer and use it in GitHub Desktop.
Save Varriount/e8f0f4fb02d8596b7caa2596c66ffc62 to your computer and use it in GitHub Desktop.
Testing async dispatch and http requests in Nim
# testing async dispatch in pursuit of an efficient async s3 interface
import os, times, math, httpclient, asyncdispatch, asyncfile
let t0 = epochTime()
proc dt() : float =
round(epochTime() - t0,6)
const aws_url = "http://localhost:1234/"
proc s3_get_bucket_object(aws_key:string,bucket:string,obj:string) : Future[string] {.async.} =
var
client = newAsyncHttpClient()
c_prog = false
# do the HMAC here
client.headers = newHttpHeaders({ "Authorization": "TEST KEY:SIG-"&obj })
client.onProgressChanged = proc(total, progress, speed: BiggestInt) {.async.} =
#echo("S3 download progress: ", progress, " of ", total)
#echo("S3 transfer rate: ", speed div 1000, "kb/s")
c_prog = true
try:
result = await client.getContent(aws_url)
except HttpRequestError:
echo "http error: "
echo getCurrentExceptionMsg()
except:
echo "unknown exception"
echo getCurrentExceptionMsg()
proc download_s3_file(aws_key, bucket, obj, output_path: string): bool {.async.} =
let data = s3_get_bucket_object(aws_key, bucket, obj)
var file = openAsync(output_path, fmReadWrite)
await file.write(data)
result = True
let bucket = "my_bucket"
var files: seq[Future[bool]] = @[]
for f in 0..99:
let file_name = "./some_file_" & $(f+1) & ".txt"
files.add(download_s3_file("blah", bucket, file_name, file_name)))
for file in files:
waitFor(file)
echo "Transfer Complete ", dt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment