du512926 2015-01-17 16:22
浏览 197

使用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

  • 写回答

1条回答 默认 最新

  • duansha7453 2015-01-17 17:51
    关注

    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() . "
    ";
    }
    

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?