updated-packages

This commit is contained in:
RafficMohammed
2023-01-08 00:13:22 +05:30
parent 3ff7df7487
commit da241bacb6
12659 changed files with 563377 additions and 510538 deletions

View File

@@ -2,6 +2,7 @@
namespace Aws\S3;
use Aws\Api\Parser\AbstractParser;
use Aws\Api\Parser\Exception\ParserException;
use Aws\Api\StructureShape;
use Aws\CommandInterface;
use Aws\Exception\AwsException;
@@ -16,6 +17,7 @@ use Psr\Http\Message\StreamInterface;
class AmbiguousSuccessParser extends AbstractParser
{
private static $ambiguousSuccesses = [
'UploadPart' => true,
'UploadPartCopy' => true,
'CopyObject' => true,
'CompleteMultipartUpload' => true,
@@ -44,7 +46,16 @@ class AmbiguousSuccessParser extends AbstractParser
&& isset(self::$ambiguousSuccesses[$command->getName()])
) {
$errorParser = $this->errorParser;
$parsed = $errorParser($response);
try {
$parsed = $errorParser($response);
} catch (ParserException $e) {
$parsed = [
'code' => 'ConnectionError',
'message' => "An error connecting to the service occurred"
. " while performing the " . $command->getName()
. " operation."
];
}
if (isset($parsed['code']) && isset($parsed['message'])) {
throw new $this->exceptionClass(
$parsed['message'],

View File

@@ -1,12 +1,14 @@
<?php
namespace Aws\S3;
use Aws\Api\Service;
use Aws\CommandInterface;
use GuzzleHttp\Psr7;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
/**
* Apply required or optional MD5s to requests before sending.
* Apply required or optional checksums to requests before sending.
*
* IMPORTANT: This middleware must be added after the "build" step.
*
@@ -14,40 +16,33 @@ use Psr\Http\Message\RequestInterface;
*/
class ApplyChecksumMiddleware
{
private static $md5 = [
'DeleteObjects',
'PutBucketCors',
'PutBucketLifecycle',
'PutBucketLifecycleConfiguration',
'PutBucketPolicy',
'PutBucketTagging',
'PutBucketReplication',
'PutObjectLegalHold',
'PutObjectRetention',
'PutObjectLockConfiguration',
];
use CalculatesChecksumTrait;
private static $sha256 = [
'PutObject',
'UploadPart',
];
/** @var Service */
private $api;
private $nextHandler;
/**
* Create a middleware wrapper function.
*
* @param Service $api
* @return callable
*/
public static function wrap()
public static function wrap(Service $api)
{
return function (callable $handler) {
return new self($handler);
return function (callable $handler) use ($api) {
return new self($handler, $api);
};
}
public function __construct(callable $nextHandler)
public function __construct(callable $nextHandler, Service $api)
{
$this->api = $api;
$this->nextHandler = $nextHandler;
}
@@ -59,13 +54,57 @@ class ApplyChecksumMiddleware
$name = $command->getName();
$body = $request->getBody();
if (in_array($name, self::$md5) && !$request->hasHeader('Content-MD5')) {
// Set the content MD5 header for operations that require it.
$request = $request->withHeader(
'Content-MD5',
base64_encode(Psr7\hash($body, 'md5', true))
);
} elseif (in_array($name, self::$sha256) && $command['ContentSHA256']) {
$op = $this->api->getOperation($command->getName());
$checksumInfo = isset($op['httpChecksum'])
? $op['httpChecksum']
: [];
$checksumMemberName = array_key_exists('requestAlgorithmMember', $checksumInfo)
? $checksumInfo['requestAlgorithmMember']
: "";
$requestedAlgorithm = isset($command[$checksumMemberName])
? $command[$checksumMemberName]
: null;
if (!empty($checksumMemberName) && !empty($requestedAlgorithm)) {
$requestedAlgorithm = strtolower($requestedAlgorithm);
$checksumMember = $op->getInput()->getMember($checksumMemberName);
$supportedAlgorithms = isset($checksumMember['enum'])
? array_map('strtolower', $checksumMember['enum'])
: null;
if (is_array($supportedAlgorithms)
&& in_array($requestedAlgorithm, $supportedAlgorithms)
) {
$headerName = "x-amz-checksum-{$requestedAlgorithm}";
$encoded = $this->getEncodedValue($requestedAlgorithm, $body);
if (!$request->hasHeader($headerName)) {
$request = $request->withHeader($headerName, $encoded);
}
} else {
throw new InvalidArgumentException(
"Unsupported algorithm supplied for input variable {$checksumMemberName}."
. " Supported checksums for this operation include: "
. implode(", ", $supportedAlgorithms) . "."
);
}
return $next($command, $request);
}
if (!empty($checksumInfo)) {
//if the checksum member is absent, check if it's required
$checksumRequired = isset($checksumInfo['requestChecksumRequired'])
? $checksumInfo['requestChecksumRequired']
: null;
if (!empty($checksumRequired) && !$request->hasHeader('Content-MD5')) {
// Set the content MD5 header for operations that require it.
$request = $request->withHeader(
'Content-MD5',
base64_encode(Psr7\Utils::hash($body, 'md5', true))
);
return $next($command, $request);
}
}
if (in_array($name, self::$sha256) && $command['ContentSHA256']) {
// Set the content hash header if provided in the parameters.
$request = $request->withHeader(
'X-Amz-Content-Sha256',

View File

@@ -76,7 +76,7 @@ class BatchDelete implements PromisorInterface
}
}
}
return $promises ? Promise\all($promises) : null;
return $promises ? Promise\Utils::all($promises) : null;
});
};
@@ -100,7 +100,7 @@ class BatchDelete implements PromisorInterface
array $options = []
) {
$fn = function (BatchDelete $that) use ($iter) {
return Promise\coroutine(function () use ($that, $iter) {
return Promise\Coroutine::of(function () use ($that, $iter) {
foreach ($iter as $obj) {
if ($promise = $that->enqueue($obj)) {
yield $promise;
@@ -112,6 +112,9 @@ class BatchDelete implements PromisorInterface
return new self($client, $bucket, $fn, $options);
}
/**
* @return PromiseInterface
*/
public function promise()
{
if (!$this->cachedPromise) {
@@ -225,12 +228,12 @@ class BatchDelete implements PromisorInterface
// When done, ensure cleanup and that any remaining are processed.
return $promise->then(
function () use ($cleanup) {
return Promise\promise_for($this->flushQueue())
return Promise\Create::promiseFor($this->flushQueue())
->then($cleanup);
},
function ($reason) use ($cleanup) {
$cleanup();
return Promise\rejection_for($reason);
return Promise\Create::rejectionFor($reason);
}
);
}

View File

@@ -0,0 +1,355 @@
<?php
namespace Aws\S3;
use Aws\Api\Service;
use Aws\Arn\AccessPointArnInterface;
use Aws\Arn\ArnParser;
use Aws\Arn\ObjectLambdaAccessPointArn;
use Aws\Arn\Exception\InvalidArnException;
use Aws\Arn\AccessPointArn as BaseAccessPointArn;
use Aws\Arn\S3\OutpostsAccessPointArn;
use Aws\Arn\S3\MultiRegionAccessPointArn;
use Aws\Arn\S3\OutpostsArnInterface;
use Aws\CommandInterface;
use Aws\Endpoint\PartitionEndpointProvider;
use Aws\Exception\InvalidRegionException;
use Aws\Exception\UnresolvedEndpointException;
use Aws\S3\Exception\S3Exception;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
/**
* Checks for access point ARN in members targeting BucketName, modifying
* endpoint as appropriate
*
* @internal
*/
class BucketEndpointArnMiddleware
{
use EndpointRegionHelperTrait;
/** @var callable */
private $nextHandler;
/** @var array */
private $nonArnableCommands = ['CreateBucket'];
/** @var boolean */
private $isUseEndpointV2;
/**
* Create a middleware wrapper function.
*
* @param Service $service
* @param $region
* @param array $config
* @return callable
*/
public static function wrap(
Service $service,
$region,
array $config,
$isUseEndpointV2
) {
return function (callable $handler) use ($service, $region, $config, $isUseEndpointV2) {
return new self($handler, $service, $region, $config, $isUseEndpointV2);
};
}
public function __construct(
callable $nextHandler,
Service $service,
$region,
array $config = [],
$isUseEndpointV2 = false
) {
$this->partitionProvider = PartitionEndpointProvider::defaultProvider();
$this->region = $region;
$this->service = $service;
$this->config = $config;
$this->nextHandler = $nextHandler;
$this->isUseEndpointV2 = $isUseEndpointV2;
}
public function __invoke(CommandInterface $cmd, RequestInterface $req)
{
$nextHandler = $this->nextHandler;
$op = $this->service->getOperation($cmd->getName())->toArray();
if (!empty($op['input']['shape'])) {
$service = $this->service->toArray();
if (!empty($input = $service['shapes'][$op['input']['shape']])) {
foreach ($input['members'] as $key => $member) {
if ($member['shape'] === 'BucketName') {
$arnableKey = $key;
break;
}
}
if (!empty($arnableKey) && ArnParser::isArn($cmd[$arnableKey])) {
try {
// Throw for commands that do not support ARN inputs
if (in_array($cmd->getName(), $this->nonArnableCommands)) {
throw new S3Exception(
'ARN values cannot be used in the bucket field for'
. ' the ' . $cmd->getName() . ' operation.',
$cmd
);
}
if (!$this->isUseEndpointV2) {
$arn = ArnParser::parse($cmd[$arnableKey]);
$partition = $this->validateArn($arn);
$host = $this->generateAccessPointHost($arn, $req);
}
// Remove encoded bucket string from path
$path = $req->getUri()->getPath();
$encoded = rawurlencode($cmd[$arnableKey]);
$len = strlen($encoded) + 1;
if (trim(substr($path, 0, $len), '/') === "{$encoded}") {
$path = substr($path, $len);
if (substr($path, 0, 1) !== "/") {
$path = '/' . $path;
}
}
if (empty($path)) {
$path = '';
}
// Set modified request
if ($this->isUseEndpointV2) {
$req = $req->withUri(
$req->getUri()->withPath($path)
);
goto next;
}
$req = $req->withUri(
$req->getUri()->withPath($path)->withHost($host)
);
// Update signing region based on ARN data if configured to do so
if ($this->config['use_arn_region']->isUseArnRegion()
&& !$this->config['use_fips_endpoint']->isUseFipsEndpoint()
) {
$region = $arn->getRegion();
} else {
$region = $this->region;
}
$endpointData = $partition([
'region' => $region,
'service' => $arn->getService()
]);
$cmd['@context']['signing_region'] = $endpointData['signingRegion'];
// Update signing service for Outposts and Lambda ARNs
if ($arn instanceof OutpostsArnInterface
|| $arn instanceof ObjectLambdaAccessPointArn
) {
$cmd['@context']['signing_service'] = $arn->getService();
}
} catch (InvalidArnException $e) {
// Add context to ARN exception
throw new S3Exception(
'Bucket parameter parsed as ARN and failed with: '
. $e->getMessage(),
$cmd,
[],
$e
);
}
}
}
}
next:
return $nextHandler($cmd, $req);
}
private function generateAccessPointHost(
BaseAccessPointArn $arn,
RequestInterface $req
) {
if ($arn instanceof OutpostsAccessPointArn) {
$accesspointName = $arn->getAccesspointName();
} else {
$accesspointName = $arn->getResourceId();
}
if ($arn instanceof MultiRegionAccessPointArn) {
$partition = $this->partitionProvider->getPartitionByName(
$arn->getPartition(),
's3'
);
$dnsSuffix = $partition->getDnsSuffix();
return "{$accesspointName}.accesspoint.s3-global.{$dnsSuffix}";
}
$host = "{$accesspointName}-" . $arn->getAccountId();
$useFips = $this->config['use_fips_endpoint']->isUseFipsEndpoint();
$fipsString = $useFips ? "-fips" : "";
if ($arn instanceof OutpostsAccessPointArn) {
$host .= '.' . $arn->getOutpostId() . '.s3-outposts';
} else if ($arn instanceof ObjectLambdaAccessPointArn) {
if (!empty($this->config['endpoint'])) {
return $host . '.' . $this->config['endpoint'];
} else {
$host .= ".s3-object-lambda{$fipsString}";
}
} else {
$host .= ".s3-accesspoint{$fipsString}";
if (!empty($this->config['dual_stack'])) {
$host .= '.dualstack';
}
}
if (!empty($this->config['use_arn_region']->isUseArnRegion())) {
$region = $arn->getRegion();
} else {
$region = $this->region;
}
$region = \Aws\strip_fips_pseudo_regions($region);
$host .= '.' . $region . '.' . $this->getPartitionSuffix($arn, $this->partitionProvider);
return $host;
}
/**
* Validates an ARN, returning a partition object corresponding to the ARN
* if successful
*
* @param $arn
* @return \Aws\Endpoint\Partition
*/
private function validateArn($arn)
{
if ($arn instanceof AccessPointArnInterface) {
// Dualstack is not supported with Outposts access points
if ($arn instanceof OutpostsAccessPointArn
&& !empty($this->config['dual_stack'])
) {
throw new UnresolvedEndpointException(
'Dualstack is currently not supported with S3 Outposts access'
. ' points. Please disable dualstack or do not supply an'
. ' access point ARN.');
}
if ($arn instanceof MultiRegionAccessPointArn) {
if (!empty($this->config['disable_multiregion_access_points'])) {
throw new UnresolvedEndpointException(
'Multi-Region Access Point ARNs are disabled, but one was provided. Please'
. ' enable them or provide a different ARN.'
);
}
if (!empty($this->config['dual_stack'])) {
throw new UnresolvedEndpointException(
'Multi-Region Access Point ARNs do not currently support dual stack. Please'
. ' disable dual stack or provide a different ARN.'
);
}
}
// Accelerate is not supported with access points
if (!empty($this->config['accelerate'])) {
throw new UnresolvedEndpointException(
'Accelerate is currently not supported with access points.'
. ' Please disable accelerate or do not supply an access'
. ' point ARN.');
}
// Path-style is not supported with access points
if (!empty($this->config['path_style'])) {
throw new UnresolvedEndpointException(
'Path-style addressing is currently not supported with'
. ' access points. Please disable path-style or do not'
. ' supply an access point ARN.');
}
// Custom endpoint is not supported with access points
if (!is_null($this->config['endpoint'])
&& !$arn instanceof ObjectLambdaAccessPointArn
) {
throw new UnresolvedEndpointException(
'A custom endpoint has been supplied along with an access'
. ' point ARN, and these are not compatible with each other.'
. ' Please only use one or the other.');
}
// Dualstack is not supported with object lambda access points
if ($arn instanceof ObjectLambdaAccessPointArn
&& !empty($this->config['dual_stack'])
) {
throw new UnresolvedEndpointException(
'Dualstack is currently not supported with Object Lambda access'
. ' points. Please disable dualstack or do not supply an'
. ' access point ARN.');
}
// Global endpoints do not support cross-region requests
if ($this->isGlobal($this->region)
&& $this->config['use_arn_region']->isUseArnRegion() == false
&& $arn->getRegion() != $this->region
&& !$arn instanceof MultiRegionAccessPointArn
) {
throw new UnresolvedEndpointException(
'Global endpoints do not support cross region requests.'
. ' Please enable use_arn_region or do not supply a global region'
. ' with a different region in the ARN.');
}
// Get partitions for ARN and client region
$arnPart = $this->partitionProvider->getPartition(
$arn->getRegion(),
's3'
);
$clientPart = $this->partitionProvider->getPartition(
$this->region,
's3'
);
// If client partition not found, try removing pseudo-region qualifiers
if (!($clientPart->isRegionMatch($this->region, 's3'))) {
$clientPart = $this->partitionProvider->getPartition(
\Aws\strip_fips_pseudo_regions($this->region),
's3'
);
}
if (!$arn instanceof MultiRegionAccessPointArn) {
// Verify that the partition matches for supplied partition and region
if ($arn->getPartition() !== $clientPart->getName()) {
throw new InvalidRegionException('The supplied ARN partition'
. " does not match the client's partition.");
}
if ($clientPart->getName() !== $arnPart->getName()) {
throw new InvalidRegionException('The corresponding partition'
. ' for the supplied ARN region does not match the'
. " client's partition.");
}
// Ensure ARN region matches client region unless
// configured for using ARN region over client region
$this->validateMatchingRegion($arn);
// Ensure it is not resolved to fips pseudo-region for S3 Outposts
$this->validateFipsConfigurations($arn);
}
return $arnPart;
}
throw new InvalidArnException('Provided ARN was not a valid S3 access'
. ' point ARN or S3 Outposts access point ARN.');
}
/**
* Checks if a region is global
*
* @param $region
* @return bool
*/
private function isGlobal($region)
{
return $region == 's3-external-1' || $region == 'aws-global';
}
}

View File

@@ -46,30 +46,75 @@ class BucketEndpointMiddleware
return $nextHandler($command, $request);
}
private function removeBucketFromPath($path, $bucket)
/**
* Performs a one-time removal of Bucket from path, then if
* the bucket name is duplicated in the path, performs additional
* removal which is dependent on the number of occurrences of the bucket
* name in a path-like format in the key name.
*
* @return string
*/
private function removeBucketFromPath($path, $bucket, $key)
{
$len = strlen($bucket) + 1;
if (substr($path, 0, $len) === "/{$bucket}") {
$path = substr($path, $len);
}
$occurrencesInKey = $this->getBucketNameOccurrencesInKey($key, $bucket);
do {
$len = strlen($bucket) + 1;
if (substr($path, 0, $len) === "/{$bucket}") {
$path = substr($path, $len);
}
} while (substr_count($path, "/{$bucket}") > $occurrencesInKey + 1);
return $path ?: '/';
}
private function removeDuplicateBucketFromHost($host, $bucket)
{
if (substr_count($host, $bucket) > 1) {
while (strpos($host, "{$bucket}.{$bucket}") === 0) {
$hostArr = explode('.', $host);
array_shift($hostArr);
$host = implode('.', $hostArr);
}
}
return $host;
}
private function getBucketNameOccurrencesInKey($key, $bucket)
{
$occurrences = 0;
if (empty($key)) {
return $occurrences;
}
$segments = explode('/', $key);
foreach($segments as $segment) {
if (strpos($segment, $bucket) === 0) {
$occurrences++;
}
}
return $occurrences;
}
private function modifyRequest(
RequestInterface $request,
CommandInterface $command
) {
$key = isset($command['Key']) ? $command['Key'] : null;
$uri = $request->getUri();
$path = $uri->getPath();
$host = $uri->getHost();
$bucket = $command['Bucket'];
$path = $this->removeBucketFromPath($path, $bucket);
$path = $this->removeBucketFromPath($path, $bucket, $key);
$host = $this->removeDuplicateBucketFromHost($host, $bucket);
// Modify the Key to make sure the key is encoded, but slashes are not.
if ($command['Key']) {
if ($key) {
$path = S3Client::encodeKey(rawurldecode($path));
}
return $request->withUri($uri->withPath($path));
return $request->withUri(
$uri->withHost($host)
->withPath($path)
);
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Aws\S3;
use AWS\CRT\CRT;
use Aws\Exception\CommonRuntimeException;
use GuzzleHttp\Psr7;
use InvalidArgumentException;
trait CalculatesChecksumTrait
{
/**
* @param string $requestedAlgorithm the algorithm to encode with
* @param string $value the value to be encoded
* @return string
*/
public static function getEncodedValue($requestedAlgorithm, $value) {
$requestedAlgorithm = strtolower($requestedAlgorithm);
$useCrt = extension_loaded('awscrt');
if ($useCrt) {
switch ($requestedAlgorithm) {
case 'crc32c':
return CRT::crc32c($value);
case 'crc32':
return CRT::crc32($value);
case 'sha256':
case 'sha1':
return base64_encode(Psr7\Utils::hash($value, $requestedAlgorithm, true));
default:
break;
throw new InvalidArgumentException(
"Invalid checksum requested: {$requestedAlgorithm}."
. " Valid algorithms are CRC32C, CRC32, SHA256, and SHA1."
);
}
} else {
if ($requestedAlgorithm == 'crc32c') {
throw new CommonRuntimeException("crc32c is not supported for checksums "
. "without use of the common runtime for php. Please enable the CRT or choose "
. "a different algorithm."
);
}
if ($requestedAlgorithm == "crc32") {
$requestedAlgorithm = "crc32b";
}
return base64_encode(Psr7\Utils::hash($value, $requestedAlgorithm, true));
}
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Aws\S3\Crypto;
use Aws\Crypto\MaterialsProviderInterfaceV2;
trait CryptoParamsTraitV2
{
use CryptoParamsTrait;
protected function getMaterialsProvider(array $args)
{
if ($args['@MaterialsProvider'] instanceof MaterialsProviderInterfaceV2) {
return $args['@MaterialsProvider'];
}
throw new \InvalidArgumentException('An instance of MaterialsProviderInterfaceV2'
. ' must be passed in the "MaterialsProvider" field.');
}
}

View File

@@ -7,7 +7,7 @@ use \Aws\Crypto\MetadataEnvelope;
class HeadersMetadataStrategy implements MetadataStrategyInterface
{
/**
* Places the information in the MetadataEnvelope in to the Meatadata for
* Places the information in the MetadataEnvelope in to the metadata for
* the PutObject request of the encrypted object.
*
* @param MetadataEnvelope $envelope Encryption data to save according to
@@ -27,7 +27,7 @@ class HeadersMetadataStrategy implements MetadataStrategyInterface
}
/**
* Generates a MetadataEnvelope according to the Metadata headers from the
* Generates a MetadataEnvelope according to the metadata headers from the
* GetObject result.
*
* @param array $args Arguments from Command and Result that contains

View File

@@ -1,11 +1,11 @@
<?php
namespace Aws\S3\Crypto;
use Aws\Crypto\DecryptionTrait;
use Aws\HashingStream;
use Aws\PhpHash;
use Aws\Crypto\AbstractCryptoClient;
use Aws\Crypto\EncryptionTrait;
use Aws\Crypto\DecryptionTrait;
use Aws\Crypto\MetadataEnvelope;
use Aws\Crypto\MaterialsProvider;
use Aws\Crypto\Cipher\CipherBuilderTrait;
@@ -17,10 +17,26 @@ use GuzzleHttp\Psr7;
/**
* Provides a wrapper for an S3Client that supplies functionality to encrypt
* data on putObject[Async] calls and decrypt data on getObject[Async] calls.
*
* Legacy implementation using older encryption workflow.
*
* AWS strongly recommends the upgrade to the S3EncryptionClientV2 (over the
* S3EncryptionClient), as it offers updated data security best practices to our
* customers who upgrade. S3EncryptionClientV2 contains breaking changes, so this
* will require planning by engineering teams to migrate. New workflows should
* just start with S3EncryptionClientV2.
*
* @deprecated
*/
class S3EncryptionClient extends AbstractCryptoClient
{
use EncryptionTrait, DecryptionTrait, CipherBuilderTrait, CryptoParamsTrait;
use CipherBuilderTrait;
use CryptoParamsTrait;
use DecryptionTrait;
use EncryptionTrait;
use UserAgentTrait;
const CRYPTO_VERSION = '1n';
private $client;
private $instructionFileSuffix;
@@ -37,6 +53,7 @@ class S3EncryptionClient extends AbstractCryptoClient
S3Client $client,
$instructionFileSuffix = null
) {
$this->appendUserAgent($client, 'feat/s3-encrypt/' . self::CRYPTO_VERSION);
$this->client = $client;
$this->instructionFileSuffix = $instructionFileSuffix;
}
@@ -60,12 +77,15 @@ class S3EncryptionClient extends AbstractCryptoClient
* - @CipherOptions: (array) Cipher options for encrypting data. Only the
* Cipher option is required. Accepts the following:
* - Cipher: (string) cbc|gcm
* See also: AbstractCryptoClient::$supportedCiphers
* See also: AbstractCryptoClient::$supportedCiphers. Note that
* cbc is deprecated and gcm should be used when possible.
* - KeySize: (int) 128|192|256
* See also: MaterialsProvider::$supportedKeySizes
* - Aad: (string) Additional authentication data. This option is
* passed directly to OpenSSL when using gcm. It is ignored when
* using cbc.
* using cbc. Note if you pass in Aad for gcm encryption, the
* PHP SDK will be able to decrypt the resulting object, but other
* AWS SDKs may not be able to do so.
*
* The optional configuration arguments are as follows:
*
@@ -95,8 +115,8 @@ class S3EncryptionClient extends AbstractCryptoClient
$envelope = new MetadataEnvelope();
return Promise\promise_for($this->encrypt(
Psr7\stream_for($args['Body']),
return Promise\Create::promiseFor($this->encrypt(
Psr7\Utils::streamFor($args['Body']),
$args['@CipherOptions'] ?: [],
$provider,
$envelope
@@ -150,12 +170,15 @@ class S3EncryptionClient extends AbstractCryptoClient
* - @CipherOptions: (array) Cipher options for encrypting data. A Cipher
* is required. Accepts the following options:
* - Cipher: (string) cbc|gcm
* See also: AbstractCryptoClient::$supportedCiphers
* See also: AbstractCryptoClient::$supportedCiphers. Note that
* cbc is deprecated and gcm should be used when possible.
* - KeySize: (int) 128|192|256
* See also: MaterialsProvider::$supportedKeySizes
* - Aad: (string) Additional authentication data. This option is
* passed directly to OpenSSL when using gcm. It is ignored when
* using cbc.
* using cbc. Note if you pass in Aad for gcm encryption, the
* PHP SDK will be able to decrypt the resulting object, but other
* AWS SDKs may not be able to do so.
*
* The optional configuration arguments are as follows:
*

View File

@@ -0,0 +1,446 @@
<?php
namespace Aws\S3\Crypto;
use Aws\Crypto\DecryptionTraitV2;
use Aws\Exception\CryptoException;
use Aws\HashingStream;
use Aws\PhpHash;
use Aws\Crypto\AbstractCryptoClientV2;
use Aws\Crypto\EncryptionTraitV2;
use Aws\Crypto\MetadataEnvelope;
use Aws\Crypto\MaterialsProvider;
use Aws\Crypto\Cipher\CipherBuilderTrait;
use Aws\S3\S3Client;
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7;
/**
* Provides a wrapper for an S3Client that supplies functionality to encrypt
* data on putObject[Async] calls and decrypt data on getObject[Async] calls.
*
* AWS strongly recommends the upgrade to the S3EncryptionClientV2 (over the
* S3EncryptionClient), as it offers updated data security best practices to our
* customers who upgrade. S3EncryptionClientV2 contains breaking changes, so this
* will require planning by engineering teams to migrate. New workflows should
* just start with S3EncryptionClientV2.
*
* Note that for PHP versions of < 7.1, this class uses an AES-GCM polyfill
* for encryption since there is no native PHP support. The performance for large
* inputs will be a lot slower than for PHP 7.1+, so upgrading older PHP version
* environments may be necessary to use this effectively.
*
* Example write path:
*
* <code>
* use Aws\Crypto\KmsMaterialsProviderV2;
* use Aws\S3\Crypto\S3EncryptionClientV2;
* use Aws\S3\S3Client;
*
* $encryptionClient = new S3EncryptionClientV2(
* new S3Client([
* 'region' => 'us-west-2',
* 'version' => 'latest'
* ])
* );
* $materialsProvider = new KmsMaterialsProviderV2(
* new KmsClient([
* 'profile' => 'default',
* 'region' => 'us-east-1',
* 'version' => 'latest',
* ],
* 'your-kms-key-id'
* );
*
* $encryptionClient->putObject([
* '@MaterialsProvider' => $materialsProvider,
* '@CipherOptions' => [
* 'Cipher' => 'gcm',
* 'KeySize' => 256,
* ],
* '@KmsEncryptionContext' => ['foo' => 'bar'],
* 'Bucket' => 'your-bucket',
* 'Key' => 'your-key',
* 'Body' => 'your-encrypted-data',
* ]);
* </code>
*
* Example read call (using objects from previous example):
*
* <code>
* $encryptionClient->getObject([
* '@MaterialsProvider' => $materialsProvider,
* '@CipherOptions' => [
* 'Cipher' => 'gcm',
* 'KeySize' => 256,
* ],
* 'Bucket' => 'your-bucket',
* 'Key' => 'your-key',
* ]);
* </code>
*/
class S3EncryptionClientV2 extends AbstractCryptoClientV2
{
use CipherBuilderTrait;
use CryptoParamsTraitV2;
use DecryptionTraitV2;
use EncryptionTraitV2;
use UserAgentTrait;
const CRYPTO_VERSION = '2.1';
private $client;
private $instructionFileSuffix;
private $legacyWarningCount;
/**
* @param S3Client $client The S3Client to be used for true uploading and
* retrieving objects from S3 when using the
* encryption client.
* @param string|null $instructionFileSuffix Suffix for a client wide
* default when using instruction
* files for metadata storage.
*/
public function __construct(
S3Client $client,
$instructionFileSuffix = null
) {
$this->appendUserAgent($client, 'feat/s3-encrypt/' . self::CRYPTO_VERSION);
$this->client = $client;
$this->instructionFileSuffix = $instructionFileSuffix;
$this->legacyWarningCount = 0;
}
private static function getDefaultStrategy()
{
return new HeadersMetadataStrategy();
}
/**
* Encrypts the data in the 'Body' field of $args and promises to upload it
* to the specified location on S3.
*
* Note that for PHP versions of < 7.1, this operation uses an AES-GCM
* polyfill for encryption since there is no native PHP support. The
* performance for large inputs will be a lot slower than for PHP 7.1+, so
* upgrading older PHP version environments may be necessary to use this
* effectively.
*
* @param array $args Arguments for encrypting an object and uploading it
* to S3 via PutObject.
*
* The required configuration arguments are as follows:
*
* - @MaterialsProvider: (MaterialsProviderV2) Provides Cek, Iv, and Cek
* encrypting/decrypting for encryption metadata.
* - @CipherOptions: (array) Cipher options for encrypting data. Only the
* Cipher option is required. Accepts the following:
* - Cipher: (string) gcm
* See also: AbstractCryptoClientV2::$supportedCiphers
* - KeySize: (int) 128|256
* See also: MaterialsProvider::$supportedKeySizes
* - Aad: (string) Additional authentication data. This option is
* passed directly to OpenSSL when using gcm. Note if you pass in
* Aad, the PHP SDK will be able to decrypt the resulting object,
* but other AWS SDKs may not be able to do so.
* - @KmsEncryptionContext: (array) Only required if using
* KmsMaterialsProviderV2. An associative array of key-value
* pairs to be added to the encryption context for KMS key encryption. An
* empty array may be passed if no additional context is desired.
*
* The optional configuration arguments are as follows:
*
* - @MetadataStrategy: (MetadataStrategy|string|null) Strategy for storing
* MetadataEnvelope information. Defaults to using a
* HeadersMetadataStrategy. Can either be a class implementing
* MetadataStrategy, a class name of a predefined strategy, or empty/null
* to default.
* - @InstructionFileSuffix: (string|null) Suffix used when writing to an
* instruction file if using an InstructionFileMetadataHandler.
*
* @return PromiseInterface
*
* @throws \InvalidArgumentException Thrown when arguments above are not
* passed or are passed incorrectly.
*/
public function putObjectAsync(array $args)
{
$provider = $this->getMaterialsProvider($args);
unset($args['@MaterialsProvider']);
$instructionFileSuffix = $this->getInstructionFileSuffix($args);
unset($args['@InstructionFileSuffix']);
$strategy = $this->getMetadataStrategy($args, $instructionFileSuffix);
unset($args['@MetadataStrategy']);
$envelope = new MetadataEnvelope();
return Promise\Create::promiseFor($this->encrypt(
Psr7\Utils::streamFor($args['Body']),
$args,
$provider,
$envelope
))->then(
function ($encryptedBodyStream) use ($args) {
$hash = new PhpHash('sha256');
$hashingEncryptedBodyStream = new HashingStream(
$encryptedBodyStream,
$hash,
self::getContentShaDecorator($args)
);
return [$hashingEncryptedBodyStream, $args];
}
)->then(
function ($putObjectContents) use ($strategy, $envelope) {
list($bodyStream, $args) = $putObjectContents;
if ($strategy === null) {
$strategy = self::getDefaultStrategy();
}
$updatedArgs = $strategy->save($envelope, $args);
$updatedArgs['Body'] = $bodyStream;
return $updatedArgs;
}
)->then(
function ($args) {
unset($args['@CipherOptions']);
return $this->client->putObjectAsync($args);
}
);
}
private static function getContentShaDecorator(&$args)
{
return function ($hash) use (&$args) {
$args['ContentSHA256'] = bin2hex($hash);
};
}
/**
* Encrypts the data in the 'Body' field of $args and uploads it to the
* specified location on S3.
*
* Note that for PHP versions of < 7.1, this operation uses an AES-GCM
* polyfill for encryption since there is no native PHP support. The
* performance for large inputs will be a lot slower than for PHP 7.1+, so
* upgrading older PHP version environments may be necessary to use this
* effectively.
*
* @param array $args Arguments for encrypting an object and uploading it
* to S3 via PutObject.
*
* The required configuration arguments are as follows:
*
* - @MaterialsProvider: (MaterialsProvider) Provides Cek, Iv, and Cek
* encrypting/decrypting for encryption metadata.
* - @CipherOptions: (array) Cipher options for encrypting data. A Cipher
* is required. Accepts the following options:
* - Cipher: (string) gcm
* See also: AbstractCryptoClientV2::$supportedCiphers
* - KeySize: (int) 128|256
* See also: MaterialsProvider::$supportedKeySizes
* - Aad: (string) Additional authentication data. This option is
* passed directly to OpenSSL when using gcm. Note if you pass in
* Aad, the PHP SDK will be able to decrypt the resulting object,
* but other AWS SDKs may not be able to do so.
* - @KmsEncryptionContext: (array) Only required if using
* KmsMaterialsProviderV2. An associative array of key-value
* pairs to be added to the encryption context for KMS key encryption. An
* empty array may be passed if no additional context is desired.
*
* The optional configuration arguments are as follows:
*
* - @MetadataStrategy: (MetadataStrategy|string|null) Strategy for storing
* MetadataEnvelope information. Defaults to using a
* HeadersMetadataStrategy. Can either be a class implementing
* MetadataStrategy, a class name of a predefined strategy, or empty/null
* to default.
* - @InstructionFileSuffix: (string|null) Suffix used when writing to an
* instruction file if an using an InstructionFileMetadataHandler was
* determined.
*
* @return \Aws\Result PutObject call result with the details of uploading
* the encrypted file.
*
* @throws \InvalidArgumentException Thrown when arguments above are not
* passed or are passed incorrectly.
*/
public function putObject(array $args)
{
return $this->putObjectAsync($args)->wait();
}
/**
* Promises to retrieve an object from S3 and decrypt the data in the
* 'Body' field.
*
* @param array $args Arguments for retrieving an object from S3 via
* GetObject and decrypting it.
*
* The required configuration argument is as follows:
*
* - @MaterialsProvider: (MaterialsProviderInterface) Provides Cek, Iv, and Cek
* encrypting/decrypting for decryption metadata. May have data loaded
* from the MetadataEnvelope upon decryption.
* - @SecurityProfile: (string) Must be set to 'V2' or 'V2_AND_LEGACY'.
* - 'V2' indicates that only objects encrypted with S3EncryptionClientV2
* content encryption and key wrap schemas are able to be decrypted.
* - 'V2_AND_LEGACY' indicates that objects encrypted with both
* S3EncryptionClientV2 and older legacy encryption clients are able
* to be decrypted.
*
* The optional configuration arguments are as follows:
*
* - SaveAs: (string) The path to a file on disk to save the decrypted
* object data. This will be handled by file_put_contents instead of the
* Guzzle sink.
*
* - @MetadataStrategy: (MetadataStrategy|string|null) Strategy for reading
* MetadataEnvelope information. Defaults to determining based on object
* response headers. Can either be a class implementing MetadataStrategy,
* a class name of a predefined strategy, or empty/null to default.
* - @InstructionFileSuffix: (string) Suffix used when looking for an
* instruction file if an InstructionFileMetadataHandler is being used.
* - @CipherOptions: (array) Cipher options for decrypting data. A Cipher
* is required. Accepts the following options:
* - Aad: (string) Additional authentication data. This option is
* passed directly to OpenSSL when using gcm. It is ignored when
* using cbc.
* - @KmsAllowDecryptWithAnyCmk: (bool) This allows decryption with
* KMS materials for any KMS key ID, instead of needing the KMS key ID to
* be specified and provided to the decrypt operation. Ignored for non-KMS
* materials providers. Defaults to false.
*
* @return PromiseInterface
*
* @throws \InvalidArgumentException Thrown when required arguments are not
* passed or are passed incorrectly.
*/
public function getObjectAsync(array $args)
{
$provider = $this->getMaterialsProvider($args);
unset($args['@MaterialsProvider']);
$instructionFileSuffix = $this->getInstructionFileSuffix($args);
unset($args['@InstructionFileSuffix']);
$strategy = $this->getMetadataStrategy($args, $instructionFileSuffix);
unset($args['@MetadataStrategy']);
if (!isset($args['@SecurityProfile'])
|| !in_array($args['@SecurityProfile'], self::$supportedSecurityProfiles)
) {
throw new CryptoException("@SecurityProfile is required and must be"
. " set to 'V2' or 'V2_AND_LEGACY'");
}
// Only throw this legacy warning once per client
if (in_array($args['@SecurityProfile'], self::$legacySecurityProfiles)
&& $this->legacyWarningCount < 1
) {
$this->legacyWarningCount++;
trigger_error(
"This S3 Encryption Client operation is configured to"
. " read encrypted data with legacy encryption modes. If you"
. " don't have objects encrypted with these legacy modes,"
. " you should disable support for them to enhance security. ",
E_USER_WARNING
);
}
$saveAs = null;
if (!empty($args['SaveAs'])) {
$saveAs = $args['SaveAs'];
}
$promise = $this->client->getObjectAsync($args)
->then(
function ($result) use (
$provider,
$instructionFileSuffix,
$strategy,
$args
) {
if ($strategy === null) {
$strategy = $this->determineGetObjectStrategy(
$result,
$instructionFileSuffix
);
}
$envelope = $strategy->load($args + [
'Metadata' => $result['Metadata']
]);
$result['Body'] = $this->decrypt(
$result['Body'],
$provider,
$envelope,
$args
);
return $result;
}
)->then(
function ($result) use ($saveAs) {
if (!empty($saveAs)) {
file_put_contents(
$saveAs,
(string)$result['Body'],
LOCK_EX
);
}
return $result;
}
);
return $promise;
}
/**
* Retrieves an object from S3 and decrypts the data in the 'Body' field.
*
* @param array $args Arguments for retrieving an object from S3 via
* GetObject and decrypting it.
*
* The required configuration argument is as follows:
*
* - @MaterialsProvider: (MaterialsProviderInterface) Provides Cek, Iv, and Cek
* encrypting/decrypting for decryption metadata. May have data loaded
* from the MetadataEnvelope upon decryption.
* - @SecurityProfile: (string) Must be set to 'V2' or 'V2_AND_LEGACY'.
* - 'V2' indicates that only objects encrypted with S3EncryptionClientV2
* content encryption and key wrap schemas are able to be decrypted.
* - 'V2_AND_LEGACY' indicates that objects encrypted with both
* S3EncryptionClientV2 and older legacy encryption clients are able
* to be decrypted.
*
* The optional configuration arguments are as follows:
*
* - SaveAs: (string) The path to a file on disk to save the decrypted
* object data. This will be handled by file_put_contents instead of the
* Guzzle sink.
* - @InstructionFileSuffix: (string|null) Suffix used when looking for an
* instruction file if an InstructionFileMetadataHandler was detected.
* - @CipherOptions: (array) Cipher options for encrypting data. A Cipher
* is required. Accepts the following options:
* - Aad: (string) Additional authentication data. This option is
* passed directly to OpenSSL when using gcm. It is ignored when
* using cbc.
* - @KmsAllowDecryptWithAnyCmk: (bool) This allows decryption with
* KMS materials for any KMS key ID, instead of needing the KMS key ID to
* be specified and provided to the decrypt operation. Ignored for non-KMS
* materials providers. Defaults to false.
*
* @return \Aws\Result GetObject call result with the 'Body' field
* wrapped in a decryption stream with its metadata
* information.
*
* @throws \InvalidArgumentException Thrown when arguments above are not
* passed or are passed incorrectly.
*/
public function getObject(array $args)
{
return $this->getObjectAsync($args)->wait();
}
}

View File

@@ -11,10 +11,20 @@ use GuzzleHttp\Promise;
/**
* Encapsulates the execution of a multipart upload of an encrypted object to S3.
*
* Legacy implementation using older encryption workflow. Use
* S3EncryptionMultipartUploaderV2 if possible.
*
* @deprecated
*/
class S3EncryptionMultipartUploader extends MultipartUploader
{
use EncryptionTrait, CipherBuilderTrait, CryptoParamsTrait;
use CipherBuilderTrait;
use CryptoParamsTrait;
use EncryptionTrait;
use UserAgentTrait;
const CRYPTO_VERSION = '1n';
/**
* Returns if the passed cipher name is supported for encryption by the SDK.
@@ -42,7 +52,8 @@ class S3EncryptionMultipartUploader extends MultipartUploader
* - @CipherOptions: (array) Cipher options for encrypting data. A Cipher
* is required. Accepts the following options:
* - Cipher: (string) cbc|gcm
* See also: AbstractCryptoClient::$supportedCiphers
* See also: AbstractCryptoClient::$supportedCiphers. Note that
* cbc is deprecated and gcm should be used when possible.
* - KeySize: (int) 128|192|256
* See also: MaterialsProvider::$supportedKeySizes
* - Aad: (string) Additional authentication data. This option is
@@ -96,6 +107,7 @@ class S3EncryptionMultipartUploader extends MultipartUploader
$source,
array $config = []
) {
$this->appendUserAgent($client, 'feat/s3-encrypt/' . self::CRYPTO_VERSION);
$this->client = $client;
$config['params'] = [];
if (!empty($config['bucket'])) {
@@ -135,7 +147,7 @@ class S3EncryptionMultipartUploader extends MultipartUploader
// Defer encryption work until promise is executed
$envelope = new MetadataEnvelope();
list($this->source, $params) = Promise\promise_for($this->encrypt(
list($this->source, $params) = Promise\Create::promiseFor($this->encrypt(
$this->source,
$this->config['@cipheroptions'] ?: [],
$this->provider,

View File

@@ -0,0 +1,176 @@
<?php
namespace Aws\S3\Crypto;
use Aws\Crypto\AbstractCryptoClientV2;
use Aws\Crypto\EncryptionTraitV2;
use Aws\Crypto\MetadataEnvelope;
use Aws\Crypto\Cipher\CipherBuilderTrait;
use Aws\S3\MultipartUploader;
use Aws\S3\S3ClientInterface;
use GuzzleHttp\Promise;
/**
* Encapsulates the execution of a multipart upload of an encrypted object to S3.
*
* Note that for PHP versions of < 7.1, this class uses an AES-GCM polyfill
* for encryption since there is no native PHP support. The performance for large
* inputs will be a lot slower than for PHP 7.1+, so upgrading older PHP version
* environments may be necessary to use this effectively.
*/
class S3EncryptionMultipartUploaderV2 extends MultipartUploader
{
use CipherBuilderTrait;
use CryptoParamsTraitV2;
use EncryptionTraitV2;
use UserAgentTrait;
CONST CRYPTO_VERSION = '2.1';
/**
* Returns if the passed cipher name is supported for encryption by the SDK.
*
* @param string $cipherName The name of a cipher to verify is registered.
*
* @return bool If the cipher passed is in our supported list.
*/
public static function isSupportedCipher($cipherName)
{
return in_array($cipherName, AbstractCryptoClientV2::$supportedCiphers);
}
private $provider;
private $instructionFileSuffix;
private $strategy;
/**
* Creates a multipart upload for an S3 object after encrypting it.
*
* Note that for PHP versions of < 7.1, this class uses an AES-GCM polyfill
* for encryption since there is no native PHP support. The performance for
* large inputs will be a lot slower than for PHP 7.1+, so upgrading older
* PHP version environments may be necessary to use this effectively.
*
* The required configuration options are as follows:
*
* - @MaterialsProvider: (MaterialsProviderV2) Provides Cek, Iv, and Cek
* encrypting/decrypting for encryption metadata.
* - @CipherOptions: (array) Cipher options for encrypting data. A Cipher
* is required. Accepts the following options:
* - Cipher: (string) gcm
* See also: AbstractCryptoClientV2::$supportedCiphers
* - KeySize: (int) 128|256
* See also: MaterialsProvider::$supportedKeySizes
* - Aad: (string) Additional authentication data. This option is
* passed directly to OpenSSL when using gcm.
* - @KmsEncryptionContext: (array) Only required if using
* KmsMaterialsProviderV2. An associative array of key-value
* pairs to be added to the encryption context for KMS key encryption. An
* empty array may be passed if no additional context is desired.
* - bucket: (string) Name of the bucket to which the object is
* being uploaded.
* - key: (string) Key to use for the object being uploaded.
*
* The optional configuration arguments are as follows:
*
* - @MetadataStrategy: (MetadataStrategy|string|null) Strategy for storing
* MetadataEnvelope information. Defaults to using a
* HeadersMetadataStrategy. Can either be a class implementing
* MetadataStrategy, a class name of a predefined strategy, or empty/null
* to default.
* - @InstructionFileSuffix: (string|null) Suffix used when writing to an
* instruction file if an using an InstructionFileMetadataHandler was
* determined.
* - acl: (string) ACL to set on the object being upload. Objects are
* private by default.
* - before_complete: (callable) Callback to invoke before the
* `CompleteMultipartUpload` operation. The callback should have a
* function signature like `function (Aws\Command $command) {...}`.
* - before_initiate: (callable) Callback to invoke before the
* `CreateMultipartUpload` operation. The callback should have a function
* signature like `function (Aws\Command $command) {...}`.
* - before_upload: (callable) Callback to invoke before any `UploadPart`
* operations. The callback should have a function signature like
* `function (Aws\Command $command) {...}`.
* - concurrency: (int, default=int(5)) Maximum number of concurrent
* `UploadPart` operations allowed during the multipart upload.
* - params: (array) An array of key/value parameters that will be applied
* to each of the sub-commands run by the uploader as a base.
* Auto-calculated options will override these parameters. If you need
* more granularity over parameters to each sub-command, use the before_*
* options detailed above to update the commands directly.
* - part_size: (int, default=int(5242880)) Part size, in bytes, to use when
* doing a multipart upload. This must between 5 MB and 5 GB, inclusive.
* - state: (Aws\Multipart\UploadState) An object that represents the state
* of the multipart upload and that is used to resume a previous upload.
* When this option is provided, the `bucket`, `key`, and `part_size`
* options are ignored.
*
* @param S3ClientInterface $client Client used for the upload.
* @param mixed $source Source of the data to upload.
* @param array $config Configuration used to perform the upload.
*/
public function __construct(
S3ClientInterface $client,
$source,
array $config = []
) {
$this->appendUserAgent($client, 'feat/s3-encrypt/' . self::CRYPTO_VERSION);
$this->client = $client;
$config['params'] = [];
if (!empty($config['bucket'])) {
$config['params']['Bucket'] = $config['bucket'];
}
if (!empty($config['key'])) {
$config['params']['Key'] = $config['key'];
}
$this->provider = $this->getMaterialsProvider($config);
unset($config['@MaterialsProvider']);
$this->instructionFileSuffix = $this->getInstructionFileSuffix($config);
unset($config['@InstructionFileSuffix']);
$this->strategy = $this->getMetadataStrategy(
$config,
$this->instructionFileSuffix
);
if ($this->strategy === null) {
$this->strategy = self::getDefaultStrategy();
}
unset($config['@MetadataStrategy']);
$config['prepare_data_source'] = $this->getEncryptingDataPreparer();
parent::__construct($client, $source, $config);
}
private static function getDefaultStrategy()
{
return new HeadersMetadataStrategy();
}
private function getEncryptingDataPreparer()
{
return function() {
// Defer encryption work until promise is executed
$envelope = new MetadataEnvelope();
list($this->source, $params) = Promise\Create::promiseFor($this->encrypt(
$this->source,
$this->config ?: [],
$this->provider,
$envelope
))->then(
function ($bodyStream) use ($envelope) {
$params = $this->strategy->save(
$envelope,
$this->config['params']
);
return [$bodyStream, $params];
}
)->wait();
$this->source->rewind();
$this->config['params'] = $params;
};
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Aws\S3\Crypto;
use Aws\AwsClientInterface;
use Aws\Middleware;
use Psr\Http\Message\RequestInterface;
trait UserAgentTrait
{
private function appendUserAgent(AwsClientInterface $client, $agentString)
{
$list = $client->getHandlerList();
$list->appendBuild(Middleware::mapRequest(
function(RequestInterface $req) use ($agentString) {
if (!empty($req->getHeader('User-Agent'))
&& !empty($req->getHeader('User-Agent')[0])
) {
$userAgent = $req->getHeader('User-Agent')[0];
if (strpos($userAgent, $agentString) === false) {
$userAgent .= " {$agentString}";
};
} else {
$userAgent = $agentString;
}
$req = $req->withHeader('User-Agent', $userAgent);
return $req;
}
));
}
}

View File

@@ -0,0 +1,106 @@
<?php
namespace Aws\S3;
use Aws\Api\Service;
use Aws\Arn\ArnInterface;
use Aws\Arn\S3\OutpostsArnInterface;
use Aws\Endpoint\PartitionEndpointProvider;
use Aws\Exception\InvalidRegionException;
/**
* @internal
*/
trait EndpointRegionHelperTrait
{
/** @var array */
private $config;
/** @var PartitionEndpointProvider */
private $partitionProvider;
/** @var string */
private $region;
/** @var Service */
private $service;
private function getPartitionSuffix(
ArnInterface $arn,
PartitionEndpointProvider $provider
) {
$partition = $provider->getPartition(
$arn->getRegion(),
$arn->getService()
);
return $partition->getDnsSuffix();
}
private function getSigningRegion(
$region,
$service,
PartitionEndpointProvider $provider
) {
$partition = $provider->getPartition($region, $service);
$data = $partition->toArray();
if (isset($data['services'][$service]['endpoints'][$region]['credentialScope']['region'])) {
return $data['services'][$service]['endpoints'][$region]['credentialScope']['region'];
}
return $region;
}
private function isMatchingSigningRegion(
$arnRegion,
$clientRegion,
$service,
PartitionEndpointProvider $provider
) {
$arnRegion = \Aws\strip_fips_pseudo_regions(strtolower($arnRegion));
$clientRegion = strtolower($clientRegion);
if ($arnRegion === $clientRegion) {
return true;
}
if ($this->getSigningRegion($clientRegion, $service, $provider) === $arnRegion) {
return true;
}
return false;
}
private function validateFipsConfigurations(ArnInterface $arn)
{
$useFipsEndpoint = !empty($this->config['use_fips_endpoint']);
if ($arn instanceof OutpostsArnInterface) {
if (empty($this->config['use_arn_region'])
|| !($this->config['use_arn_region']->isUseArnRegion())
) {
$region = $this->region;
} else {
$region = $arn->getRegion();
}
if (\Aws\is_fips_pseudo_region($region)) {
throw new InvalidRegionException(
'Fips is currently not supported with S3 Outposts access'
. ' points. Please provide a non-fips region or do not supply an'
. ' access point ARN.');
}
}
}
private function validateMatchingRegion(ArnInterface $arn)
{
if (!($this->isMatchingSigningRegion(
$arn->getRegion(),
$this->region,
$this->service->getEndpointPrefix(),
$this->partitionProvider)
)) {
if (empty($this->config['use_arn_region'])
|| !($this->config['use_arn_region']->isUseArnRegion())
) {
throw new InvalidRegionException('The region'
. " specified in the ARN (" . $arn->getRegion()
. ") does not match the client region ("
. "{$this->region}).");
}
}
}
}

View File

@@ -1,6 +1,8 @@
<?php
namespace Aws\S3;
use Aws\Arn\ArnParser;
use Aws\Multipart\AbstractUploadManager;
use Aws\ResultInterface;
use GuzzleHttp\Psr7;
@@ -9,8 +11,10 @@ class MultipartCopy extends AbstractUploadManager
{
use MultipartUploadingTrait;
/** @var string */
/** @var string|array */
private $source;
/** @var string */
private $sourceVersionId;
/** @var ResultInterface */
private $sourceMetadata;
@@ -50,19 +54,26 @@ class MultipartCopy extends AbstractUploadManager
* result of executing a HeadObject command on the copy source.
*
* @param S3ClientInterface $client Client used for the upload.
* @param string $source Location of the data to be copied
* (in the form /<bucket>/<key>).
* @param array $config Configuration used to perform the upload.
* @param string|array $source Location of the data to be copied (in the
* form /<bucket>/<key>). If the key contains a '?'
* character, instead pass an array of source_key,
* source_bucket, and source_version_id.
* @param array $config Configuration used to perform the upload.
*/
public function __construct(
S3ClientInterface $client,
$source,
array $config = []
) {
$this->source = '/' . ltrim($source, '/');
parent::__construct($client, array_change_key_case($config) + [
'source_metadata' => null
]);
if (is_array($source)) {
$this->source = $source;
} else {
$this->source = $this->getInputSource($source);
}
parent::__construct(
$client,
array_change_key_case($config) + ['source_metadata' => null]
);
}
/**
@@ -80,12 +91,12 @@ class MultipartCopy extends AbstractUploadManager
return [
'command' => [
'initiate' => 'CreateMultipartUpload',
'upload' => 'UploadPartCopy',
'upload' => 'UploadPartCopy',
'complete' => 'CompleteMultipartUpload',
],
'id' => [
'bucket' => 'Bucket',
'key' => 'Key',
'bucket' => 'Bucket',
'key' => 'Key',
'upload_id' => 'UploadId',
],
'part_num' => 'PartNumber',
@@ -101,8 +112,7 @@ class MultipartCopy extends AbstractUploadManager
if (!$this->state->hasPartBeenUploaded($partNumber)) {
$command = $this->client->getCommand(
$this->info['command']['upload'],
$this->createPart($partNumber, $parts)
+ $this->getState()->getId()
$this->createPart($partNumber, $parts) + $this->getState()->getId()
);
$command->getHandlerList()->appendSign($resultHandler, 'mup');
yield $command;
@@ -120,9 +130,26 @@ class MultipartCopy extends AbstractUploadManager
foreach ($params as $k => $v) {
$data[$k] = $v;
}
// The source parameter here is usually a string, but can be overloaded as an array
// if the key contains a '?' character to specify where the query parameters start
if (is_array($this->source)) {
$key = str_replace('%2F', '/', rawurlencode($this->source['source_key']));
$data['CopySource'] = '/' . $this->source['source_bucket'] . '/' . $key;
} else {
$data['CopySource'] = $this->source;
list($bucket, $key) = explode('/', ltrim($this->source, '/'), 2);
$data['CopySource'] = '/' . $bucket . '/' . implode(
'/',
array_map(
'urlencode',
explode('/', rawurldecode($key))
)
);
}
$data['PartNumber'] = $partNumber;
if (!empty($this->sourceVersionId)) {
$data['CopySource'] .= "?versionId=" . $this->sourceVersionId;
}
$defaultPartSize = $this->determinePartSize();
$startByte = $defaultPartSize * ($partNumber - 1);
@@ -164,20 +191,52 @@ class MultipartCopy extends AbstractUploadManager
if ($this->config['source_metadata'] instanceof ResultInterface) {
return $this->config['source_metadata'];
}
list($bucket, $key) = explode('/', ltrim($this->source, '/'), 2);
$headParams = [
'Bucket' => $bucket,
'Key' => $key,
];
if (strpos($key, '?')) {
list($key, $query) = explode('?', $key, 2);
$headParams['Key'] = $key;
$query = Psr7\parse_query($query, false);
if (isset($query['versionId'])) {
$headParams['VersionId'] = $query['versionId'];
//if the source variable was overloaded with an array, use the inputs for key and bucket
if (is_array($this->source)) {
$headParams = [
'Key' => $this->source['source_key'],
'Bucket' => $this->source['source_bucket']
];
if (isset($this->source['source_version_id'])) {
$this->sourceVersionId = $this->source['source_version_id'];
$headParams['VersionId'] = $this->sourceVersionId;
}
//otherwise, use the default source parsing behavior
} else {
list($bucket, $key) = explode('/', ltrim($this->source, '/'), 2);
$headParams = [
'Bucket' => $bucket,
'Key' => $key,
];
if (strpos($key, '?')) {
list($key, $query) = explode('?', $key, 2);
$headParams['Key'] = $key;
$query = Psr7\Query::parse($query, false);
if (isset($query['versionId'])) {
$this->sourceVersionId = $query['versionId'];
$headParams['VersionId'] = $this->sourceVersionId;
}
}
}
return $this->client->headObject($headParams);
}
/**
* Get the url decoded input source, starting with a slash if it is not an
* ARN to standardize the source location syntax.
*
* @param string $inputSource The source that was passed to the constructor
* @return string The source, starting with a slash if it's not an arn
*/
private function getInputSource($inputSource)
{
if (ArnParser::isArn($inputSource)) {
$sourceBuilder = '';
} else {
$sourceBuilder = "/";
}
$sourceBuilder .= ltrim(rawurldecode($inputSource), '/');
return $sourceBuilder;
}
}

View File

@@ -37,7 +37,7 @@ class MultipartUploader extends AbstractUploader
* operations. The callback should have a function signature like
* `function (Aws\Command $command) {...}`.
* - bucket: (string, required) Name of the bucket to which the object is
* being uploaded.
* being uploaded, or an S3 access point ARN.
* - concurrency: (int, default=int(5)) Maximum number of concurrent
* `UploadPart` operations allowed during the multipart upload.
* - key: (string, required) Key to use for the object being uploaded.
@@ -113,8 +113,8 @@ class MultipartUploader extends AbstractUploader
// Case 2: Stream is not seekable; must store in temp stream.
$source = $this->limitPartStream($this->source);
$source = $this->decorateWithHashes($source, $data);
$body = Psr7\stream_for();
Psr7\copy_to_stream($source, $body);
$body = Psr7\Utils::streamFor();
Psr7\Utils::copyToStream($source, $body);
}
$contentLength = $body->getSize();
@@ -139,7 +139,7 @@ class MultipartUploader extends AbstractUploader
protected function getSourceMimeType()
{
if ($uri = $this->source->getMetadata('uri')) {
return Psr7\mimetype_from_filename($uri)
return Psr7\MimeType::fromFilename($uri)
?: 'application/octet-stream';
}
}

View File

@@ -1,9 +1,12 @@
<?php
namespace Aws\S3;
use Aws\Arn\ArnParser;
use Aws\Arn\S3\AccessPointArn;
use Aws\Exception\MultipartUploadException;
use Aws\Result;
use Aws\S3\Exception\S3Exception;
use GuzzleHttp\Promise\Coroutine;
use GuzzleHttp\Promise\PromisorInterface;
use InvalidArgumentException;
@@ -73,10 +76,12 @@ class ObjectCopier implements PromisorInterface
* Perform the configured copy asynchronously. Returns a promise that is
* fulfilled with the result of the CompleteMultipartUpload or CopyObject
* operation or rejected with an exception.
*
* @return Coroutine
*/
public function promise()
{
return \GuzzleHttp\Promise\coroutine(function () {
return Coroutine::of(function () {
$headObjectCommand = $this->client->getCommand(
'HeadObject',
$this->options['params'] + $this->source
@@ -139,8 +144,20 @@ class ObjectCopier implements PromisorInterface
private function getSourcePath()
{
$sourcePath = "/{$this->source['Bucket']}/"
. rawurlencode($this->source['Key']);
if (ArnParser::isArn($this->source['Bucket'])) {
try {
new AccessPointArn($this->source['Bucket']);
} catch (\Exception $e) {
throw new \InvalidArgumentException(
'Provided ARN was a not a valid S3 access point ARN ('
. $e->getMessage() . ')',
0,
$e
);
}
}
$sourcePath = "/{$this->source['Bucket']}/" . rawurlencode($this->source['Key']);
if (isset($this->source['VersionId'])) {
$sourcePath .= "?versionId={$this->source['VersionId']}";
}

View File

@@ -1,6 +1,7 @@
<?php
namespace Aws\S3;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Promise\PromisorInterface;
use GuzzleHttp\Psr7;
use Psr\Http\Message\StreamInterface;
@@ -30,7 +31,8 @@ class ObjectUploader implements PromisorInterface
/**
* @param S3ClientInterface $client The S3 Client used to execute
* the upload command(s).
* @param string $bucket Bucket to upload the object.
* @param string $bucket Bucket to upload the object, or
* an S3 access point ARN.
* @param string $key Key of the object.
* @param mixed $body Object data to upload. Can be a
* StreamInterface, PHP stream
@@ -54,11 +56,14 @@ class ObjectUploader implements PromisorInterface
$this->client = $client;
$this->bucket = $bucket;
$this->key = $key;
$this->body = Psr7\stream_for($body);
$this->body = Psr7\Utils::streamFor($body);
$this->acl = $acl;
$this->options = $options + self::$defaults;
}
/**
* @return PromiseInterface
*/
public function promise()
{
/** @var int $mup_threshold */
@@ -112,8 +117,8 @@ class ObjectUploader implements PromisorInterface
* Read up to 5MB into a buffer to determine how to upload the body.
* @var StreamInterface $buffer
*/
$buffer = Psr7\stream_for();
Psr7\copy_to_stream($body, $buffer, MultipartUploader::PART_MIN_SIZE);
$buffer = Psr7\Utils::streamFor();
Psr7\Utils::copyToStream($body, $buffer, MultipartUploader::PART_MIN_SIZE);
// If body < 5MB, use PutObject with the buffer.
if ($buffer->getSize() < MultipartUploader::PART_MIN_SIZE) {
@@ -123,7 +128,7 @@ class ObjectUploader implements PromisorInterface
}
// If body >= 5 MB, then use multipart. [YES]
if ($body->isSeekable()) {
if ($body->isSeekable() && $body->getMetadata('uri') !== 'php://input') {
// If the body is seekable, just rewind the body.
$body->seek(0);
} else {

View File

@@ -56,7 +56,7 @@ class PostObjectV4
$credentials = $this->client->getCredentials()->wait();
if ($securityToken = $credentials->getSecurityToken()) {
array_push($options, ['x-amz-security-token' => $securityToken]);
$options [] = ['x-amz-security-token' => $securityToken];
$formInputs['X-Amz-Security-Token'] = $securityToken;
}

View File

@@ -44,7 +44,9 @@ class PutObjectUrlMiddleware
switch ($name) {
case 'PutObject':
case 'CopyObject':
$result['ObjectURL'] = $result['@metadata']['effectiveUri'];
$result['ObjectURL'] = isset($result['@metadata']['effectiveUri'])
? $result['@metadata']['effectiveUri']
: null;
break;
case 'CompleteMultipartUpload':
$result['ObjectURL'] = $result['Location'];

View File

@@ -0,0 +1,42 @@
<?php
namespace Aws\S3\RegionalEndpoint;
class Configuration implements ConfigurationInterface
{
private $endpointsType;
private $isFallback;
public function __construct($endpointsType, $isFallback = false)
{
$this->endpointsType = strtolower($endpointsType);
$this->isFallback = $isFallback;
if (!in_array($this->endpointsType, ['legacy', 'regional'])) {
throw new \InvalidArgumentException(
"Configuration parameter must either be 'legacy' or 'regional'."
);
}
}
/**
* {@inheritdoc}
*/
public function getEndpointsType()
{
return $this->endpointsType;
}
/**
* {@inheritdoc}
*/
public function toArray()
{
return [
'endpoints_type' => $this->getEndpointsType()
];
}
public function isFallback()
{
return $this->isFallback;
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Aws\S3\RegionalEndpoint;
/**
* Provides access to S3 regional endpoints configuration options: endpoints_type
*/
interface ConfigurationInterface
{
/**
* Returns the endpoints type
*
* @return string
*/
public function getEndpointsType();
/**
* Returns the configuration as an associative array
*
* @return array
*/
public function toArray();
}

View File

@@ -0,0 +1,195 @@
<?php
namespace Aws\S3\RegionalEndpoint;
use Aws\AbstractConfigurationProvider;
use Aws\CacheInterface;
use Aws\ConfigurationProviderInterface;
use Aws\S3\RegionalEndpoint\Exception\ConfigurationException;
use GuzzleHttp\Promise;
/**
* A configuration provider is a function that returns a promise that is
* fulfilled with a {@see \Aws\S3\RegionalEndpoint\ConfigurationInterface}
* or rejected with an {@see \Aws\S3\RegionalEndpoint\Exception\ConfigurationException}.
*
* <code>
* use Aws\S3\RegionalEndpoint\ConfigurationProvider;
* $provider = ConfigurationProvider::defaultProvider();
* // Returns a ConfigurationInterface or throws.
* $config = $provider()->wait();
* </code>
*
* Configuration providers can be composed to create configuration using
* conditional logic that can create different configurations in different
* environments. You can compose multiple providers into a single provider using
* {@see \Aws\S3\RegionalEndpoint\ConfigurationProvider::chain}. This function
* accepts providers as variadic arguments and returns a new function that will
* invoke each provider until a successful configuration is returned.
*
* <code>
* // First try an INI file at this location.
* $a = ConfigurationProvider::ini(null, '/path/to/file.ini');
* // Then try an INI file at this location.
* $b = ConfigurationProvider::ini(null, '/path/to/other-file.ini');
* // Then try loading from environment variables.
* $c = ConfigurationProvider::env();
* // Combine the three providers together.
* $composed = ConfigurationProvider::chain($a, $b, $c);
* // Returns a promise that is fulfilled with a configuration or throws.
* $promise = $composed();
* // Wait on the configuration to resolve.
* $config = $promise->wait();
* </code>
*/
class ConfigurationProvider extends AbstractConfigurationProvider
implements ConfigurationProviderInterface
{
const ENV_ENDPOINTS_TYPE = 'AWS_S3_US_EAST_1_REGIONAL_ENDPOINT';
const INI_ENDPOINTS_TYPE = 's3_us_east_1_regional_endpoint';
const DEFAULT_ENDPOINTS_TYPE = 'legacy';
public static $cacheKey = 'aws_s3_us_east_1_regional_endpoint_config';
protected static $interfaceClass = ConfigurationInterface::class;
protected static $exceptionClass = ConfigurationException::class;
/**
* Create a default config provider that first checks for environment
* variables, then checks for a specified profile in the environment-defined
* config file location (env variable is 'AWS_CONFIG_FILE', file location
* defaults to ~/.aws/config), then checks for the "default" profile in the
* environment-defined config file location, and failing those uses a default
* fallback set of configuration options.
*
* This provider is automatically wrapped in a memoize function that caches
* previously provided config options.
*
* @param array $config
*
* @return callable
*/
public static function defaultProvider(array $config = [])
{
$configProviders = [self::env()];
if (
!isset($config['use_aws_shared_config_files'])
|| $config['use_aws_shared_config_files'] != false
) {
$configProviders[] = self::ini();
}
$configProviders[] = self::fallback();
$memo = self::memoize(
call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders)
);
if (isset($config['s3_us_east_1_regional_endpoint'])
&& $config['s3_us_east_1_regional_endpoint'] instanceof CacheInterface
) {
return self::cache($memo, $config['s3_us_east_1_regional_endpoint'], self::$cacheKey);
}
return $memo;
}
public static function env()
{
return function () {
// Use config from environment variables, if available
$endpointsType = getenv(self::ENV_ENDPOINTS_TYPE);
if (!empty($endpointsType)) {
return Promise\Create::promiseFor(
new Configuration($endpointsType)
);
}
return self::reject('Could not find environment variable config'
. ' in ' . self::ENV_ENDPOINTS_TYPE);
};
}
/**
* Config provider that creates config using a config file whose location
* is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to
* ~/.aws/config if not specified
*
* @param string|null $profile Profile to use. If not specified will use
* the "default" profile.
* @param string|null $filename If provided, uses a custom filename rather
* than looking in the default directory.
*
* @return callable
*/
public static function ini(
$profile = null,
$filename = null
) {
$filename = $filename ?: (self::getDefaultConfigFilename());
$profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default');
return function () use ($profile, $filename) {
if (!@is_readable($filename)) {
return self::reject("Cannot read configuration from $filename");
}
$data = \Aws\parse_ini_file($filename, true);
if ($data === false) {
return self::reject("Invalid config file: $filename");
}
if (!isset($data[$profile])) {
return self::reject("'$profile' not found in config file");
}
if (!isset($data[$profile][self::INI_ENDPOINTS_TYPE])) {
return self::reject("Required S3 regional endpoint config values
not present in INI profile '{$profile}' ({$filename})");
}
return Promise\Create::promiseFor(
new Configuration($data[$profile][self::INI_ENDPOINTS_TYPE])
);
};
}
/**
* Fallback config options when other sources are not set.
*
* @return callable
*/
public static function fallback()
{
return function () {
return Promise\Create::promiseFor(
new Configuration(self::DEFAULT_ENDPOINTS_TYPE, true)
);
};
}
/**
* Unwraps a configuration object in whatever valid form it is in,
* always returning a ConfigurationInterface object.
*
* @param mixed $config
* @return ConfigurationInterface
* @throws \InvalidArgumentException
*/
public static function unwrap($config)
{
if (is_callable($config)) {
$config = $config();
}
if ($config instanceof Promise\PromiseInterface) {
$config = $config->wait();
}
if ($config instanceof ConfigurationInterface) {
return $config;
}
if (is_string($config)) {
return new Configuration($config);
}
if (is_array($config) && isset($config['endpoints_type'])) {
return new Configuration($config['endpoints_type']);
}
throw new \InvalidArgumentException('Not a valid S3 regional endpoint '
. 'configuration argument.');
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Aws\S3\RegionalEndpoint\Exception;
use Aws\HasMonitoringEventsTrait;
use Aws\MonitoringEventsInterface;
/**
* Represents an error interacting with configuration for sts regional endpoints
*/
class ConfigurationException extends \RuntimeException implements
MonitoringEventsInterface
{
use HasMonitoringEventsTrait;
}

View File

@@ -5,15 +5,25 @@ use Aws\Api\ApiProvider;
use Aws\Api\DocModel;
use Aws\Api\Service;
use Aws\AwsClient;
use Aws\CacheInterface;
use Aws\ClientResolver;
use Aws\Command;
use Aws\Exception\AwsException;
use Aws\HandlerList;
use Aws\InputValidationMiddleware;
use Aws\Middleware;
use Aws\Retry\QuotaManager;
use Aws\RetryMiddleware;
use Aws\ResultInterface;
use Aws\CommandInterface;
use Aws\RetryMiddlewareV2;
use Aws\S3\UseArnRegion\Configuration;
use Aws\S3\UseArnRegion\ConfigurationInterface;
use Aws\S3\UseArnRegion\ConfigurationProvider as UseArnRegionConfigurationProvider;
use Aws\S3\RegionalEndpoint\ConfigurationProvider;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\RequestInterface;
/**
@@ -37,12 +47,16 @@ use Psr\Http\Message\RequestInterface;
* @method \GuzzleHttp\Promise\Promise deleteBucketCorsAsync(array $args = [])
* @method \Aws\Result deleteBucketEncryption(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketEncryptionAsync(array $args = [])
* @method \Aws\Result deleteBucketIntelligentTieringConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketIntelligentTieringConfigurationAsync(array $args = [])
* @method \Aws\Result deleteBucketInventoryConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketInventoryConfigurationAsync(array $args = [])
* @method \Aws\Result deleteBucketLifecycle(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketLifecycleAsync(array $args = [])
* @method \Aws\Result deleteBucketMetricsConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketMetricsConfigurationAsync(array $args = [])
* @method \Aws\Result deleteBucketOwnershipControls(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketOwnershipControlsAsync(array $args = [])
* @method \Aws\Result deleteBucketPolicy(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketPolicyAsync(array $args = [])
* @method \Aws\Result deleteBucketReplication(array $args = [])
@@ -69,6 +83,8 @@ use Psr\Http\Message\RequestInterface;
* @method \GuzzleHttp\Promise\Promise getBucketCorsAsync(array $args = [])
* @method \Aws\Result getBucketEncryption(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketEncryptionAsync(array $args = [])
* @method \Aws\Result getBucketIntelligentTieringConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketIntelligentTieringConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketInventoryConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketInventoryConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketLifecycle(array $args = [])
@@ -85,6 +101,8 @@ use Psr\Http\Message\RequestInterface;
* @method \GuzzleHttp\Promise\Promise getBucketNotificationAsync(array $args = [])
* @method \Aws\Result getBucketNotificationConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketNotificationConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketOwnershipControls(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketOwnershipControlsAsync(array $args = [])
* @method \Aws\Result getBucketPolicy(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketPolicyAsync(array $args = [])
* @method \Aws\Result getBucketPolicyStatus(array $args = [])
@@ -103,6 +121,8 @@ use Psr\Http\Message\RequestInterface;
* @method \GuzzleHttp\Promise\Promise getObjectAsync(array $args = [])
* @method \Aws\Result getObjectAcl(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectAclAsync(array $args = [])
* @method \Aws\Result getObjectAttributes(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectAttributesAsync(array $args = [])
* @method \Aws\Result getObjectLegalHold(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectLegalHoldAsync(array $args = [])
* @method \Aws\Result getObjectLockConfiguration(array $args = [])
@@ -121,6 +141,8 @@ use Psr\Http\Message\RequestInterface;
* @method \GuzzleHttp\Promise\Promise headObjectAsync(array $args = [])
* @method \Aws\Result listBucketAnalyticsConfigurations(array $args = [])
* @method \GuzzleHttp\Promise\Promise listBucketAnalyticsConfigurationsAsync(array $args = [])
* @method \Aws\Result listBucketIntelligentTieringConfigurations(array $args = [])
* @method \GuzzleHttp\Promise\Promise listBucketIntelligentTieringConfigurationsAsync(array $args = [])
* @method \Aws\Result listBucketInventoryConfigurations(array $args = [])
* @method \GuzzleHttp\Promise\Promise listBucketInventoryConfigurationsAsync(array $args = [])
* @method \Aws\Result listBucketMetricsConfigurations(array $args = [])
@@ -147,6 +169,8 @@ use Psr\Http\Message\RequestInterface;
* @method \GuzzleHttp\Promise\Promise putBucketCorsAsync(array $args = [])
* @method \Aws\Result putBucketEncryption(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketEncryptionAsync(array $args = [])
* @method \Aws\Result putBucketIntelligentTieringConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketIntelligentTieringConfigurationAsync(array $args = [])
* @method \Aws\Result putBucketInventoryConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketInventoryConfigurationAsync(array $args = [])
* @method \Aws\Result putBucketLifecycle(array $args = [])
@@ -161,6 +185,8 @@ use Psr\Http\Message\RequestInterface;
* @method \GuzzleHttp\Promise\Promise putBucketNotificationAsync(array $args = [])
* @method \Aws\Result putBucketNotificationConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketNotificationConfigurationAsync(array $args = [])
* @method \Aws\Result putBucketOwnershipControls(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketOwnershipControlsAsync(array $args = [])
* @method \Aws\Result putBucketPolicy(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketPolicyAsync(array $args = [])
* @method \Aws\Result putBucketReplication(array $args = [])
@@ -195,11 +221,16 @@ use Psr\Http\Message\RequestInterface;
* @method \GuzzleHttp\Promise\Promise uploadPartAsync(array $args = [])
* @method \Aws\Result uploadPartCopy(array $args = [])
* @method \GuzzleHttp\Promise\Promise uploadPartCopyAsync(array $args = [])
* @method \Aws\Result writeGetObjectResponse(array $args = [])
* @method \GuzzleHttp\Promise\Promise writeGetObjectResponseAsync(array $args = [])
*/
class S3Client extends AwsClient implements S3ClientInterface
{
use S3ClientTrait;
/** @var array */
private static $mandatoryAttributes = ['Bucket', 'Key'];
public static function getArguments()
{
$args = parent::getArguments();
@@ -215,6 +246,19 @@ class S3Client extends AwsClient implements S3ClientInterface
. 'result of injecting the bucket into the URL. This '
. 'option is useful for interacting with CNAME endpoints.',
],
'use_arn_region' => [
'type' => 'config',
'valid' => [
'bool',
Configuration::class,
CacheInterface::class,
'callable'
],
'doc' => 'Set to true to allow passed in ARNs to override'
. ' client region. Accepts...',
'fn' => [__CLASS__, '_apply_use_arn_region'],
'default' => [UseArnRegionConfigurationProvider::class, 'defaultProvider'],
],
'use_accelerate_endpoint' => [
'type' => 'config',
'valid' => ['bool'],
@@ -226,15 +270,6 @@ class S3Client extends AwsClient implements S3ClientInterface
. ' be accessed via an Accelerate endpoint.',
'default' => false,
],
'use_dual_stack_endpoint' => [
'type' => 'config',
'valid' => ['bool'],
'doc' => 'Set to true to send requests to an S3 Dual Stack'
. ' endpoint by default, which enables IPv6 Protocol.'
. ' Can be enabled or disabled on individual operations by setting'
. ' \'@use_dual_stack_endpoint\' to true or false.',
'default' => false,
],
'use_path_style_endpoint' => [
'type' => 'config',
'valid' => ['bool'],
@@ -244,6 +279,15 @@ class S3Client extends AwsClient implements S3ClientInterface
. ' \'@use_path_style_endpoint\' to true or false.',
'default' => false,
],
'disable_multiregion_access_points' => [
'type' => 'config',
'valid' => ['bool'],
'doc' => 'Set to true to disable the usage of'
. ' multi region access points. These are enabled by default.'
. ' Can be enabled or disabled on individual operations by setting'
. ' \'@disable_multiregion_access_points\' to true or false.',
'default' => false,
],
];
}
@@ -260,11 +304,28 @@ class S3Client extends AwsClient implements S3ClientInterface
* interacting with CNAME endpoints.
* - calculate_md5: (bool) Set to false to disable calculating an MD5
* for all Amazon S3 signed uploads.
* - s3_us_east_1_regional_endpoint:
* (Aws\S3\RegionalEndpoint\ConfigurationInterface|Aws\CacheInterface\|callable|string|array)
* Specifies whether to use regional or legacy endpoints for the us-east-1
* region. Provide an Aws\S3\RegionalEndpoint\ConfigurationInterface object, an
* instance of Aws\CacheInterface, a callable configuration provider used
* to create endpoint configuration, a string value of `legacy` or
* `regional`, or an associative array with the following keys:
* endpoint_types: (string) Set to `legacy` or `regional`, defaults to
* `legacy`
* - use_accelerate_endpoint: (bool) Set to true to send requests to an S3
* Accelerate endpoint by default. Can be enabled or disabled on
* individual operations by setting '@use_accelerate_endpoint' to true or
* false. Note: you must enable S3 Accelerate on a bucket before it can be
* accessed via an Accelerate endpoint.
* - use_arn_region: (Aws\S3\UseArnRegion\ConfigurationInterface,
* Aws\CacheInterface, bool, callable) Set to true to enable the client
* to use the region from a supplied ARN argument instead of the client's
* region. Provide an instance of Aws\S3\UseArnRegion\ConfigurationInterface,
* an instance of Aws\CacheInterface, a callable that provides a promise for
* a Configuration object, or a boolean value. Defaults to false (i.e.
* the SDK will not follow the ARN region if it conflicts with the client
* region and instead throw an error).
* - use_dual_stack_endpoint: (bool) Set to true to send requests to an S3
* Dual Stack endpoint by default, which enables IPv6 Protocol.
* Can be enabled or disabled on individual operations by setting
@@ -275,38 +336,78 @@ class S3Client extends AwsClient implements S3ClientInterface
* Can be enabled or disabled on individual operations by setting
* '@use_path_style_endpoint\' to true or false. Note:
* you cannot use it together with an accelerate endpoint.
* - disable_multiregion_access_points: (bool) Set to true to disable
* sending multi region requests. They are enabled by default.
* Can be enabled or disabled on individual operations by setting
* '@disable_multiregion_access_points\' to true or false. Note:
* you cannot use it together with an accelerate or dualstack endpoint.
*
* @param array $args
*/
public function __construct(array $args)
{
if (
!isset($args['s3_us_east_1_regional_endpoint'])
|| $args['s3_us_east_1_regional_endpoint'] instanceof CacheInterface
) {
$args['s3_us_east_1_regional_endpoint'] = ConfigurationProvider::defaultProvider($args);
}
$this->addBuiltIns($args);
parent::__construct($args);
$stack = $this->getHandlerList();
$stack->appendInit(SSECMiddleware::wrap($this->getEndpoint()->getScheme()), 's3.ssec');
$stack->appendBuild(ApplyChecksumMiddleware::wrap(), 's3.checksum');
$stack->appendBuild(ApplyChecksumMiddleware::wrap($this->getApi()), 's3.checksum');
$stack->appendBuild(
Middleware::contentType(['PutObject', 'UploadPart']),
's3.content_type'
);
// Use the bucket style middleware when using a "bucket_endpoint" (for cnames)
if ($this->getConfig('bucket_endpoint')) {
$stack->appendBuild(BucketEndpointMiddleware::wrap(), 's3.bucket_endpoint');
} else {
} elseif (!$this->isUseEndpointV2()) {
$stack->appendBuild(
S3EndpointMiddleware::wrap(
$this->getRegion(),
$this->getConfig('endpoint_provider'),
[
'dual_stack' => $this->getConfig('use_dual_stack_endpoint'),
'accelerate' => $this->getConfig('use_accelerate_endpoint'),
'path_style' => $this->getConfig('use_path_style_endpoint')
'path_style' => $this->getConfig('use_path_style_endpoint'),
'use_fips_endpoint' => $this->getConfig('use_fips_endpoint'),
'dual_stack' =>
$this->getConfig('use_dual_stack_endpoint')->isUseDualStackEndpoint(),
]
),
's3.endpoint_middleware'
);
}
$stack->appendBuild(
BucketEndpointArnMiddleware::wrap(
$this->getApi(),
$this->getRegion(),
[
'use_arn_region' => $this->getConfig('use_arn_region'),
'accelerate' => $this->getConfig('use_accelerate_endpoint'),
'path_style' => $this->getConfig('use_path_style_endpoint'),
'dual_stack' =>
$this->getConfig('use_dual_stack_endpoint')->isUseDualStackEndpoint(),
'use_fips_endpoint' => $this->getConfig('use_fips_endpoint'),
'disable_multiregion_access_points' =>
$this->getConfig('disable_multiregion_access_points'),
'endpoint' => isset($args['endpoint'])
? $args['endpoint']
: null
],
$this->isUseEndpointV2()
),
's3.bucket_endpoint_arn'
);
$stack->appendValidate(
InputValidationMiddleware::wrap($this->getApi(), self::$mandatoryAttributes),
'input_validation_middleware'
);
$stack->appendSign(PutObjectUrlMiddleware::wrap(), 's3.put_object_url');
$stack->appendSign(PermanentRedirectMiddleware::wrap(), 's3.permanent_redirect');
$stack->appendInit(Middleware::sourceFile($this->getApi()), 's3.source_file');
@@ -314,6 +415,9 @@ class S3Client extends AwsClient implements S3ClientInterface
$stack->appendInit($this->getLocationConstraintMiddleware(), 's3.location');
$stack->appendInit($this->getEncodingTypeMiddleware(), 's3.auto_encode');
$stack->appendInit($this->getHeadObjectMiddleware(), 's3.head_object');
if ($this->isUseEndpointV2()) {
$this->processEndpointV2Model();
}
}
/**
@@ -329,6 +433,9 @@ class S3Client extends AwsClient implements S3ClientInterface
*/
public static function isBucketDnsCompatible($bucket)
{
if (!is_string($bucket)) {
return false;
}
$bucketLen = strlen($bucket);
return ($bucketLen >= 3 && $bucketLen <= 63) &&
@@ -337,26 +444,61 @@ class S3Client extends AwsClient implements S3ClientInterface
preg_match('/^[a-z0-9]([a-z0-9\-\.]*[a-z0-9])?$/', $bucket);
}
public function createPresignedRequest(CommandInterface $command, $expires)
public static function _apply_use_arn_region($value, array &$args, HandlerList $list)
{
if ($value instanceof CacheInterface) {
$value = UseArnRegionConfigurationProvider::defaultProvider($args);
}
if (is_callable($value)) {
$value = $value();
}
if ($value instanceof PromiseInterface) {
$value = $value->wait();
}
if ($value instanceof ConfigurationInterface) {
$args['use_arn_region'] = $value;
} else {
// The Configuration class itself will validate other inputs
$args['use_arn_region'] = new Configuration($value);
}
}
public function createPresignedRequest(CommandInterface $command, $expires, array $options = [])
{
$command = clone $command;
$command->getHandlerList()->remove('signer');
$request = \Aws\serialize($command);
$signing_name = $this->getSigningName($request->getUri()->getHost());
/** @var \Aws\Signature\SignatureInterface $signer */
$signer = call_user_func(
$this->getSignatureProvider(),
$this->getConfig('signature_version'),
$this->getConfig('signing_name'),
$signing_name,
$this->getConfig('signing_region')
);
return $signer->presign(
\Aws\serialize($command),
$request,
$this->getCredentials()->wait(),
$expires
$expires,
$options
);
}
/**
* Returns the URL to an object identified by its bucket and key.
*
* The URL returned by this method is not signed nor does it ensure that the
* bucket and key given to the method exist. If you need a signed URL, then
* use the {@see \Aws\S3\S3Client::createPresignedRequest} method and get
* the URI of the signed request.
*
* @param string $bucket The name of the bucket where the object is located
* @param string $key The key of the object
*
* @return string The URL to the object
*/
public function getObjectUrl($bucket, $key)
{
$command = $this->getCommand('GetObject', [
@@ -505,62 +647,185 @@ class S3Client extends AwsClient implements S3ClientInterface
};
}
/** @internal */
public static function _applyRetryConfig($value, $_, HandlerList $list)
/**
* Special handling for when the service name is s3-object-lambda.
* So, if the host contains s3-object-lambda, then the service name
* returned is s3-object-lambda, otherwise the default signing service is returned.
* @param string $host The host to validate if is a s3-object-lambda URL.
* @return string returns the signing service name to be used
*/
private function getSigningName($host)
{
if (!$value) {
return;
if (strpos( $host, 's3-object-lambda')) {
return 's3-object-lambda';
}
$decider = RetryMiddleware::createDefaultDecider($value);
$decider = function ($retries, $command, $request, $result, $error) use ($decider, $value) {
$maxRetries = null !== $command['@retries']
? $command['@retries']
: $value;
return $this->getConfig('signing_name');
}
if ($decider($retries, $command, $request, $result, $error)) {
return true;
}
/**
* Modifies API definition to remove `Bucket` from request URIs.
* This is now handled by the endpoint ruleset.
*
* @return void
*
* @internal
*/
private function processEndpointV2Model()
{
$definition = $this->getApi()->getDefinition();
if ($error instanceof AwsException
&& $retries < $maxRetries
) {
if ($error->getResponse()
&& $error->getResponse()->getStatusCode() >= 400
) {
return strpos(
$error->getResponse()->getBody(),
'Your socket connection to the server'
) !== false;
}
if ($error->getPrevious() instanceof RequestException) {
// All commands except CompleteMultipartUpload are
// idempotent and may be retried without worry if a
// networking error has occurred.
return $command->getName() !== 'CompleteMultipartUpload';
foreach($definition['operations'] as &$operation) {
if (isset($operation['http']['requestUri'])) {
$requestUri = $operation['http']['requestUri'];
if ($requestUri === "/{Bucket}") {
$requestUri = str_replace('/{Bucket}', '/', $requestUri);
} else {
$requestUri = str_replace('/{Bucket}', '', $requestUri);
}
$operation['http']['requestUri'] = $requestUri;
}
}
$this->getApi()->setDefinition($definition);
}
/**
* Adds service-specific client built-in values
*
* @return void
*/
private function addBuiltIns($args)
{
if ($args['region'] !== 'us-east-1') {
return false;
};
}
$key = 'AWS::S3::UseGlobalEndpoint';
$result = $args['s3_us_east_1_regional_endpoint'] instanceof \Closure ?
$args['s3_us_east_1_regional_endpoint']()->wait() : $args['s3_us_east_1_regional_endpoint'];
$delay = [RetryMiddleware::class, 'exponentialDelay'];
$list->appendSign(Middleware::retry($decider, $delay), 'retry');
if (is_string($result)) {
if ($result === 'regional') {
$value = false;
} else if ($result === 'legacy') {
$value = true;
} else {
return;
}
} else {
if ($result->isFallback()
|| $result->getEndpointsType() === 'legacy'
) {
$value = true;
} else {
$value = false;
}
}
$this->clientBuiltIns[$key] = $value;
}
/** @internal */
public static function _applyRetryConfig($value, $args, HandlerList $list)
{
if ($value) {
$config = \Aws\Retry\ConfigurationProvider::unwrap($value);
if ($config->getMode() === 'legacy') {
$maxRetries = $config->getMaxAttempts() - 1;
$decider = RetryMiddleware::createDefaultDecider($maxRetries);
$decider = function ($retries, $command, $request, $result, $error) use ($decider, $maxRetries) {
$maxRetries = null !== $command['@retries']
? $command['@retries']
: $maxRetries;
if ($decider($retries, $command, $request, $result, $error)) {
return true;
}
if ($error instanceof AwsException
&& $retries < $maxRetries
) {
if ($error->getResponse()
&& $error->getResponse()->getStatusCode() >= 400
) {
return strpos(
$error->getResponse()->getBody(),
'Your socket connection to the server'
) !== false;
}
if ($error->getPrevious() instanceof RequestException) {
// All commands except CompleteMultipartUpload are
// idempotent and may be retried without worry if a
// networking error has occurred.
return $command->getName() !== 'CompleteMultipartUpload';
}
}
return false;
};
$delay = [RetryMiddleware::class, 'exponentialDelay'];
$list->appendSign(Middleware::retry($decider, $delay), 'retry');
} else {
$defaultDecider = RetryMiddlewareV2::createDefaultDecider(
new QuotaManager(),
$config->getMaxAttempts()
);
$list->appendSign(
RetryMiddlewareV2::wrap(
$config,
[
'collect_stats' => $args['stats']['retries'],
'decider' => function(
$attempts,
CommandInterface $cmd,
$result
) use ($defaultDecider, $config) {
$isRetryable = $defaultDecider($attempts, $cmd, $result);
if (!$isRetryable
&& $result instanceof AwsException
&& $attempts < $config->getMaxAttempts()
) {
if (!empty($result->getResponse())
&& strpos(
$result->getResponse()->getBody(),
'Your socket connection to the server'
) !== false
) {
$isRetryable = false;
}
if ($result->getPrevious() instanceof RequestException
&& $cmd->getName() !== 'CompleteMultipartUpload'
) {
$isRetryable = true;
}
}
return $isRetryable;
}
]
),
'retry'
);
}
}
}
/** @internal */
public static function _applyApiProvider($value, array &$args, HandlerList $list)
{
ClientResolver::_apply_api_provider($value, $args, $list);
ClientResolver::_apply_api_provider($value, $args);
$args['parser'] = new GetBucketLocationParser(
new AmbiguousSuccessParser(
new RetryableMalformedResponseParser(
$args['parser'],
new ValidateResponseChecksumParser(
new AmbiguousSuccessParser(
new RetryableMalformedResponseParser(
$args['parser'],
$args['exception_class']
),
$args['error_parser'],
$args['exception_class']
),
$args['error_parser'],
$args['exception_class']
$args['api']
)
);
}
@@ -574,6 +839,28 @@ class S3Client extends AwsClient implements S3ClientInterface
$b64 = '<div class="alert alert-info">This value will be base64 encoded on your behalf.</div>';
$opt = '<div class="alert alert-info">This value will be computed for you it is not supplied.</div>';
// Add a note on the CopyObject docs
$s3ExceptionRetryMessage = "<p>Additional info on response behavior: if there is"
. " an internal error in S3 after the request was successfully recieved,"
. " a 200 response will be returned with an <code>S3Exception</code> embedded"
. " in it; this will still be caught and retried by"
. " <code>RetryMiddleware.</code></p>";
$docs['operations']['CopyObject'] .= $s3ExceptionRetryMessage;
$docs['operations']['CompleteMultipartUpload'] .= $s3ExceptionRetryMessage;
$docs['operations']['UploadPartCopy'] .= $s3ExceptionRetryMessage;
$docs['operations']['UploadPart'] .= $s3ExceptionRetryMessage;
// Add note about stream ownership in the putObject call
$guzzleStreamMessage = "<p>Additional info on behavior of the stream"
. " parameters: Psr7 takes ownership of streams and will automatically close"
. " streams when this method is called with a stream as the <code>Body</code>"
. " parameter. To prevent this, set the <code>Body</code> using"
. " <code>GuzzleHttp\Psr7\stream_for</code> method with a is an instance of"
. " <code>Psr\Http\Message\StreamInterface</code>, and it will be returned"
. " unmodified. This will allow you to keep the stream in scope. </p>";
$docs['operations']['PutObject'] .= $guzzleStreamMessage;
// Add the SourceFile parameter.
$docs['shapes']['SourceFile']['base'] = 'The path to a file on disk to use instead of the Body parameter.';
$api['shapes']['SourceFile'] = ['type' => 'string'];
@@ -630,4 +917,60 @@ class S3Client extends AwsClient implements S3ClientInterface
new DocModel($docs)
];
}
/**
* @internal
* @codeCoverageIgnore
*/
public static function addDocExamples($examples)
{
$getObjectExample = [
'input' => [
'Bucket' => 'arn:aws:s3:us-east-1:123456789012:accesspoint:myaccesspoint',
'Key' => 'my-key'
],
'output' => [
'Body' => 'class GuzzleHttp\Psr7\Stream#208 (7) {...}',
'ContentLength' => '11',
'ContentType' => 'application/octet-stream',
],
'comments' => [
'input' => '',
'output' => 'Simplified example output'
],
'description' => 'The following example retrieves an object by referencing the bucket via an S3 accesss point ARN. Result output is simplified for the example.',
'id' => '',
'title' => 'To get an object via an S3 access point ARN'
];
if (isset($examples['GetObject'])) {
$examples['GetObject'] []= $getObjectExample;
} else {
$examples['GetObject'] = [$getObjectExample];
}
$putObjectExample = [
'input' => [
'Bucket' => 'arn:aws:s3:us-east-1:123456789012:accesspoint:myaccesspoint',
'Key' => 'my-key',
'Body' => 'my-body',
],
'output' => [
'ObjectURL' => 'https://my-bucket.s3.us-east-1.amazonaws.com/my-key'
],
'comments' => [
'input' => '',
'output' => 'Simplified example output'
],
'description' => 'The following example uploads an object by referencing the bucket via an S3 accesss point ARN. Result output is simplified for the example.',
'id' => '',
'title' => 'To upload an object via an S3 access point ARN'
];
if (isset($examples['PutObject'])) {
$examples['PutObject'] []= $putObjectExample;
} else {
$examples['PutObject'] = [$putObjectExample];
}
return $examples;
}
}

View File

@@ -4,6 +4,7 @@ namespace Aws\S3;
use Aws\AwsClientInterface;
use Aws\CommandInterface;
use Aws\ResultInterface;
use Aws\S3\Exception\S3Exception;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\RequestInterface;
@@ -12,22 +13,22 @@ interface S3ClientInterface extends AwsClientInterface
/**
* Create a pre-signed URL for the given S3 command object.
*
* @param CommandInterface $command Command to create a pre-signed
* URL for.
* @param int|string|\DateTime $expires The time at which the URL should
* expire. This can be a Unix
* timestamp, a PHP DateTime object,
* or a string that can be evaluated
* by strtotime().
* @param CommandInterface $command Command to create a pre-signed
* URL for.
* @param int|string|\DateTimeInterface $expires The time at which the URL should
* expire. This can be a Unix
* timestamp, a PHP DateTime object,
* or a string that can be evaluated
* by strtotime().
*
* @return RequestInterface
*/
public function createPresignedRequest(CommandInterface $command, $expires);
public function createPresignedRequest(CommandInterface $command, $expires, array $options = []);
/**
* Returns the URL to an object identified by its bucket and key.
*
* The URL returned by this method is not signed nor does it ensure the the
* The URL returned by this method is not signed nor does it ensure that the
* bucket and key given to the method exist. If you need a signed URL, then
* use the {@see \Aws\S3\S3Client::createPresignedRequest} method and get
* the URI of the signed request.
@@ -40,6 +41,8 @@ interface S3ClientInterface extends AwsClientInterface
public function getObjectUrl($bucket, $key);
/**
* @deprecated Use doesBucketExistV2() instead
*
* Determines whether or not a bucket exists by name.
*
* @param string $bucket The name of the bucket
@@ -49,6 +52,24 @@ interface S3ClientInterface extends AwsClientInterface
public function doesBucketExist($bucket);
/**
* Determines whether or not a bucket exists by name. This method uses S3's
* HeadBucket operation and requires the relevant bucket permissions in the
* default case to prevent errors.
*
* @param string $bucket The name of the bucket
* @param bool $accept403 Set to true for this method to return true in the case of
* invalid bucket-level permissions. Credentials MUST be valid
* to avoid inaccuracies. Using the default value of false will
* cause an exception to be thrown instead.
*
* @return bool
* @throws S3Exception|Exception if there is an unhandled exception
*/
public function doesBucketExistV2($bucket, $accept403);
/**
* @deprecated Use doesObjectExistV2() instead
*
* Determines whether or not an object exists by name.
*
* @param string $bucket The name of the bucket
@@ -60,11 +81,37 @@ interface S3ClientInterface extends AwsClientInterface
*/
public function doesObjectExist($bucket, $key, array $options = []);
/**
* Determines whether or not an object exists by name. This method uses S3's HeadObject
* operation and requires the relevant bucket and object permissions to prevent errors.
*
* @param string $bucket The name of the bucket
* @param string $key The key of the object
* @param bool $includeDeleteMarkers Set to true to consider delete markers
* existing objects. Using the default value
* of false will ignore delete markers and
* return false.
* @param array $options Additional options available in the HeadObject
* operation (e.g., VersionId).
*
* @return bool
* @throws S3Exception|Exception if there is an unhandled exception
*/
public function doesObjectExistV2($bucket, $key, $includeDeleteMarkers = false, array $options = []);
/**
* Register the Amazon S3 stream wrapper with this client instance.
*/
public function registerStreamWrapper();
/**
* Registers the Amazon S3 stream wrapper with this client instance.
*
*This version uses doesObjectExistV2 and doesBucketExistV2 to check
* resource existence.
*/
public function registerStreamWrapperV2();
/**
* Deletes objects from Amazon S3 that match the result of a ListObjects
* operation. For example, this allows you to do things like delete all

View File

@@ -6,6 +6,7 @@ use Aws\CommandInterface;
use Aws\Exception\AwsException;
use Aws\HandlerList;
use Aws\ResultInterface;
use Aws\S3\Exception\PermanentRedirectException;
use Aws\S3\Exception\S3Exception;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Promise\RejectedPromise;
@@ -98,6 +99,19 @@ trait S3ClientTrait
StreamWrapper::register($this);
}
/**
* @see S3ClientInterface::registerStreamWrapperV2()
*/
public function registerStreamWrapperV2()
{
StreamWrapper::register(
$this,
's3',
null,
true
);
}
/**
* @see S3ClientInterface::deleteMatchingObjects()
*/
@@ -260,6 +274,30 @@ trait S3ClientTrait
);
}
/**
* @see S3ClientInterface::doesBucketExistV2()
*/
public function doesBucketExistV2($bucket, $accept403 = false)
{
$command = $this->getCommand('HeadBucket', ['Bucket' => $bucket]);
try {
$this->execute($command);
return true;
} catch (S3Exception $e) {
if (
($accept403 && $e->getStatusCode() === 403)
|| $e instanceof PermanentRedirectException
) {
return true;
}
if ($e->getStatusCode() === 404) {
return false;
}
throw $e;
}
}
/**
* @see S3ClientInterface::doesObjectExist()
*/
@@ -273,6 +311,44 @@ trait S3ClientTrait
);
}
/**
* @see S3ClientInterface::doesObjectExistV2()
*/
public function doesObjectExistV2(
$bucket,
$key,
$includeDeleteMarkers = false,
array $options = []
){
$command = $this->getCommand('HeadObject', [
'Bucket' => $bucket,
'Key' => $key
] + $options
);
try {
$this->execute($command);
return true;
} catch (S3Exception $e) {
if ($includeDeleteMarkers
&& $this->useDeleteMarkers($e)
) {
return true;
}
if ($e->getStatusCode() === 404) {
return false;
}
throw $e;
}
}
private function useDeleteMarkers($exception)
{
$response = $exception->getResponse();
return !empty($response)
&& $response->getHeader('x-amz-delete-marker');
}
/**
* Determines whether or not a resource exists using a command
*

View File

@@ -1,7 +1,14 @@
<?php
namespace Aws\S3;
use Aws\Arn\ArnParser;
use Aws\Arn\ObjectLambdaAccessPointArn;
use Aws\ClientResolver;
use Aws\CommandInterface;
use Aws\Endpoint\EndpointProvider;
use Aws\Endpoint\PartitionEndpointProvider;
use GuzzleHttp\Exception\InvalidArgumentException;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\RequestInterface;
/**
@@ -38,27 +45,33 @@ class S3EndpointMiddleware
/** @var string */
private $region;
/** @var callable */
private $endpointProvider;
/** @var callable */
private $nextHandler;
/** @var string */
private $endpoint;
/**
* Create a middleware wrapper function
*
* @param string $region
* @param EndpointProvider $endpointProvider
* @param array $options
*
* @return callable
*/
public static function wrap($region, array $options)
public static function wrap($region, $endpointProvider, array $options)
{
return function (callable $handler) use ($region, $options) {
return new self($handler, $region, $options);
return function (callable $handler) use ($region, $endpointProvider, $options) {
return new self($handler, $region, $options, $endpointProvider);
};
}
public function __construct(
callable $nextHandler,
$region,
array $options
array $options,
$endpointProvider = null
) {
$this->pathStyleByDefault = isset($options['path_style'])
? (bool) $options['path_style'] : false;
@@ -67,37 +80,47 @@ class S3EndpointMiddleware
$this->accelerateByDefault = isset($options['accelerate'])
? (bool) $options['accelerate'] : false;
$this->region = (string) $region;
$this->endpoint = isset($options['endpoint'])
? $options['endpoint'] : "";
$this->endpointProvider = is_null($endpointProvider)
? PartitionEndpointProvider::defaultProvider()
: $endpointProvider;
$this->nextHandler = $nextHandler;
}
public function __invoke(CommandInterface $command, RequestInterface $request)
{
switch ($this->endpointPatternDecider($command, $request)) {
case self::HOST_STYLE:
$request = $this->applyHostStyleEndpoint($command, $request);
break;
case self::NO_PATTERN:
case self::PATH_STYLE:
break;
case self::DUALSTACK:
$request = $this->applyDualStackEndpoint($command, $request);
break;
case self::ACCELERATE:
$request = $this->applyAccelerateEndpoint(
$command,
$request,
's3-accelerate'
);
break;
case self::ACCELERATE_DUALSTACK:
$request = $this->applyAccelerateEndpoint(
$command,
$request,
's3-accelerate.dualstack'
);
break;
if (!empty($this->endpoint)) {
$request = $this->applyEndpoint($command, $request);
} else {
switch ($this->endpointPatternDecider($command, $request)) {
case self::HOST_STYLE:
$request = $this->applyHostStyleEndpoint($command, $request);
break;
case self::NO_PATTERN:
break;
case self::PATH_STYLE:
$request = $this->applyPathStyleEndpointCustomizations($command, $request);
break;
case self::DUALSTACK:
$request = $this->applyDualStackEndpoint($command, $request);
break;
case self::ACCELERATE:
$request = $this->applyAccelerateEndpoint(
$command,
$request,
's3-accelerate'
);
break;
case self::ACCELERATE_DUALSTACK:
$request = $this->applyAccelerateEndpoint(
$command,
$request,
's3-accelerate.dualstack'
);
break;
}
}
$nextHandler = $this->nextHandler;
return $nextHandler($command, $request);
}
@@ -110,7 +133,8 @@ class S3EndpointMiddleware
&& (
$request->getUri()->getScheme() === 'http'
|| strpos($command['Bucket'], '.') === false
);
)
&& filter_var($request->getUri()->getHost(), FILTER_VALIDATE_IP) === false;
}
private function endpointPatternDecider(
@@ -183,14 +207,40 @@ class S3EndpointMiddleware
return $request;
}
private function applyPathStyleEndpointCustomizations(
CommandInterface $command,
RequestInterface $request
) {
if ($command->getName() == 'WriteGetObjectResponse') {
$dnsSuffix = $this->endpointProvider
->getPartition($this->region, 's3')
->getDnsSuffix();
$fips = \Aws\is_fips_pseudo_region($this->region) ? "-fips" : "";
$region = \Aws\strip_fips_pseudo_regions($this->region);
$host =
"{$command['RequestRoute']}.s3-object-lambda{$fips}.{$region}.{$dnsSuffix}";
$uri = $request->getUri();
$request = $request->withUri(
$uri->withHost($host)
->withPath($this->getBucketlessPath(
$uri->getPath(),
$command
))
);
}
return $request;
}
private function applyDualStackEndpoint(
CommandInterface $command,
RequestInterface $request
) {
$request = $request->withUri(
$request->getUri()
->withHost($this->getDualStackHost())
$request->getUri()->withHost($this->getDualStackHost())
);
if (empty($command['@use_path_style_endpoint'])
&& !$this->pathStyleByDefault
&& self::isRequestHostStyleCompatible($command, $request)
@@ -202,7 +252,10 @@ class S3EndpointMiddleware
private function getDualStackHost()
{
return "s3.dualstack.{$this->region}.amazonaws.com";
$dnsSuffix = $this->endpointProvider
->getPartition($this->region, 's3')
->getDnsSuffix();
return "s3.dualstack.{$this->region}.{$dnsSuffix}";
}
private function applyAccelerateEndpoint(
@@ -223,12 +276,68 @@ class S3EndpointMiddleware
private function getAccelerateHost(CommandInterface $command, $pattern)
{
return "{$command['Bucket']}.{$pattern}.amazonaws.com";
$dnsSuffix = $this->endpointProvider
->getPartition($this->region, 's3')
->getDnsSuffix();
return "{$command['Bucket']}.{$pattern}.{$dnsSuffix}";
}
private function getBucketlessPath($path, CommandInterface $command)
{
$pattern = '/^\\/' . preg_quote($command['Bucket'], '/') . '/';
return preg_replace($pattern, '', $path) ?: '/';
$path = preg_replace($pattern, '', $path) ?: '/';
if (substr($path, 0 , 1) !== '/') {
$path = '/' . $path;
}
return $path;
}
private function applyEndpoint(
CommandInterface $command,
RequestInterface $request
) {
$dualStack = isset($command['@use_dual_stack_endpoint'])
? $command['@use_dual_stack_endpoint'] : $this->dualStackByDefault;
if (ArnParser::isArn($command['Bucket'])) {
$arn = ArnParser::parse($command['Bucket']);
$outpost = $arn->getService() == 's3-outposts';
if ($outpost && $dualStack) {
throw new InvalidArgumentException("Outposts + dualstack is not supported");
}
if ($arn instanceof ObjectLambdaAccessPointArn) {
return $request;
}
}
if ($dualStack) {
throw new InvalidArgumentException("Custom Endpoint + Dualstack not supported");
}
if ($command->getName() == 'WriteGetObjectResponse') {
$host = "{$command['RequestRoute']}.{$this->endpoint}";
$uri = $request->getUri();
return $request = $request->withUri(
$uri->withHost($host)
->withPath($this->getBucketlessPath(
$uri->getPath(),
$command
))
);
}
$host = ($this->pathStyleByDefault) ?
$this->endpoint :
$this->getBucketStyleHost(
$command,
$this->endpoint
);
$uri = $request->getUri();
$scheme = $uri->getScheme();
if(empty($scheme)){
$request = $request->withUri(
$uri->withHost($host)
);
} else {
$request = $request->withUri($uri);
}
return $request;
}
}

View File

@@ -30,12 +30,16 @@ use GuzzleHttp\Promise;
* @method \GuzzleHttp\Promise\Promise deleteBucketCorsAsync(array $args = [])
* @method \Aws\Result deleteBucketEncryption(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketEncryptionAsync(array $args = [])
* @method \Aws\Result deleteBucketIntelligentTieringConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketIntelligentTieringConfigurationAsync(array $args = [])
* @method \Aws\Result deleteBucketInventoryConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketInventoryConfigurationAsync(array $args = [])
* @method \Aws\Result deleteBucketLifecycle(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketLifecycleAsync(array $args = [])
* @method \Aws\Result deleteBucketMetricsConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketMetricsConfigurationAsync(array $args = [])
* @method \Aws\Result deleteBucketOwnershipControls(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketOwnershipControlsAsync(array $args = [])
* @method \Aws\Result deleteBucketPolicy(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteBucketPolicyAsync(array $args = [])
* @method \Aws\Result deleteBucketReplication(array $args = [])
@@ -62,6 +66,8 @@ use GuzzleHttp\Promise;
* @method \GuzzleHttp\Promise\Promise getBucketCorsAsync(array $args = [])
* @method \Aws\Result getBucketEncryption(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketEncryptionAsync(array $args = [])
* @method \Aws\Result getBucketIntelligentTieringConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketIntelligentTieringConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketInventoryConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketInventoryConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketLifecycle(array $args = [])
@@ -78,6 +84,8 @@ use GuzzleHttp\Promise;
* @method \GuzzleHttp\Promise\Promise getBucketNotificationAsync(array $args = [])
* @method \Aws\Result getBucketNotificationConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketNotificationConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketOwnershipControls(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketOwnershipControlsAsync(array $args = [])
* @method \Aws\Result getBucketPolicy(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketPolicyAsync(array $args = [])
* @method \Aws\Result getBucketPolicyStatus(array $args = [])
@@ -96,6 +104,8 @@ use GuzzleHttp\Promise;
* @method \GuzzleHttp\Promise\Promise getObjectAsync(array $args = [])
* @method \Aws\Result getObjectAcl(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectAclAsync(array $args = [])
* @method \Aws\Result getObjectAttributes(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectAttributesAsync(array $args = [])
* @method \Aws\Result getObjectLegalHold(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectLegalHoldAsync(array $args = [])
* @method \Aws\Result getObjectLockConfiguration(array $args = [])
@@ -114,6 +124,8 @@ use GuzzleHttp\Promise;
* @method \GuzzleHttp\Promise\Promise headObjectAsync(array $args = [])
* @method \Aws\Result listBucketAnalyticsConfigurations(array $args = [])
* @method \GuzzleHttp\Promise\Promise listBucketAnalyticsConfigurationsAsync(array $args = [])
* @method \Aws\Result listBucketIntelligentTieringConfigurations(array $args = [])
* @method \GuzzleHttp\Promise\Promise listBucketIntelligentTieringConfigurationsAsync(array $args = [])
* @method \Aws\Result listBucketInventoryConfigurations(array $args = [])
* @method \GuzzleHttp\Promise\Promise listBucketInventoryConfigurationsAsync(array $args = [])
* @method \Aws\Result listBucketMetricsConfigurations(array $args = [])
@@ -140,6 +152,8 @@ use GuzzleHttp\Promise;
* @method \GuzzleHttp\Promise\Promise putBucketCorsAsync(array $args = [])
* @method \Aws\Result putBucketEncryption(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketEncryptionAsync(array $args = [])
* @method \Aws\Result putBucketIntelligentTieringConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketIntelligentTieringConfigurationAsync(array $args = [])
* @method \Aws\Result putBucketInventoryConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketInventoryConfigurationAsync(array $args = [])
* @method \Aws\Result putBucketLifecycle(array $args = [])
@@ -154,6 +168,8 @@ use GuzzleHttp\Promise;
* @method \GuzzleHttp\Promise\Promise putBucketNotificationAsync(array $args = [])
* @method \Aws\Result putBucketNotificationConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketNotificationConfigurationAsync(array $args = [])
* @method \Aws\Result putBucketOwnershipControls(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketOwnershipControlsAsync(array $args = [])
* @method \Aws\Result putBucketPolicy(array $args = [])
* @method \GuzzleHttp\Promise\Promise putBucketPolicyAsync(array $args = [])
* @method \Aws\Result putBucketReplication(array $args = [])
@@ -188,6 +204,8 @@ use GuzzleHttp\Promise;
* @method \GuzzleHttp\Promise\Promise uploadPartAsync(array $args = [])
* @method \Aws\Result uploadPartCopy(array $args = [])
* @method \GuzzleHttp\Promise\Promise uploadPartCopyAsync(array $args = [])
* @method \Aws\Result writeGetObjectResponse(array $args = [])
* @method \GuzzleHttp\Promise\Promise writeGetObjectResponseAsync(array $args = [])
*/
class S3MultiRegionClient extends BaseClient implements S3ClientInterface
{
@@ -239,7 +257,7 @@ class S3MultiRegionClient extends BaseClient implements S3ClientInterface
$command['@region'] = $region;
}
return Promise\coroutine(function () use (
return Promise\Coroutine::of(function () use (
$handler,
$command,
$cacheKey
@@ -285,7 +303,7 @@ class S3MultiRegionClient extends BaseClient implements S3ClientInterface
};
}
public function createPresignedRequest(CommandInterface $command, $expires)
public function createPresignedRequest(CommandInterface $command, $expires, array $options = [])
{
if (empty($command['Bucket'])) {
throw new \InvalidArgumentException('The S3\\MultiRegionClient'
@@ -317,7 +335,7 @@ class S3MultiRegionClient extends BaseClient implements S3ClientInterface
{
$cacheKey = $this->getCacheKey($bucketName);
if ($cached = $this->cache->get($cacheKey)) {
return Promise\promise_for($cached);
return Promise\Create::promiseFor($cached);
}
/** @var S3ClientInterface $regionalClient */

View File

@@ -1,6 +1,9 @@
<?php
namespace Aws\S3;
use Aws\Arn\Exception\InvalidArnException;
use Aws\Arn\S3\AccessPointArn;
use Aws\Arn\ArnParser;
use GuzzleHttp\Psr7;
use Psr\Http\Message\UriInterface;
@@ -31,11 +34,25 @@ class S3UriParser
* @param string|UriInterface $uri
*
* @return array
* @throws \InvalidArgumentException
* @throws \InvalidArgumentException|InvalidArnException
*/
public function parse($uri)
{
$url = Psr7\uri_for($uri);
// Attempt to parse host component of uri as an ARN
$components = $this->parseS3UrlComponents($uri);
if (!empty($components)) {
if (ArnParser::isArn($components['host'])) {
$arn = new AccessPointArn($components['host']);
return [
'bucket' => $components['host'],
'key' => $components['path'],
'path_style' => false,
'region' => $arn->getRegion()
];
}
}
$url = Psr7\Utils::uriFor($uri);
if ($url->getScheme() == $this->streamWrapperScheme) {
return $this->parseStreamWrapper($url);
@@ -61,6 +78,19 @@ class S3UriParser
return $result;
}
private function parseS3UrlComponents($uri)
{
preg_match("/^([a-zA-Z0-9]*):\/\/([a-zA-Z0-9:-]*)\/(.*)/", $uri, $components);
if (empty($components)) {
return [];
}
return [
'scheme' => $components[1],
'host' => $components[2],
'path' => $components[3],
];
}
private function parseStreamWrapper(UriInterface $url)
{
$result = self::$defaultResult;

View File

@@ -94,6 +94,12 @@ class StreamWrapper
/** @var string The opened protocol (e.g., "s3") */
private $protocol = 's3';
/** @var bool Keeps track of whether stream has been flushed since opening */
private $isFlushed = false;
/** @var bool Whether or not to use V2 bucket and object existence methods */
private static $useV2Existence = false;
/**
* Register the 's3://' stream wrapper
*
@@ -104,8 +110,10 @@ class StreamWrapper
public static function register(
S3ClientInterface $client,
$protocol = 's3',
CacheInterface $cache = null
CacheInterface $cache = null,
$v2Existence = false
) {
self::$useV2Existence = $v2Existence;
if (in_array($protocol, stream_get_wrappers())) {
stream_wrapper_unregister($protocol);
}
@@ -127,12 +135,19 @@ class StreamWrapper
public function stream_close()
{
if (!$this->isFlushed
&& empty($this->body->getSize())
&& $this->mode !== 'r'
) {
$this->stream_flush();
}
$this->body = $this->cache = null;
}
public function stream_open($path, $mode, $options, &$opened_path)
{
$this->initProtocol($path);
$this->isFlushed = false;
$this->params = $this->getBucketKey($path);
$this->mode = rtrim($mode, 'bt');
@@ -140,11 +155,11 @@ class StreamWrapper
return $this->triggerError($errors);
}
return $this->boolCall(function() use ($path) {
return $this->boolCall(function() {
switch ($this->mode) {
case 'r': return $this->openReadStream($path);
case 'a': return $this->openAppendStream($path);
default: return $this->openWriteStream($path);
case 'r': return $this->openReadStream();
case 'a': return $this->openAppendStream();
default: return $this->openWriteStream();
}
});
}
@@ -156,6 +171,15 @@ class StreamWrapper
public function stream_flush()
{
// Check if stream body size has been
// calculated via a flush or close
if($this->body->getSize() === null && $this->mode !== 'r') {
return $this->triggerError(
"Unable to determine stream size. Did you forget to close or flush the stream?"
);
}
$this->isFlushed = true;
if ($this->mode == 'r') {
return false;
}
@@ -169,12 +193,12 @@ class StreamWrapper
// Attempt to guess the ContentType of the upload based on the
// file extension of the key
if (!isset($params['ContentType']) &&
($type = Psr7\mimetype_from_filename($params['Key']))
($type = Psr7\MimeType::fromFilename($params['Key']))
) {
$params['ContentType'] = $type;
}
$this->clearCacheKey("s3://{$params['Bucket']}/{$params['Key']}");
$this->clearCacheKey("{$this->protocol}://{$params['Bucket']}/{$params['Key']}");
return $this->boolCall(function () use ($params) {
return (bool) $this->getClient()->putObject($params);
});
@@ -304,8 +328,10 @@ class StreamWrapper
private function statDirectory($parts, $path, $flags)
{
// Stat "directories": buckets, or "s3://"
$method = self::$useV2Existence ? 'doesBucketExistV2' : 'doesBucketExist';
if (!$parts['Bucket'] ||
$this->getClient()->doesBucketExist($parts['Bucket'])
$this->getClient()->$method($parts['Bucket'])
) {
return $this->formatUrlStat($path);
}
@@ -446,7 +472,7 @@ class StreamWrapper
*/
public function dir_rewinddir()
{
$this->boolCall(function() {
return $this->boolCall(function() {
$this->objectIterator = null;
$this->dir_opendir($this->openedPath, null);
return true;
@@ -573,16 +599,16 @@ class StreamWrapper
. "Use one 'r', 'w', 'a', or 'x'.";
}
// When using mode "x" validate if the file exists before attempting
// to read
if ($mode == 'x' &&
$this->getClient()->doesObjectExist(
if ($mode === 'x') {
$method = self::$useV2Existence ? 'doesObjectExistV2' : 'doesObjectExist';
if ($this->getClient()->$method(
$this->getOption('Bucket'),
$this->getOption('Key'),
$this->getOptions(true)
)
) {
$errors[] = "{$path} already exists on Amazon S3";
)) {
$errors[] = "{$path} already exists on Amazon S3";
}
}
return $errors;
@@ -785,7 +811,9 @@ class StreamWrapper
*/
private function createBucket($path, array $params)
{
if ($this->getClient()->doesBucketExist($params['Bucket'])) {
$method = self::$useV2Existence ? 'doesBucketExistV2' : 'doesBucketExist';
if ($this->getClient()->$method($params['Bucket'])) {
return $this->triggerError("Bucket already exists: {$path}");
}
@@ -811,10 +839,12 @@ class StreamWrapper
$params['Body'] = '';
// Fail if this pseudo directory key already exists
if ($this->getClient()->doesObjectExist(
$method = self::$useV2Existence ? 'doesObjectExistV2' : 'doesObjectExist';
if ($this->getClient()->$method(
$params['Bucket'],
$params['Key'])
) {
$params['Key']
)) {
return $this->triggerError("Subfolder already exists: {$path}");
}
@@ -945,6 +975,6 @@ class StreamWrapper
{
$size = $this->body->getSize();
return $size !== null ? $size : $this->size;
return !empty($size) ? $size : $this->size;
}
}

View File

@@ -5,6 +5,7 @@ use Aws;
use Aws\CommandInterface;
use Aws\Exception\AwsException;
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Promise\PromisorInterface;
use Iterator;
@@ -32,7 +33,8 @@ class Transfer implements PromisorInterface
* the path to a directory on disk to upload, an s3 scheme URI that contains
* the bucket and key (e.g., "s3://bucket/key"), or an \Iterator object
* that yields strings containing filenames that are the path to a file on
* disk or an s3 scheme URI. The "/key" portion of an s3 URI is optional.
* disk or an s3 scheme URI. The bucket portion of the s3 URI may be an S3
* access point ARN. The "/key" portion of an s3 URI is optional.
*
* When providing an iterator for the $source argument, you must also
* provide a 'base_dir' key value pair in the $options argument.
@@ -128,12 +130,16 @@ class Transfer implements PromisorInterface
if ($options['debug'] === true) {
$options['debug'] = fopen('php://output', 'w');
}
$this->addDebugToBefore($options['debug']);
if (is_resource($options['debug'])) {
$this->addDebugToBefore($options['debug']);
}
}
}
/**
* Transfers the files.
*
* @return PromiseInterface
*/
public function promise()
{
@@ -212,23 +218,28 @@ class Transfer implements PromisorInterface
return rtrim(str_replace('\\', '/', $path), '/');
}
private function resolveUri($uri)
private function resolvesOutsideTargetDirectory($sink, $objectKey)
{
$resolved = [];
$sections = explode('/', $uri);
foreach ($sections as $section) {
$sections = explode('/', $sink);
$targetSectionsLength = count(explode('/', $objectKey));
$targetSections = array_slice($sections, -($targetSectionsLength + 1));
$targetDirectory = $targetSections[0];
foreach ($targetSections as $section) {
if ($section === '.' || $section === '') {
continue;
}
if ($section === '..') {
array_pop($resolved);
if (empty($resolved) || $resolved[0] !== $targetDirectory) {
return true;
}
} else {
$resolved []= $section;
}
}
return ($uri[0] === '/' ? '/' : '')
. implode('/', $resolved);
return false;
}
private function createDownloadPromise()
@@ -237,17 +248,10 @@ class Transfer implements PromisorInterface
$prefix = "s3://{$parts['Bucket']}/"
. (isset($parts['Key']) ? $parts['Key'] . '/' : '');
$commands = [];
foreach ($this->getDownloadsIterator() as $object) {
// Prepare the sink.
$objectKey = preg_replace('/^' . preg_quote($prefix, '/') . '/', '', $object);
$resolveSink = $this->destination['path'] . '/';
if (isset($parts['Key']) && strpos($objectKey, $parts['Key']) !== 0) {
$resolveSink .= $parts['Key'] . '/';
}
$resolveSink .= $objectKey;
$sink = $this->destination['path'] . '/' . $objectKey;
$command = $this->client->getCommand(
@@ -255,11 +259,7 @@ class Transfer implements PromisorInterface
$this->getS3Args($object) + ['@http' => ['sink' => $sink]]
);
if (strpos(
$this->resolveUri($resolveSink),
$this->destination['path']
) !== 0
) {
if ($this->resolvesOutsideTargetDirectory($sink, $objectKey)) {
throw new AwsException(
'Cannot download key ' . $objectKey
. ', its relative path resolves outside the'
@@ -297,7 +297,7 @@ class Transfer implements PromisorInterface
// Create an EachPromise, that will concurrently handle the upload
// operations' yielded promises from the iterator.
return Promise\each_limit_all($files, $this->concurrency);
return Promise\Each::ofLimitAll($files, $this->concurrency);
}
/** @return Iterator */
@@ -352,6 +352,7 @@ class Transfer implements PromisorInterface
{
$args = $this->s3Args;
$args['Key'] = $this->createS3Key($filename);
$filename = $filename instanceof \SplFileInfo ? $filename->getPathname() : $filename;
return (new MultipartUploader($this->client, $filename, [
'bucket' => $args['Bucket'],

View File

@@ -0,0 +1,37 @@
<?php
namespace Aws\S3\UseArnRegion;
use Aws;
use Aws\S3\UseArnRegion\Exception\ConfigurationException;
class Configuration implements ConfigurationInterface
{
private $useArnRegion;
public function __construct($useArnRegion)
{
$this->useArnRegion = Aws\boolean_value($useArnRegion);
if (is_null($this->useArnRegion)) {
throw new ConfigurationException("'use_arn_region' config option"
. " must be a boolean value.");
}
}
/**
* {@inheritdoc}
*/
public function isUseArnRegion()
{
return $this->useArnRegion;
}
/**
* {@inheritdoc}
*/
public function toArray()
{
return [
'use_arn_region' => $this->isUseArnRegion(),
];
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Aws\S3\UseArnRegion;
interface ConfigurationInterface
{
/**
* Returns whether or not to use the ARN region if it differs from client
*
* @return bool
*/
public function isUseArnRegion();
/**
* Returns the configuration as an associative array
*
* @return array
*/
public function toArray();
}

View File

@@ -0,0 +1,175 @@
<?php
namespace Aws\S3\UseArnRegion;
use Aws\AbstractConfigurationProvider;
use Aws\CacheInterface;
use Aws\ConfigurationProviderInterface;
use Aws\S3\UseArnRegion\Exception\ConfigurationException;
use GuzzleHttp\Promise;
/**
* A configuration provider is a function that returns a promise that is
* fulfilled with a {@see \Aws\S3\UseArnRegion\ConfigurationInterface}
* or rejected with an {@see \Aws\S3\UseArnRegion\Exception\ConfigurationException}.
*
* <code>
* use Aws\S3\UseArnRegion\ConfigurationProvider;
* $provider = ConfigurationProvider::defaultProvider();
* // Returns a ConfigurationInterface or throws.
* $config = $provider()->wait();
* </code>
*
* Configuration providers can be composed to create configuration using
* conditional logic that can create different configurations in different
* environments. You can compose multiple providers into a single provider using
* {@see Aws\S3\UseArnRegion\ConfigurationProvider::chain}. This function
* accepts providers as variadic arguments and returns a new function that will
* invoke each provider until a successful configuration is returned.
*
* <code>
* // First try an INI file at this location.
* $a = ConfigurationProvider::ini(null, '/path/to/file.ini');
* // Then try an INI file at this location.
* $b = ConfigurationProvider::ini(null, '/path/to/other-file.ini');
* // Then try loading from environment variables.
* $c = ConfigurationProvider::env();
* // Combine the three providers together.
* $composed = ConfigurationProvider::chain($a, $b, $c);
* // Returns a promise that is fulfilled with a configuration or throws.
* $promise = $composed();
* // Wait on the configuration to resolve.
* $config = $promise->wait();
* </code>
*/
class ConfigurationProvider extends AbstractConfigurationProvider
implements ConfigurationProviderInterface
{
const ENV_USE_ARN_REGION = 'AWS_S3_USE_ARN_REGION';
const INI_USE_ARN_REGION = 's3_use_arn_region';
const DEFAULT_USE_ARN_REGION = true;
public static $cacheKey = 'aws_s3_use_arn_region_config';
protected static $interfaceClass = ConfigurationInterface::class;
protected static $exceptionClass = ConfigurationException::class;
/**
* Create a default config provider that first checks for environment
* variables, then checks for a specified profile in the environment-defined
* config file location (env variable is 'AWS_CONFIG_FILE', file location
* defaults to ~/.aws/config), then checks for the "default" profile in the
* environment-defined config file location, and failing those uses a default
* fallback set of configuration options.
*
* This provider is automatically wrapped in a memoize function that caches
* previously provided config options.
*
* @param array $config
*
* @return callable
*/
public static function defaultProvider(array $config = [])
{
$configProviders = [self::env()];
if (
!isset($config['use_aws_shared_config_files'])
|| $config['use_aws_shared_config_files'] != false
) {
$configProviders[] = self::ini();
}
$configProviders[] = self::fallback();
$memo = self::memoize(
call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders)
);
if (isset($config['use_arn_region'])
&& $config['use_arn_region'] instanceof CacheInterface
) {
return self::cache($memo, $config['use_arn_region'], self::$cacheKey);
}
return $memo;
}
/**
* Provider that creates config from environment variables.
*
* @return callable
*/
public static function env()
{
return function () {
// Use config from environment variables, if available
$useArnRegion = getenv(self::ENV_USE_ARN_REGION);
if (!empty($useArnRegion)) {
return Promise\Create::promiseFor(
new Configuration($useArnRegion)
);
}
return self::reject('Could not find environment variable config'
. ' in ' . self::ENV_USE_ARN_REGION);
};
}
/**
* Config provider that creates config using a config file whose location
* is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to
* ~/.aws/config if not specified
*
* @param string|null $profile Profile to use. If not specified will use
* the "default" profile.
* @param string|null $filename If provided, uses a custom filename rather
* than looking in the default directory.
*
* @return callable
*/
public static function ini($profile = null, $filename = null)
{
$filename = $filename ?: (self::getDefaultConfigFilename());
$profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default');
return function () use ($profile, $filename) {
if (!@is_readable($filename)) {
return self::reject("Cannot read configuration from $filename");
}
// Use INI_SCANNER_NORMAL instead of INI_SCANNER_TYPED for PHP 5.5 compatibility
$data = \Aws\parse_ini_file($filename, true, INI_SCANNER_NORMAL);
if ($data === false) {
return self::reject("Invalid config file: $filename");
}
if (!isset($data[$profile])) {
return self::reject("'$profile' not found in config file");
}
if (!isset($data[$profile][self::INI_USE_ARN_REGION])) {
return self::reject("Required S3 Use Arn Region config values
not present in INI profile '{$profile}' ({$filename})");
}
// INI_SCANNER_NORMAL parses false-y values as an empty string
if ($data[$profile][self::INI_USE_ARN_REGION] === "") {
$data[$profile][self::INI_USE_ARN_REGION] = false;
}
return Promise\Create::promiseFor(
new Configuration($data[$profile][self::INI_USE_ARN_REGION])
);
};
}
/**
* Fallback config options when other sources are not set.
*
* @return callable
*/
public static function fallback()
{
return function () {
return Promise\Create::promiseFor(
new Configuration(self::DEFAULT_USE_ARN_REGION)
);
};
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Aws\S3\UseArnRegion\Exception;
use Aws\HasMonitoringEventsTrait;
use Aws\MonitoringEventsInterface;
/**
* Represents an error interacting with configuration for S3's UseArnRegion
*/
class ConfigurationException extends \RuntimeException implements
MonitoringEventsInterface
{
use HasMonitoringEventsTrait;
}

View File

@@ -0,0 +1,149 @@
<?php
namespace Aws\S3;
use Aws\Api\Parser\AbstractParser;
use Aws\Api\Service;
use Aws\Api\StructureShape;
use Aws\CommandInterface;
use Aws\S3\Exception\S3Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
/**
* @internal Decorates a parser for the S3 service to validate the response checksum.
*/
class ValidateResponseChecksumParser extends AbstractParser
{
use CalculatesChecksumTrait;
/**
* @param callable $parser Parser to wrap.
*/
public function __construct(callable $parser, Service $api)
{
$this->api = $api;
$this->parser = $parser;
}
public function __invoke(
CommandInterface $command,
ResponseInterface $response
) {
$fn = $this->parser;
$result = $fn($command, $response);
//Skip this middleware if the operation doesn't have an httpChecksum
$op = $this->api->getOperation($command->getName());
$checksumInfo = isset($op['httpChecksum'])
? $op['httpChecksum']
: [];
if (empty($checksumInfo)) {
return $result;
}
//Skip this middleware if the operation doesn't send back a checksum, or the user doesn't opt in
$checksumModeEnabledMember = isset($checksumInfo['requestValidationModeMember'])
? $checksumInfo['requestValidationModeMember']
: "";
$checksumModeEnabled = isset($command[$checksumModeEnabledMember])
? $command[$checksumModeEnabledMember]
: "";
$responseAlgorithms = isset($checksumInfo['responseAlgorithms'])
? $checksumInfo['responseAlgorithms']
: [];
if (empty($responseAlgorithms)
|| strtolower($checksumModeEnabled) !== "enabled"
) {
return $result;
}
if (extension_loaded('awscrt')) {
$checksumPriority = ['CRC32C', 'CRC32', 'SHA1', 'SHA256'];
} else {
$checksumPriority = ['CRC32', 'SHA1', 'SHA256'];
}
$checksumsToCheck = array_intersect($responseAlgorithms, $checksumPriority);
$checksumValidationInfo = $this->validateChecksum($checksumsToCheck, $response);
if ($checksumValidationInfo['status'] == "SUCCEEDED") {
$result['ChecksumValidated'] = $checksumValidationInfo['checksum'];
} else if ($checksumValidationInfo['status'] == "FAILED"){
//Ignore failed validations on GetObject if it's a multipart get which returned a full multipart object
if ($command->getName() == "GetObject"
&& !empty($checksumValidationInfo['checksumHeaderValue'])
) {
$headerValue = $checksumValidationInfo['checksumHeaderValue'];
$lastDashPos = strrpos($headerValue, '-');
$endOfChecksum = substr($headerValue, $lastDashPos + 1);
if (is_numeric($endOfChecksum)
&& intval($endOfChecksum) > 1
&& intval($endOfChecksum) < 10000) {
return $result;
}
}
throw new S3Exception(
"Calculated response checksum did not match the expected value",
$command
);
}
return $result;
}
public function parseMemberFromStream(
StreamInterface $stream,
StructureShape $member,
$response
) {
return $this->parser->parseMemberFromStream($stream, $member, $response);
}
/**
* @param $checksumPriority
* @param ResponseInterface $response
*/
public function validateChecksum($checksumPriority, ResponseInterface $response)
{
$checksumToValidate = $this->chooseChecksumHeaderToValidate(
$checksumPriority,
$response
);
$validationStatus = "SKIPPED";
$checksumHeaderValue = null;
if (!empty($checksumToValidate)) {
$checksumHeaderValue = $response->getHeader(
'x-amz-checksum-' . $checksumToValidate
);
if (isset($checksumHeaderValue)) {
$checksumHeaderValue = $checksumHeaderValue[0];
$calculatedChecksumValue = $this->getEncodedValue(
$checksumToValidate,
$response->getBody()
);
$validationStatus = $checksumHeaderValue == $calculatedChecksumValue
? "SUCCEEDED"
: "FAILED";
}
}
return [
"status" => $validationStatus,
"checksum" => $checksumToValidate,
"checksumHeaderValue" => $checksumHeaderValue,
];
}
/**
* @param $checksumPriority
* @param ResponseInterface $response
*/
public function chooseChecksumHeaderToValidate(
$checksumPriority,
ResponseInterface $response
) {
foreach ($checksumPriority as $checksum) {
$checksumHeader = 'x-amz-checksum-' . $checksum;
if ($response->hasHeader($checksumHeader)) {
return $checksum;
}
}
return null;
}
}