aws s3 php 上传文件,使用PHP SDK在amazon S3上上传文件

我正在尝试通过他们的

PHP SDK上传我的亚马逊S3上的图片.所以我做了一个小脚本来做到这一点.但是,我的脚本不起作用,我的异常不会向我发回任何错误消息.

我是AWS新手,谢谢你的帮助.

这是代码:

config.php文件

return array(

'includes' => array('_aws'),

'services' => array(

'default_settings' => array(

'params' => array(

'key' => 'PUBLICKEY',

'secret' => 'PRIVATEKEY',

'region' => 'eu-west-1'

)

)

)

);

?>

的index.php

//Installing AWS SDK via phar

require 'aws.phar';

use Aws\S3\S3Client;

use Aws\S3\Exception\S3Exception;

$bucket = 'infact';

$keyname = 'myImage';

// $filepath should be absolute path to a file on disk

$filepath = 'image.jpg';

// Instantiate the client.

$s3 = S3Client::factory('config.php');

// Upload a file.

try {

$result = $s3->putObject(array(

'Bucket' => $bucket,

'Key' => $keyname,

'SourceFile' => $filePath,

'ContentType' => 'text/plain',

'ACL' => 'public-read',

'StorageClass' => 'REDUCED_REDUNDANCY'

));

// Print the URL to the object.

echo $result['ObjectURL'] . "\n";

} catch (S3Exception $e) {

echo $e->getMessage() . "\n";

}

?>

编辑:我现在使用此代码,但它仍然无法正常工作.我甚至没有错误或异常消息.

require 'aws.phar';

use Aws\S3\S3Client;

use Aws\S3\Exception\S3Exception;

$bucket = 'infactr';

$keyname = 'sample';

// $filepath should be absolute path to a file on disk

$filepath = 'image.jpg';

// Instantiate the client.

$s3 = S3Client::factory(array(

'key' => 'key',

'secret' => 'privatekey',

'region' => 'eu-west-1'

));

try {

// Upload data.

$result = $s3->putObject(array(

'Bucket' => $bucket,

'Key' => $keyname,

'SourceFile' => $filePath,

'ACL' => 'public-read',

'ContentType' => 'image/jpeg'

));

// Print the URL to the object.

echo $result['ObjectURL'] . "\n";

} catch (S3Exception $e) {

echo $e->getMessage() . "\n";

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值