3

I'm using the Amazon AWS SDK for PHP (namely, version 2.7.16) to upload files to an S3 bucket. How can I set a timeout for http/tcp operations (connection, upload, etc.)? Although I've googled a lot I wasn't able to find out how.

Sample code I'm using:

$awsS3Client = Aws\S3\S3Client::factory(array(
        'key' => '...',
        'secret' => '...'
    ));

$awsS3Client->putObject(array(
            'Bucket' => '...',
            'Key'    => 'destin/ation.file',
            'ACL'    => 'private',
            'Body'   => 'content'
        ));

so I'd like to set a timeout on the putObject() call.

Thanks!

mmanzato
  • 213
  • 3
  • 8

2 Answers2

7

Eventually I helped myself:

$awsS3Client = Aws\S3\S3Client::factory(array(
        'key' => '...',
        'secret' => '...'
        'curl.options' => array(
            CURLOPT_CONNECTTIMEOUT => 5,
            CURLOPT_TIMEOUT => 10,
        )
    ));

Looks like AWS PHP uses curl internally, so network related options are set this way.

mmanzato
  • 213
  • 3
  • 8
  • Aws\AwsClient::factory is now deprecated : https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.AwsClient.html#_factory Using the constructor directly seems to do the same job. – gileri Aug 06 '21 at 14:13
2

With SDK version 3 this can be configured using the http configuration key.

$awsS3Client = Aws\S3\S3Client([
        'key' => '...',
        'secret' => '...',
        'http' => [
            'connect_timeout' => 5,
            'timeout' => 10,
        ]
    ]);
kkochanski
  • 2,178
  • 23
  • 28
gileri
  • 662
  • 8
  • 16