一、引入composer
"aws/aws-sdk-php": "^3.137", "league/flysystem-aws-s3-v3": "^1.0"
二、添加配置
's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), #key 'secret' => env('AWS_SECRET_ACCESS_KEY'), #秘钥 'region' => env('AWS_DEFAULT_REGION'), #区域 'bucket' => env('AWS_BUCKET'), #桶 'root' => '/images', # 存储桶桶路径 'visibility' => 'public', # 是否公开 'endpoint' => env('AWS_ENDPOINT'), #源站 ],三、配置说明
AWS_BUCKET=test AWS_ENDPOINT=http://s3.amazonaws.com/ 注意:这里的endpoint是不需要桶名字的,最终源站就是test.s3.amazonaws.com四、使用案例
$bucketName = 'test'; $client = S3Client::factory(array( "key" => "blog.phpfs.com", "secret" => "blog.phpfs.com", "region" => 'eu-central-1', "scheme" => "http", 'version' => 'latest', 'endpoint' => 'http://s3.amazonaws.com/', )); $file_Path = public_path("blog.phpfs.com/test.png"); try { $Key = "images/img/test.png"; $op = [ 'Bucket' => '', 'Key' => $Key, 'Body' => file_get_contents($file_Path), 'ACL' => 'public-read', ]; $result = $client->putObject($op); // $result = $client->upload($bucketName, $Key,file_get_contents($file_Path), 'public-read'); print_r($result); } catch (Aws\S3\Exception\S3Exception $e) { echo "There was an error uploading the file.\n"; echo $e->getMessage(); }