简体   繁体   中英

Errors using PHP SDK for Amazon S3

EDIT : Problem solved (thanks to this post ) ! I just needed to install curl :

sudo apt-get install php5-curl

I am working on a tool to upload images in a AWS S3 bucket from a browser. I am using the PHP SDK provided by Amazon. I tried the code provided in the documentation , but it does not work with me:

use Aws\S3\S3Client;

$bucket = '*** Your Bucket Name ***';
$keyname = '*** Your Object Key ***';
// $filepath should be absolute path to a file on disk                      
$filepath = '*** Your File Path ***';

// Instantiate the client.
$s3 = S3Client::factory();

// Upload a file.
$result = $s3->putObject(array(
    'Bucket'       => $bucket,
    'Key'          => $keyname,
    'SourceFile'   => $filepath,
    'ContentType'  => 'text/plain',
    'ACL'          => 'public-read',
    'StorageClass' => 'REDUCED_REDUNDANCY',
    'Metadata'     => array(    
        'param1' => 'value 1',
        'param2' => 'value 2'
    )
));

echo $result['ObjectURL'];

Even if I keep only these 2 lines:

use Aws\S3\S3Client;
$s3 = S3Client::factory();

...I get these errors:

Notice: Use of undefined constant CURLE_COULDNT_RESOLVE_HOST - assumed 'CURLE_COULDNT_RESOLVE_HOST' in phar:///var/www/aws.phar/Aws/S3/S3Client.php on line 244

Notice: Use of undefined constant CURLE_COULDNT_CONNECT - assumed 'CURLE_COULDNT_CONNECT' in phar:///var/www/aws.phar/Aws/S3/S3Client.php on line 244

Notice: Use of undefined constant CURLE_PARTIAL_FILE - assumed 'CURLE_PARTIAL_FILE' in phar:///var/www/aws.phar/Aws/S3/S3Client.php on line 244

Notice: Use of undefined constant CURLE_WRITE_ERROR - assumed 'CURLE_WRITE_ERROR' in phar:///var/www/aws.phar/Aws/S3/S3Client.php on line 244

Notice: Use of undefined constant CURLE_READ_ERROR - assumed 'CURLE_READ_ERROR' in phar:///var/www/aws.phar/Aws/S3/S3Client.php on line 244

Notice: Use of undefined constant CURLE_OPERATION_TIMEOUTED - assumed 'CURLE_OPERATION_TIMEOUTED' in phar:///var/www/aws.phar/Aws/S3/S3Client.php on line 244

Notice: Use of undefined constant CURLE_SSL_CONNECT_ERROR - assumed 'CURLE_SSL_CONNECT_ERROR' in phar:///var/www/aws.phar/Aws/S3/S3Client.php on line 244

Notice: Use of undefined constant CURLE_HTTP_PORT_FAILED - assumed 'CURLE_HTTP_PORT_FAILED' in phar:///var/www/aws.phar/Aws/S3/S3Client.php on line 244

Notice: Use of undefined constant CURLE_GOT_NOTHING - assumed 'CURLE_GOT_NOTHING' in phar:///var/www/aws.phar/Aws/S3/S3Client.php on line 244

Notice: Use of undefined constant CURLE_SEND_ERROR - assumed 'CURLE_SEND_ERROR' in phar:///var/www/aws.phar/Aws/S3/S3Client.php on line 244

Notice: Use of undefined constant CURLE_RECV_ERROR - assumed 'CURLE_RECV_ERROR' in phar:///var/www/aws.phar/Aws/S3/S3Client.php on line 244

Fatal error: Uncaught exception 'Guzzle\\Common\\Exception\\RuntimeException' with message 'The PHP cURL extension must be installed to use Guzzle.' in phar:///var/www/aws.phar/Guzzle/Http/Client.php:72 Stack trace: #0 phar:///var/www/aws.phar/Aws/Common/Client/AbstractClient.php(79): Guzzle\\Http\\Client->__construct(' https://s3.amaz ...', Object(Guzzle\\Common\\Collection)) #1 phar:///var/www/aws.phar/Aws/Common/Client/ClientBuilder.php(249): Aws\\Common\\Client\\AbstractClient->__construct(Object(Aws\\Common\\Credentials\\RefreshableInstanceProfileCredentials), Object(Aws\\S3\\S3Signature), Object(Guzzle\\Common\\Collection)) #2 phar:///var/www/aws.phar/Aws/S3/S3Client.php(207): Aws\\Common\\Client\\ClientBuilder->build() #3 /var/www/response.php(30): Aws\\S3\\S3Client::factory() #4 {main} thrown in phar:///var/www/aws.phar/Guzzle/Http/Client.php on line 72

Just installed aws php sdk on my local machine, without any aws registration, after several trys got this working code:

use Aws\S3\S3Client;

$bucket = '*** Your Bucket Name ***';
$keyname = '*** Your Object Key ***';
// $filepath should be absolute path to a file on disk                      
$filepath = '*** Your File Path ***';

// Instantiate the client.
$s3 = S3Client::factory();
try{

// Upload a file.
$result = $s3->putObject(array(
    'Bucket'       => $bucket,
    'Key'          => $keyname,
    'SourceFile'   => $filepath,
    'ContentType'  => 'text/plain',
    'ACL'          => 'public-read',
    'StorageClass' => 'REDUCED_REDUNDANCY',
    'Metadata'     => array(    
        'param1' => 'value 1',
        'param2' => 'value 2'
    )
));
echo $result['ObjectURL'];

} catch (Exception $e) {
    echo $e->getMessage() . "\n";
}

this code return aws Exception message:

You must specify a non-null value for the Body or SourceFile parameters.

and if I change $filepath to the path to any real file, this code outputs:

Error retrieving credentials from the instance profile metadata server. When you are not running inside of Amazon EC2, you must provide your AWS access key ID and secret access key in the "key" and "secret" options when creating a client or provide an instantiated Aws\\Common\\Credentials\\CredentialsInterface object. ([curl] 28: Connection timed out after 5000 milliseconds [url] http://XXX.xxx.xxx.xxx/latest/meta-data/iam/security-credentials/ )

so if any question you are very welcome, but it must work.

by the way if I remove catch section from the code I get same error messages from php on my broken page.

Hope this could help you in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM