1瘤旨、下載aws-sdk-php-laravel
git clone https://github.com/aws/aws-sdk-php-laravel.git
2、安裝aws-sdk-php
cd aws-sdk-php-laravel/
curl -sS https://getcomposer.org/installer | php #安裝 composer.phar
vim composer.json
修改如下內(nèi)容
image.png
php composer.phar update
php composer.phar require aws/aws-sdk-php #安裝aws-sdk-php
3因宇、在AWS上創(chuàng)建一個(gè)存儲(chǔ)桶
[root@hostname-172-31-9-249 /data/wwwroot/aws-sdk-php-laravel]# vim create-bucket.php
<?
require 'vendor/autoload.php';
use Aws\S3\S3Client;
$bucketName = 'maiyuan2'; #存儲(chǔ)桶的名字
$client = new S3Client([
'version' => 'latest',
'region' => 'us-west-1', #要改為美國(guó)西部察滑,不然會(huì)報(bào)錯(cuò)
'credentials' => [
'key' => '', #訪問秘鑰
'secret' => '' #私有訪問秘鑰
]
]);
try {
$result = $client->createBucket([
'Bucket' => $bucketName, // REQUIRED
'ACL' => 'public-read',
]);
} catch (Aws\S3\Exception\S3Exception $e) {
// output error message if fails
echo $e->getMessage();
}
?>
chmod +x create-bucket.php
cd vendor/
chmod +x autoload.php
php create-bucket.php #執(zhí)行發(fā)現(xiàn)在aws上創(chuàng)建存儲(chǔ)桶成功
4、上傳文件到桶
[root@hostname-172-31-9-249 /data/wwwroot/aws-sdk-php-laravel]# vim upload-to-aws.php
<?
require 'vendor/autoload.php';
use Aws\S3\S3Client;
// Instantiate an Amazon S3 client.
$s3 = new S3Client([
'version' => 'latest',
'region' => 'us-west-1', #改為美國(guó)西部
'credentials' => [
'key' => '', #訪問秘鑰
'secret' => '' #私有訪問秘鑰
]
]);
$bucketName = 'maiyuan2'; #存儲(chǔ)桶的名字
$file_Path = '/data/wwwroot/aws-sdk-php-laravel/QQ圖片20180223091800.png'; #要上傳的文件的路徑
$key = basename($file_Path);
// Upload a publicly accessible file. The file size and type are determined by the SDK.
try {
$result = $s3->putObject([
'Bucket' => $bucketName,
'Key' => $key,
'Body' => fopen($file_Path, 'r'),
'ACL' => 'public-read',
]);
echo $result->get('ObjectURL');
} catch (Aws\S3\Exception\S3Exception $e) {
echo "There was an error uploading the file.\n";
echo $e->getMessage();
}
?>
chmod +x upload-to-aws.php
php upload-to-aws.php #在S3上發(fā)現(xiàn)上傳圖片成功
本文參考:https://github.com/aws/aws-sdk-php-laravel
https://artisansweb.net/upload-files-amazon-s3-using-aws-php-sdk/
https://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/installation.html