Skip to content

Instantly share code, notes, and snippets.

@bdelbosc
Last active January 24, 2024 21:08
Show Gist options
  • Save bdelbosc/1c35c69827b764682d0927aab9c8cf9f to your computer and use it in GitHub Desktop.
Save bdelbosc/1c35c69827b764682d0927aab9c8cf9f to your computer and use it in GitHub Desktop.
#!/bin/bash -e
bucket=$1
shift
filename=$1
shift
contentType=$1
shift
# Grab the config values
configFile="/path/to/.aws/credentials"
eval $(cat $configFile | grep -P "^\w+=.*$")
# Calculate the signature.
resource="/${bucket}/${filename}"
dateValue=$(date -R)
stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}"
signature=$(echo -en ${stringToSign} | openssl sha1 -hmac ${aws_secret_access_key} -binary | base64)
# Output the Date and Authorization headers values
echo "${dateValue}|AWS ${aws_access_key_id}:${signature}"
// Gatling upload of file (blobFilename, blobPath, blobMimeType) into bucket with a PUT request (limited to 5g)
.exec(session => {
val script = "/path/to/awsS3Sign.sh " +
bucket + " " + session("blobFilename").as[String] + " " + session("blobMimeType").as[String]
val scriptOutput: String = script.!!
val dateHeader: String = scriptOutput.substring(0, scriptOutput.indexOf('|'))
val authorizationHeader: String = scriptOutput.substring(scriptOutput.indexOf('|') + 1).trim()
println("Upload " + session("blobFilename").as[String])
session.set("awsDate", dateHeader)
.set("awsAuth", authorizationHeader)
})
.exec(
http("s3 upload")
.put("https://" + bucket + ".s3.amazonaws.com/${blobFilename}")
.header("Host", bucket + ".s3.amazonaws.com")
.header("Date", "${awsDate}")
.header("Content-Type", "${blobMimeType}")
.header("Authorization", "${awsAuth}")
.body(RawFileBody("${blobPath}"))
.check(status.in(200))
)
// Disable the cache because S3 does not handle the If-None-Match header
val httpProtocol = http.disableCaching
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment