v2: Updates

* Simplifies & beautifies everything
* Introduces a new Class system.
* Errors are defaulted to AWS's handler.
* New function names & more efficient handling.
* Should fix a majority of the errors.

Please read the README for more!
This commit is contained in:
Devang Srivastava 2020-09-28 15:32:51 +05:30
commit e6d7753dc8
1095 changed files with 45088 additions and 2911 deletions

View file

@ -2,9 +2,12 @@
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;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
/**
* Converts errors returned with a status code of 200 to a retryable error type.
@ -14,13 +17,12 @@ use Psr\Http\Message\ResponseInterface;
class AmbiguousSuccessParser extends AbstractParser
{
private static $ambiguousSuccesses = [
'UploadPart' => true,
'UploadPartCopy' => true,
'CopyObject' => true,
'CompleteMultipartUpload' => true,
];
/** @var callable */
private $parser;
/** @var callable */
private $errorParser;
/** @var string */
@ -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'],
@ -57,4 +68,12 @@ class AmbiguousSuccessParser extends AbstractParser
$fn = $this->parser;
return $fn($command, $response);
}
public function parseMemberFromStream(
StreamInterface $stream,
StructureShape $member,
$response
) {
return $this->parser->parseMemberFromStream($stream, $member, $response);
}
}

View file

@ -1,6 +1,8 @@
<?php
namespace Aws\S3;
use Aws\Api\ApiProvider;
use Aws\Api\Service;
use Aws\CommandInterface;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;
@ -14,37 +16,32 @@ use Psr\Http\Message\RequestInterface;
*/
class ApplyChecksumMiddleware
{
private static $md5 = [
'DeleteObjects',
'PutBucketCors',
'PutBucketLifecycle',
'PutBucketLifecycleConfiguration',
'PutBucketPolicy',
'PutBucketTagging',
'PutBucketReplication',
];
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;
}
@ -56,7 +53,9 @@ class ApplyChecksumMiddleware
$name = $command->getName();
$body = $request->getBody();
if (in_array($name, self::$md5) && !$request->hasHeader('Content-MD5')) {
$op = $this->api->getOperation($command->getName());
if (!empty($op['httpChecksumRequired']) && !$request->hasHeader('Content-MD5')) {
// Set the content MD5 header for operations that require it.
$request = $request->withHeader(
'Content-MD5',

View file

@ -100,7 +100,7 @@ class BatchDelete implements PromisorInterface
array $options = []
) {
$fn = function (BatchDelete $that) use ($iter) {
return \GuzzleHttp\Promise\coroutine(function () use ($that, $iter) {
return Promise\coroutine(function () use ($that, $iter) {
foreach ($iter as $obj) {
if ($promise = $that->enqueue($obj)) {
yield $promise;

View file

@ -0,0 +1,289 @@
<?php
namespace Aws\S3;
use Aws\Api\Service;
use Aws\Arn\ArnInterface;
use Aws\Arn\ArnParser;
use Aws\Arn\Exception\InvalidArnException;
use Aws\Arn\S3\AccessPointArn;
use Aws\CommandInterface;
use Aws\Endpoint\PartitionEndpointProvider;
use Aws\Exception\InvalidRegionException;
use Aws\Exception\UnresolvedEndpointException;
use Aws\S3\Exception\S3Exception;
use Psr\Http\Message\RequestInterface;
/**
* Checks for access point ARN in members targeting BucketName, modifying
* endpoint as appropriate
*
* @internal
*/
class BucketEndpointArnMiddleware
{
/** @var Service */
private $service;
/** @var callable */
private $nextHandler;
/** @var string */
private $region;
/** @var $config */
private $config;
/** @var PartitionEndpointProvider */
private $partitionProvider;
/** @var array */
private $nonArnableCommands = ['CreateBucket'];
/**
* Create a middleware wrapper function.
*
* @param Service $service
* @param $region
* @param array $config
* @return callable
*/
public static function wrap(
Service $service,
$region,
array $config
) {
return function (callable $handler) use ($service, $region, $config) {
return new self($handler, $service, $region, $config);
};
}
public function __construct(
callable $nextHandler,
Service $service,
$region,
array $config = []
) {
$this->partitionProvider = PartitionEndpointProvider::defaultProvider();
$this->region = $region;
$this->service = $service;
$this->config = $config;
$this->nextHandler = $nextHandler;
}
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
);
}
$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 (substr($path, 0, $len) === "/{$encoded}") {
$path = substr($path, $len);
}
if (empty($path)) {
$path = '';
}
// Set modified request
$req = $req->withUri(
$req->getUri()->withHost($host)->withPath($path)
);
// Update signing region based on ARN data if configured to do so
if ($this->config['use_arn_region']->isUseArnRegion()) {
$region = $arn->getRegion();
} else {
$region = $this->region;
}
$endpointData = $partition([
'region' => $region,
'service' => $arn->getService()
]);
$cmd['@context']['signing_region'] = $endpointData['signingRegion'];
} catch (InvalidArnException $e) {
// Add context to ARN exception
throw new S3Exception(
'Bucket parameter parsed as ARN and failed with: '
. $e->getMessage(),
$cmd,
[],
$e
);
}
}
}
}
return $nextHandler($cmd, $req);
}
private function generateAccessPointHost(
AccessPointArn $arn,
RequestInterface $req
) {
$host = $arn->getResourceId() . '-' . $arn->getAccountId()
. '.s3-accesspoint';
if (!empty($this->config['dual_stack'])) {
$host .= '.dualstack';
}
if (!empty($this->config['use_arn_region']->isUseArnRegion())) {
$region = $arn->getRegion();
} else {
$region = $this->region;
}
$host .= '.' . $region . '.' . $this->getPartitionSuffix($arn);
return $host;
}
private function getPartitionSuffix(ArnInterface $arn)
{
$partition = $this->partitionProvider->getPartition(
$arn->getRegion(),
$arn->getService()
);
return $partition->getDnsSuffix();
}
private function getSigningRegion($region)
{
$partition = PartitionEndpointProvider::defaultProvider()->getPartition($region, 's3');
$data = $partition->toArray();
if (isset($data['services']['s3']['endpoints'][$region]['credentialScope']['region'])) {
return $data['services']['s3']['endpoints'][$region]['credentialScope']['region'];
}
return $region;
}
private function isMatchingSigningRegion($arnRegion, $clientRegion)
{
$arnRegion = strtolower($arnRegion);
$clientRegion = $this->stripPseudoRegions(strtolower($clientRegion));
if ($arnRegion === $clientRegion) {
return true;
}
if ($this->getSigningRegion($clientRegion) === $arnRegion) {
return true;
}
return false;
}
private function stripPseudoRegions($region)
{
return str_replace(['fips-', '-fips'], ['', ''], $region);
}
/**
* 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 AccessPointArn) {
// 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'])) {
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.');
}
// 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(
$this->stripPseudoRegions($this->region),
's3'
);
}
// 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
if (!($this->isMatchingSigningRegion($arn->getRegion(), $this->region))) {
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}).");
}
}
return $arnPart;
}
throw new InvalidArnException('Provided ARN was not'
. ' a valid S3 access point ARN');
}
}

View file

@ -0,0 +1,75 @@
<?php
namespace Aws\S3\Crypto;
use Aws\Crypto\MaterialsProvider;
use Aws\Crypto\MetadataEnvelope;
use Aws\Crypto\MetadataStrategyInterface;
trait CryptoParamsTrait
{
protected function getMaterialsProvider(array $args)
{
if ($args['@MaterialsProvider'] instanceof MaterialsProvider) {
return $args['@MaterialsProvider'];
}
throw new \InvalidArgumentException('An instance of MaterialsProvider'
. ' must be passed in the "MaterialsProvider" field.');
}
protected function getInstructionFileSuffix(array $args)
{
return !empty($args['@InstructionFileSuffix'])
? $args['@InstructionFileSuffix']
: $this->instructionFileSuffix;
}
protected function determineGetObjectStrategy(
$result,
$instructionFileSuffix
) {
if (isset($result['Metadata'][MetadataEnvelope::CONTENT_KEY_V2_HEADER])) {
return new HeadersMetadataStrategy();
}
return new InstructionFileMetadataStrategy(
$this->client,
$instructionFileSuffix
);
}
protected function getMetadataStrategy(array $args, $instructionFileSuffix)
{
if (!empty($args['@MetadataStrategy'])) {
if ($args['@MetadataStrategy'] instanceof MetadataStrategyInterface) {
return $args['@MetadataStrategy'];
}
if (is_string($args['@MetadataStrategy'])) {
switch ($args['@MetadataStrategy']) {
case HeadersMetadataStrategy::class:
return new HeadersMetadataStrategy();
case InstructionFileMetadataStrategy::class:
return new InstructionFileMetadataStrategy(
$this->client,
$instructionFileSuffix
);
default:
throw new \InvalidArgumentException('Could not match the'
. ' specified string in "MetadataStrategy" to a'
. ' predefined strategy.');
}
} else {
throw new \InvalidArgumentException('The metadata strategy that'
. ' was passed to "MetadataStrategy" was unrecognized.');
}
} elseif ($instructionFileSuffix) {
return new InstructionFileMetadataStrategy(
$this->client,
$instructionFileSuffix
);
}
return null;
}
}

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,14 +1,14 @@
<?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\MetadataStrategyInterface;
use Aws\Crypto\Cipher\CipherBuilderTrait;
use Aws\S3\S3Client;
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\PromiseInterface;
@ -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;
use CipherBuilderTrait;
use CryptoParamsTrait;
use DecryptionTrait;
use EncryptionTrait;
use UserAgentTrait;
const CRYPTO_VERSION = '1n';
private $client;
private $instructionFileSuffix;
@ -37,79 +53,16 @@ class S3EncryptionClient extends AbstractCryptoClient
S3Client $client,
$instructionFileSuffix = null
) {
$this->appendUserAgent($client, 'S3CryptoV' . self::CRYPTO_VERSION);
$this->client = $client;
$this->instructionFileSuffix = $instructionFileSuffix;
}
private function getMaterialsProvider(array $args)
{
if ($args['@MaterialsProvider'] instanceof MaterialsProvider) {
return $args['@MaterialsProvider'];
} else {
throw new \InvalidArgumentException('An instance of MaterialsProvider'
. ' must be passed in the "MaterialsProvider" field.');
}
}
private function getInstructionFileSuffix(array $args)
{
return !empty($args['@InstructionFileSuffix'])
? $args['@InstructionFileSuffix']
: $this->instructionFileSuffix;
}
private static function getDefaultStrategy()
{
return new HeadersMetadataStrategy();
}
private function determineGetObjectStrategy(
$result,
$instructionFileSuffix
) {
if (isset($result['Metadata'][MetadataEnvelope::CONTENT_KEY_V2_HEADER])) {
return new HeadersMetadataStrategy();
} else {
return new InstructionFileMetadataStrategy(
$this->client,
$instructionFileSuffix
);
}
}
private function getMetadataStrategy(array $args, $instructionFileSuffix)
{
if (!empty($args['@MetadataStrategy'])) {
if ($args['@MetadataStrategy'] instanceof MetadataStrategyInterface) {
return $args['@MetadataStrategy'];
} elseif (is_string($args['@MetadataStrategy'])) {
switch ($args['@MetadataStrategy']) {
case HeadersMetadataStrategy::class:
return new HeadersMetadataStrategy();
case InstructionFileMetadataStrategy::class:
return new InstructionFileMetadataStrategy(
$this->client,
$instructionFileSuffix
);
default:
throw new \InvalidArgumentException('Could not match the'
. ' specified string in "MetadataStrategy" to a'
. ' predefined strategy.');
}
} else {
throw new \InvalidArgumentException('The metadata strategy that'
. ' was passed to "MetadataStrategy" was unrecognized.');
}
} elseif ($instructionFileSuffix) {
return new InstructionFileMetadataStrategy(
$this->client,
$instructionFileSuffix
);
}
return null;
}
/**
* Encrypts the data in the 'Body' field of $args and promises to upload it
* to the specified location on S3.
@ -124,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:
*
@ -214,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, 'S3CryptoV' . 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\promise_for($this->encrypt(
Psr7\stream_for($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

@ -0,0 +1,169 @@
<?php
namespace Aws\S3\Crypto;
use Aws\Crypto\AbstractCryptoClient;
use Aws\Crypto\EncryptionTrait;
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.
*
* Legacy implementation using older encryption workflow. Use
* S3EncryptionMultipartUploaderV2 if possible.
*
* @deprecated
*/
class S3EncryptionMultipartUploader extends MultipartUploader
{
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.
*
* @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, AbstractCryptoClient::$supportedCiphers);
}
private $provider;
private $instructionFileSuffix;
private $strategy;
/**
* Creates a multipart upload for an S3 object after encrypting it.
*
* The required configuration options 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) cbc|gcm
* 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.
* - 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, 'S3CryptoV' . 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\promise_for($this->encrypt(
$this->source,
$this->config['@cipheroptions'] ?: [],
$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,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, 'S3CryptoV' . 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\promise_for($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

@ -1,12 +1,18 @@
<?php
namespace Aws\S3\Exception;
use Aws\HasMonitoringEventsTrait;
use Aws\MonitoringEventsInterface;
/**
* Exception thrown when errors occur while deleting objects using a
* {@see S3\BatchDelete} object.
*/
class DeleteMultipleObjectsException extends \Exception
class DeleteMultipleObjectsException extends \Exception implements
MonitoringEventsInterface
{
use HasMonitoringEventsTrait;
private $deleted = [];
private $errors = [];

View file

@ -2,8 +2,10 @@
namespace Aws\S3;
use Aws\Api\Parser\AbstractParser;
use Aws\Api\StructureShape;
use Aws\CommandInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
/**
* @internal Decorates a parser for the S3 service to correctly handle the
@ -11,9 +13,6 @@ use Psr\Http\Message\ResponseInterface;
*/
class GetBucketLocationParser extends AbstractParser
{
/** @var callable */
private $parser;
/**
* @param callable $parser Parser to wrap.
*/
@ -39,4 +38,12 @@ class GetBucketLocationParser extends AbstractParser
return $result;
}
public function parseMemberFromStream(
StreamInterface $stream,
StructureShape $member,
$response
) {
return $this->parser->parseMemberFromStream($stream, $member, $response);
}
}

View file

@ -1,6 +1,8 @@
<?php
namespace Aws\S3;
use Aws\Arn\ArnParser;
use Aws\Multipart\AbstractUploadManager;
use Aws\ResultInterface;
use GuzzleHttp\Psr7;
@ -50,19 +52,25 @@ 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
* @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 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 (ArnParser::isArn($source)) {
$this->source = '';
} else {
$this->source = "/";
}
$this->source .= ltrim($source, '/');
parent::__construct(
$client,
array_change_key_case($config) + ['source_metadata' => null]
);
}
/**
@ -80,12 +88,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 +109,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;
@ -121,7 +128,13 @@ class MultipartCopy extends AbstractUploadManager
$data[$k] = $v;
}
$data['CopySource'] = $this->source;
list($bucket, $key) = explode('/', ltrim($this->source, '/'), 2);
$data['CopySource'] = '/' . $bucket . '/' . rawurlencode(
implode(
'/',
array_map('urlencode', explode('/', $key))
)
);
$data['PartNumber'] = $partNumber;
$defaultPartSize = $this->determinePartSize();

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.
@ -48,6 +48,9 @@ class MultipartUploader extends AbstractUploader
* 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.
* - prepare_data_source: (callable) Callback to invoke before starting the
* multipart upload workflow. The callback should have a function
* signature like `function () {...}`.
* - 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`

View file

@ -1,6 +1,8 @@
<?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;
@ -139,8 +141,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

@ -30,7 +30,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
@ -70,19 +71,19 @@ class ObjectUploader implements PromisorInterface
'key' => $this->key,
'acl' => $this->acl
] + $this->options))->promise();
} else {
// Perform a regular PutObject operation.
$command = $this->client->getCommand('PutObject', [
'Bucket' => $this->bucket,
'Key' => $this->key,
'Body' => $this->body,
'ACL' => $this->acl,
] + $this->options['params']);
if (is_callable($this->options['before_upload'])) {
$this->options['before_upload']($command);
}
return $this->client->executeAsync($command);
}
// Perform a regular PutObject operation.
$command = $this->client->getCommand('PutObject', [
'Bucket' => $this->bucket,
'Key' => $this->key,
'Body' => $this->body,
'ACL' => $this->acl,
] + $this->options['params']);
if (is_callable($this->options['before_upload'])) {
$this->options['before_upload']($command);
}
return $this->client->executeAsync($command);
}
public function upload()
@ -123,7 +124,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,35 @@
<?php
namespace Aws\S3\RegionalEndpoint;
class Configuration implements ConfigurationInterface
{
private $endpointsType;
public function __construct($endpointsType)
{
$this->endpointsType = strtolower($endpointsType);
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()
];
}
}

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('self::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\promise_for(
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\promise_for(
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\promise_for(
new Configuration(self::DEFAULT_ENDPOINTS_TYPE)
);
};
}
/**
* 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

@ -2,10 +2,12 @@
namespace Aws\S3;
use Aws\Api\Parser\AbstractParser;
use Aws\Api\StructureShape;
use Aws\Api\Parser\Exception\ParserException;
use Aws\CommandInterface;
use Aws\Exception\AwsException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
/**
* Converts malformed responses to a retryable error type.
@ -14,8 +16,6 @@ use Psr\Http\Message\ResponseInterface;
*/
class RetryableMalformedResponseParser extends AbstractParser
{
/** @var callable */
private $parser;
/** @var string */
private $exceptionClass;
@ -45,4 +45,12 @@ class RetryableMalformedResponseParser extends AbstractParser
);
}
}
public function parseMemberFromStream(
StreamInterface $stream,
StructureShape $member,
$response
) {
return $this->parser->parseMemberFromStream($stream, $member, $response);
}
}

