Skip to content

Commit

Permalink
S3: connect v4 presigned URL support to storage
Browse files Browse the repository at this point in the history
  • Loading branch information
zerocrates committed May 29, 2024
1 parent 037a8d7 commit 1ce89ad
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions application/libraries/Omeka/Storage/Adapter/ZendS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,30 +221,46 @@ public function getUri($path)
$uri = "$endpoint/$object";

if ($expiration = $this->_getExpiration()) {
$timestamp = time();
$expirationSeconds = $expiration * 60;
$expires = $timestamp + $expirationSeconds;
// "Chunk" expirations to allow browser caching
$expires = $expires + $expirationSeconds - ($expires % $expirationSeconds);
if ($this->_sigV4) {
$uri .= '?' . $this->_getPresignedQueryV4("/$object", $expiration);
} else {
$uri .= '?' . $this->_getPresignedQueryV2("/$object", $expiration);
}
}

return $uri;
}

$accessKeyId = $this->_options[self::AWS_KEY_OPTION];
$secretKey = $this->_options[self::AWS_SECRET_KEY_OPTION];
protected function _getPresignedQueryV2($path, $expiration)
{
$timestamp = time();
$expirationSeconds = $expiration * 60;
$expires = $timestamp + $expirationSeconds;
// "Chunk" expirations to allow browser caching
$expires = $expires + $expirationSeconds - ($expires % $expirationSeconds);

$stringToSign = "GET\n\n\n$expires\n/$object";
$accessKeyId = $this->_options[self::AWS_KEY_OPTION];
$secretKey = $this->_options[self::AWS_SECRET_KEY_OPTION];

$signature = base64_encode(
Zend_Crypt_Hmac::compute($secretKey, 'sha1', $stringToSign, Zend_Crypt_Hmac::BINARY));
$stringToSign = "GET\n\n\n$expires\n$path";

$query['AWSAccessKeyId'] = $accessKeyId;
$query['Expires'] = $expires;
$query['Signature'] = $signature;
$signature = base64_encode(
Zend_Crypt_Hmac::compute($secretKey, 'sha1', $stringToSign, Zend_Crypt_Hmac::BINARY));

$queryString = http_build_query($query);
$query['AWSAccessKeyId'] = $accessKeyId;
$query['Expires'] = $expires;
$query['Signature'] = $signature;

$uri .= "?$queryString";
}
return http_build_query($query);
}

return $uri;
protected function _getPresignedQueryV4($path, $expiration)
{
$expirationSeconds = $expiration * 60;
// SigV4 expirations are limited to 7 days
$expires = min($expirationSeconds, 604800);

return $this->_s3->getPresignedURLQuery($path, $expires);
}

/**
Expand Down

0 comments on commit 1ce89ad

Please sign in to comment.