aws s3 php,如何通过PHP访问AWS S3?

tldr;您有各种AWS SDK版本

>根据您在消息中提供的链接(link),您已经安装了php sdk v3

>根据您的示例,您使用PHP sdk v2

>创建一个客户端 – 简单的方法

// Include the SDK using the Composer autoloader

require 'vendor/autoload.php';

$s3 = new Aws\S3\S3Client([

'version' => 'latest',

'region' => 'us-east-1'

]);

>创建一个客户端 – 使用sdk类

// Use the us-west-2 region and latest version of each client.

$sharedConfig = [

'region' => 'us-west-2',

'version' => 'latest'

];

// Create an SDK class used to share configuration across clients.

$sdk = new Aws\Sdk($sharedConfig);

// Create an Amazon S3 client using the shared configuration data.

$s3 = $sdk->createS3();

一旦你有你的客户端,你可以使用你现有的代码(是的,这个是v3)在s3上放置一个新对象,这样你就会得到像

// Include the AWS SDK using the Composer autoloader.

require '../vendor/autoload.php';

use Aws\S3\S3Client;

use Aws\S3\Exception\S3Exception;

$bucket = 'testbucket';

$keyname = 'test.txt';

// Instantiate the client.

-- select method 1 or 2 --

try {

// Upload data.

$result = $s3->putObject(array(

'Bucket' => $bucket,

'Key' => $keyname,

'Body' => 'Hello, world!',

'ACL' => 'public-read'

));

// Print the URL to the object.

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

} catch (S3Exception $e) {

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值