Skip to content

Commit

Permalink
correctly defaulting aws region (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
miki725 authored Jan 26, 2024
1 parent f183a3b commit 4ac970d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 26 deletions.
4 changes: 3 additions & 1 deletion nimutils/awsclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
* a request proc which takes an AwsClient and the request params to handle Sigv4 signing and async dispatch
]#

import times, tables, unicode, uri
import times, tables, unicode, uri, std/envvars
import strutils except toLower
import httpclient
import sigv4, net

export sigv4.AwsCredentials, sigv4.AwsScope

let defaultRegion* = getEnv("AWS_REGION", getEnv("AWS_DEFAULT_REGION", "us-east-1"))

type
EAWSCredsMissing = object of IOError
AwsRequest* = tuple
Expand Down
7 changes: 2 additions & 5 deletions nimutils/s3client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
]#

import strutils except toLower
import times, unicode, tables, httpclient, xmlparser, xmltree, uri, std/[envvars]
import times, unicode, tables, httpclient, xmlparser, xmltree, uri
import awsclient
export awsclient

const
awsURI = "https://amazonaws.com"

let
defRegion = getEnv("AWS_DEFAULT_REGION", "us-east-1")

type
S3Client* = object of AwsClient

Expand All @@ -28,7 +25,7 @@ type
etag*: string
size*: int

proc newS3Client*(creds: AwsCredentials, region: string = defRegion,
proc newS3Client*(creds: AwsCredentials, region: string = defaultRegion,
host: string = awsURI): S3Client =
let
# TODO - use some kind of template and compile-time variable to put the correct kernel used to build the sdk in the UA?
Expand Down
21 changes: 6 additions & 15 deletions nimutils/sinks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -258,26 +258,17 @@ type S3SinkState* = ref object of RootRef

proc s3SinkInit(cfg: SinkConfig): bool =
try:
var region, extra: string
let
uri = parseURI(cfg.params["uri"])
bucket = uri.hostname
uid = cfg.params["uid"]
secret = cfg.params["secret"]
token = cfg.params.getOrDefault("token", "")
region = cfg.params.getOrDefault("region", defaultRegion)
extra = cfg.params.getOrDefault("extra", "")
baseObj = uri.path[1 .. ^1] # Strip the leading /
(objPath, nameBase) = splitPath(baseObj)

if "region" in cfg.params:
region = cfg.params["region"]
else:
region = "us-east-1"

if "extra" in cfg.params:
extra = cfg.params["extra"]
else:
extra = ""

cfg.private = S3SinkState(region: region, uri: uri, uid: uid,
secret: secret, token: token,
bucket: bucket, objPath: objPath,
Expand Down Expand Up @@ -308,8 +299,8 @@ proc s3SinkOut(msg: string, cfg: SinkConfig, t: Topic, ignored: StringTable) =
newPath = joinPath(state.objPath, newTail)
response = client.put_object(state.bucket, newPath, msg)

if response.status[0] != '2':
raise newException(ValueError, response.status)
if not response.code.is2xx():
raise newException(ValueError, response.status & ": " & response.body())
else:
cfg.iolog(t, "Post to: " & newPath & "; response = " & response.status)

Expand Down Expand Up @@ -398,7 +389,7 @@ proc postSinkOut(msg: string, cfg: SinkConfig, t: Topic, ignored: StringTable) =
firstRetryDelayMs = 100)

if not response.code.is2xx():
raise newException(ValueError, response.status)
raise newException(ValueError, response.status & ": " & response.body())

cfg.iolog(t, "Post " & response.status)

Expand Down Expand Up @@ -440,7 +431,7 @@ proc presignSinkOut(msg: string, cfg: SinkConfig, t: Topic, ignored: StringTable
firstRetryDelayMs = 100)

if not response.code.is2xx():
raise newException(ValueError, response.status)
raise newException(ValueError, response.status & ": " & response.body())

cfg.iolog(t, "Presign " & response.status)

Expand Down
7 changes: 2 additions & 5 deletions nimutils/stsclient.nim
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import httpclient, strutils, tables, times, uri, std/[envvars, json]
import httpclient, strutils, tables, times, uri, std/[json]
import awsclient
export awsclient

const
awsURI = "https://amazonaws.com"

let
defRegion = getEnv("AWS_DEFAULT_REGION", "us-east-1")

type
StsClient* = object of AwsClient

proc newStsClient*(creds: AwsCredentials,
region: string = defRegion,
region: string = defaultRegion,
host: string = awsURI): StsClient =
let
# TODO - use some kind of template and compile-time variable to put the correct kernel used to build the sdk in the UA?
Expand Down

0 comments on commit 4ac970d

Please sign in to comment.