View file

@ -5,17 +5,24 @@ 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\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;
use GuzzleHttp\Psr7;
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\RequestInterface;
/**
@ -59,6 +66,8 @@ use Psr\Http\Message\RequestInterface;
* @method \GuzzleHttp\Promise\Promise deleteObjectTaggingAsync(array $args = [])
* @method \Aws\Result deleteObjects(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteObjectsAsync(array $args = [])
* @method \Aws\Result deletePublicAccessBlock(array $args = [])
* @method \GuzzleHttp\Promise\Promise deletePublicAccessBlockAsync(array $args = [])
* @method \Aws\Result getBucketAccelerateConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketAccelerateConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketAcl(array $args = [])
@ -87,6 +96,8 @@ use Psr\Http\Message\RequestInterface;
* @method \GuzzleHttp\Promise\Promise getBucketNotificationConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketPolicy(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketPolicyAsync(array $args = [])
* @method \Aws\Result getBucketPolicyStatus(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketPolicyStatusAsync(array $args = [])
* @method \Aws\Result getBucketReplication(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketReplicationAsync(array $args = [])
* @method \Aws\Result getBucketRequestPayment(array $args = [])
@ -101,10 +112,18 @@ 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 getObjectLegalHold(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectLegalHoldAsync(array $args = [])
* @method \Aws\Result getObjectLockConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectLockConfigurationAsync(array $args = [])
* @method \Aws\Result getObjectRetention(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectRetentionAsync(array $args = [])
* @method \Aws\Result getObjectTagging(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectTaggingAsync(array $args = [])
* @method \Aws\Result getObjectTorrent(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectTorrentAsync(array $args = [])
* @method \Aws\Result getPublicAccessBlock(array $args = [])
* @method \GuzzleHttp\Promise\Promise getPublicAccessBlockAsync(array $args = [])
* @method \Aws\Result headBucket(array $args = [])
* @method \GuzzleHttp\Promise\Promise headBucketAsync(array $args = [])
* @method \Aws\Result headObject(array $args = [])
@ -167,10 +186,20 @@ use Psr\Http\Message\RequestInterface;
* @method \GuzzleHttp\Promise\Promise putObjectAsync(array $args = [])
* @method \Aws\Result putObjectAcl(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectAclAsync(array $args = [])
* @method \Aws\Result putObjectLegalHold(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectLegalHoldAsync(array $args = [])
* @method \Aws\Result putObjectLockConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectLockConfigurationAsync(array $args = [])
* @method \Aws\Result putObjectRetention(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectRetentionAsync(array $args = [])
* @method \Aws\Result putObjectTagging(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectTaggingAsync(array $args = [])
* @method \Aws\Result putPublicAccessBlock(array $args = [])
* @method \GuzzleHttp\Promise\Promise putPublicAccessBlockAsync(array $args = [])
* @method \Aws\Result restoreObject(array $args = [])
* @method \GuzzleHttp\Promise\Promise restoreObjectAsync(array $args = [])
* @method \Aws\Result selectObjectContent(array $args = [])
* @method \GuzzleHttp\Promise\Promise selectObjectContentAsync(array $args = [])
* @method \Aws\Result uploadPart(array $args = [])
* @method \GuzzleHttp\Promise\Promise uploadPartAsync(array $args = [])
* @method \Aws\Result uploadPartCopy(array $args = [])
@ -195,6 +224,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'],
@ -240,11 +282,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
@ -260,16 +319,21 @@ class S3Client extends AwsClient implements S3ClientInterface
*/
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);
}
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');
@ -277,6 +341,7 @@ class S3Client extends AwsClient implements S3ClientInterface
$stack->appendBuild(
S3EndpointMiddleware::wrap(
$this->getRegion(),
$this->getConfig('endpoint_provider'),
[
'dual_stack' => $this->getConfig('use_dual_stack_endpoint'),
'accelerate' => $this->getConfig('use_accelerate_endpoint'),
@ -287,6 +352,22 @@ class S3Client extends AwsClient implements S3ClientInterface
);
}
$stack->appendBuild(
BucketEndpointArnMiddleware::wrap(
$this->getApi(),
$this->getRegion(),
[
'use_arn_region' => $this->getConfig('use_arn_region'),
'dual_stack' => $this->getConfig('use_dual_stack_endpoint'),
'accelerate' => $this->getConfig('use_accelerate_endpoint'),
'path_style' => $this->getConfig('use_path_style_endpoint'),
'endpoint' => isset($args['endpoint'])
? $args['endpoint']
: null
]
),
's3.bucket_endpoint_arn'
);
$stack->appendSign(PutObjectUrlMiddleware::wrap(), 's3.put_object_url');
$stack->appendSign(PermanentRedirectMiddleware::wrap(), 's3.permanent_redirect');
$stack->appendInit(Middleware::sourceFile($this->getApi()), 's3.source_file');
@ -317,7 +398,26 @@ 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');
@ -333,10 +433,24 @@ class S3Client extends AwsClient implements S3ClientInterface
return $signer->presign(
\Aws\serialize($command),
$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', [
@ -486,49 +600,97 @@ class S3Client extends AwsClient implements S3ClientInterface
}
/** @internal */
public static function _applyRetryConfig($value, $_, HandlerList $list)
public static function _applyRetryConfig($value, $args, HandlerList $list)
{
if (!$value) {
return;
}
if ($value) {
$config = \Aws\Retry\ConfigurationProvider::unwrap($value);
$decider = RetryMiddleware::createDefaultDecider($value);
$decider = function ($retries, $command, $request, $result, $error) use ($decider, $value) {
$maxRetries = null !== $command['@retries']
? $command['@retries']
: $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;
} elseif ($error instanceof AwsException
&& $retries < $maxRetries
) {
if (
$error->getResponse()
&& $error->getResponse()->getStatusCode() >= 400
) {
return strpos(
$error->getResponse()->getBody(),
'Your socket connection to the server'
) !== false;
} elseif ($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';
}
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'
);
}
return false;
};
$delay = [RetryMiddleware::class, 'exponentialDelay'];
$list->appendSign(Middleware::retry($decider, $delay), '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(
@ -550,6 +712,18 @@ 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 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'];
@ -606,4 +780,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

@ -22,12 +22,12 @@ interface S3ClientInterface extends AwsClientInterface
*
* @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.

View file

@ -9,6 +9,7 @@ use Aws\ResultInterface;
use Aws\S3\Exception\S3Exception;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Promise\RejectedPromise;
use Psr\Http\Message\ResponseInterface;
/**
* A trait providing S3-specific functionality. This is meant to be used in
@ -224,7 +225,7 @@ trait S3ClientTrait
if ($e->getAwsErrorCode() === 'AuthorizationHeaderMalformed') {
$region = $this->determineBucketRegionFromExceptionBody(
$response->getBody()
$response
);
if (!empty($region)) {
return $region;
@ -236,10 +237,10 @@ trait S3ClientTrait
});
}
private function determineBucketRegionFromExceptionBody($responseBody)
private function determineBucketRegionFromExceptionBody(ResponseInterface $response)
{
try {
$element = $this->parseXml($responseBody);
$element = $this->parseXml($response->getBody(), $response);
if (!empty($element->Region)) {
return (string)$element->Region;
}

View file

@ -2,7 +2,8 @@
namespace Aws\S3;
use Aws\CommandInterface;
use Aws\S3\Exception\S3Exception;
use Aws\Endpoint\EndpointProvider;
use Aws\Endpoint\PartitionEndpointProvider;
use Psr\Http\Message\RequestInterface;
/**
@ -39,27 +40,31 @@ class S3EndpointMiddleware
/** @var string */
private $region;
/** @var callable */
private $endpointProvider;
/** @var callable */
private $nextHandler;
/**
* 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;
@ -68,6 +73,9 @@ class S3EndpointMiddleware
$this->accelerateByDefault = isset($options['accelerate'])
? (bool) $options['accelerate'] : false;
$this->region = (string) $region;
$this->endpointProvider = is_null($endpointProvider)
? PartitionEndpointProvider::defaultProvider()
: $endpointProvider;
$this->nextHandler = $nextHandler;
}
@ -111,7 +119,8 @@ class S3EndpointMiddleware
&& (
$request->getUri()->getScheme() === 'http'
|| strpos($command['Bucket'], '.') === false
);
)
&& filter_var($request->getUri()->getHost(), FILTER_VALIDATE_IP) === false;
}
private function endpointPatternDecider(
@ -131,17 +140,23 @@ class S3EndpointMiddleware
return $this->canAccelerate($command)
? self::ACCELERATE_DUALSTACK
: self::DUALSTACK;
} elseif ($accelerate && $this->canAccelerate($command)) {
}
if ($accelerate && $this->canAccelerate($command)) {
return self::ACCELERATE;
} elseif ($dualStack) {
}
if ($dualStack) {
return self::DUALSTACK;
} elseif (!$pathStyle
}
if (!$pathStyle
&& self::isRequestHostStyleCompatible($command, $request)
) {
return self::HOST_STYLE;
} else {
return self::PATH_STYLE;
}
return self::PATH_STYLE;
}
private function canAccelerate(CommandInterface $command)
@ -183,9 +198,9 @@ class S3EndpointMiddleware
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)
@ -197,7 +212,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(
@ -218,7 +236,10 @@ 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)

View file

@ -1,7 +1,6 @@
<?php
namespace Aws\S3;
use Aws\Api\Parser\PayloadParserTrait;
use Aws\CacheInterface;
use Aws\CommandInterface;
use Aws\LruArrayCache;
@ -51,6 +50,8 @@ use GuzzleHttp\Promise;
* @method \GuzzleHttp\Promise\Promise deleteObjectTaggingAsync(array $args = [])
* @method \Aws\Result deleteObjects(array $args = [])
* @method \GuzzleHttp\Promise\Promise deleteObjectsAsync(array $args = [])
* @method \Aws\Result deletePublicAccessBlock(array $args = [])
* @method \GuzzleHttp\Promise\Promise deletePublicAccessBlockAsync(array $args = [])
* @method \Aws\Result getBucketAccelerateConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketAccelerateConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketAcl(array $args = [])
@ -79,6 +80,8 @@ use GuzzleHttp\Promise;
* @method \GuzzleHttp\Promise\Promise getBucketNotificationConfigurationAsync(array $args = [])
* @method \Aws\Result getBucketPolicy(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketPolicyAsync(array $args = [])
* @method \Aws\Result getBucketPolicyStatus(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketPolicyStatusAsync(array $args = [])
* @method \Aws\Result getBucketReplication(array $args = [])
* @method \GuzzleHttp\Promise\Promise getBucketReplicationAsync(array $args = [])
* @method \Aws\Result getBucketRequestPayment(array $args = [])
@ -93,10 +96,18 @@ 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 getObjectLegalHold(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectLegalHoldAsync(array $args = [])
* @method \Aws\Result getObjectLockConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectLockConfigurationAsync(array $args = [])
* @method \Aws\Result getObjectRetention(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectRetentionAsync(array $args = [])
* @method \Aws\Result getObjectTagging(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectTaggingAsync(array $args = [])
* @method \Aws\Result getObjectTorrent(array $args = [])
* @method \GuzzleHttp\Promise\Promise getObjectTorrentAsync(array $args = [])
* @method \Aws\Result getPublicAccessBlock(array $args = [])
* @method \GuzzleHttp\Promise\Promise getPublicAccessBlockAsync(array $args = [])
* @method \Aws\Result headBucket(array $args = [])
* @method \GuzzleHttp\Promise\Promise headBucketAsync(array $args = [])
* @method \Aws\Result headObject(array $args = [])
@ -159,10 +170,20 @@ use GuzzleHttp\Promise;
* @method \GuzzleHttp\Promise\Promise putObjectAsync(array $args = [])
* @method \Aws\Result putObjectAcl(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectAclAsync(array $args = [])
* @method \Aws\Result putObjectLegalHold(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectLegalHoldAsync(array $args = [])
* @method \Aws\Result putObjectLockConfiguration(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectLockConfigurationAsync(array $args = [])
* @method \Aws\Result putObjectRetention(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectRetentionAsync(array $args = [])
* @method \Aws\Result putObjectTagging(array $args = [])
* @method \GuzzleHttp\Promise\Promise putObjectTaggingAsync(array $args = [])
* @method \Aws\Result putPublicAccessBlock(array $args = [])
* @method \GuzzleHttp\Promise\Promise putPublicAccessBlockAsync(array $args = [])
* @method \Aws\Result restoreObject(array $args = [])
* @method \GuzzleHttp\Promise\Promise restoreObjectAsync(array $args = [])
* @method \Aws\Result selectObjectContent(array $args = [])
* @method \GuzzleHttp\Promise\Promise selectObjectContentAsync(array $args = [])
* @method \Aws\Result uploadPart(array $args = [])
* @method \GuzzleHttp\Promise\Promise uploadPartAsync(array $args = [])
* @method \Aws\Result uploadPartCopy(array $args = [])
@ -245,7 +266,7 @@ class S3MultiRegionClient extends BaseClient implements S3ClientInterface
} catch (AwsException $e) {
if ($e->getAwsErrorCode() === 'AuthorizationHeaderMalformed') {
$region = $this->determineBucketRegionFromExceptionBody(
$e->getResponse()->getBody()
$e->getResponse()
);
if (!empty($region)) {
$this->cache->set($cacheKey, $region);
@ -264,7 +285,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'

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,10 +34,24 @@ class S3UriParser
* @param string|UriInterface $uri
*
* @return array
* @throws \InvalidArgumentException
* @throws \InvalidArgumentException|InvalidArnException
*/
public function parse($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\uri_for($uri);
if ($url->getScheme() == $this->streamWrapperScheme) {
@ -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,9 @@ 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;
/**
* Register the 's3://' stream wrapper
*
@ -127,12 +130,16 @@ class StreamWrapper
public function stream_close()
{
if ($this->body->getSize() === 0 && !($this->isFlushed)) {
$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 +147,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 +163,7 @@ class StreamWrapper
public function stream_flush()
{
$this->isFlushed = true;
if ($this->mode == 'r') {
return false;
}
@ -174,7 +182,7 @@ class StreamWrapper
$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);
});
@ -281,10 +289,10 @@ class StreamWrapper
// Return as if it is a bucket to account for console
// bucket objects (e.g., zero-byte object "foo/")
return $this->formatUrlStat($path);
} else {
// Attempt to stat and cache regular object
return $this->formatUrlStat($result->toArray());
}
// Attempt to stat and cache regular object
return $this->formatUrlStat($result->toArray());
} catch (S3Exception $e) {
// Maybe this isn't an actual key, but a prefix. Do a prefix
// listing of objects to determine.
@ -446,7 +454,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;
@ -945,6 +953,6 @@ class StreamWrapper
{
$size = $this->body->getSize();
return $size !== null ? $size : $this->size;
return !empty($size) ? $size : $this->size;
}
}

View file

@ -5,7 +5,6 @@ use Aws;
use Aws\CommandInterface;
use Aws\Exception\AwsException;
use GuzzleHttp\Promise;
use GuzzleHttp\Psr7;
use GuzzleHttp\Promise\PromisorInterface;
use Iterator;
@ -33,7 +32,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.
@ -47,10 +47,9 @@ class Transfer implements PromisorInterface
* iterator. If the $source option is not an array, then this option is
* ignored.
* - before: (callable) A callback to invoke before each transfer. The
* callback accepts the following positional arguments: string $source,
* string $dest, Aws\CommandInterface $command. The provided command will
* be either a GetObject, PutObject, InitiateMultipartUpload, or
* UploadPart command.
* callback accepts a single argument: Aws\CommandInterface $command.
* The provided command will be either a GetObject, PutObject,
* InitiateMultipartUpload, or UploadPart command.
* - mup_threshold: (int) Size in bytes in which a multipart upload should
* be used instead of PutObject. Defaults to 20971520 (20 MB).
* - concurrency: (int, default=5) Number of files to upload concurrently.
@ -130,7 +129,9 @@ 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']);
}
}
}
@ -354,7 +355,8 @@ 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'],
'key' => $args['Key'],

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 = false;
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('self::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\promise_for(
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\promise_for(
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\promise_for(
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;
}