Php 从AmazonS3 bucket对象获取图像url
如何让php提取url,以便将ex:存储到我的sql数据库专栏照片中 现在,我的web应用程序显示以下错误: 无法将Aws\S3\Iterator\ListObjectsIterator类型的对象用作第127行中的数组Php 从AmazonS3 bucket对象获取图像url,php,amazon-web-services,amazon-s3,Php,Amazon Web Services,Amazon S3,如何让php提取url,以便将ex:存储到我的sql数据库专栏照片中 现在,我的web应用程序显示以下错误: 无法将Aws\S3\Iterator\ListObjectsIterator类型的对象用作第127行中的数组 //story image to s3 bucket try { $s3->putObject([ 'Bucket' => $config['s3']['bucket'], 'Ke
//story image to s3 bucket
try {
$s3->putObject([
'Bucket' => $config['s3']['bucket'],
'Key' => "uploads/{$name_of_uploaded_file}",
'Body' => fopen($path_of_uploaded_file, 'rb'),
'ACL' => 'public-read'
]);
//remove the file
unlink($path_of_uploaded_file);
} catch(S3Exception $e){
die("there was an error");
}
//retrieve image url
$objects = $s3->getIterator('ListObjects', [
'Bucket' => $config['s3']['bucket']
]);
//put img url into variable so that it stores it into sql table
$photoLink = $s3->getObjectUrl($config['s3']['bucket'], $objects['Key']);
if (is_uploaded_file($_FILES['uploaded_file']['size'])){
//send items to pending database
//note already connected to db
//inserts in pending db
$sql = "INSERT INTO pending (id,photo,title,description,name) VALUES ('', :photo, :title, :description, :name)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':photo', $photoLink);
$stmt->bindParam(':description', $story);
$stmt->bindParam(':name', $first_name);
$stmt->execute();
}else {
header('Location:index.php');
}
这不是你想象的那样。正如方法名称所示,这是一个迭代器,而不是内存中的数组。这个迭代器将覆盖bucket中的每一项,而不仅仅是您上传的文件。由于它是一个迭代器,当您尝试使用数组访问方法($photoLink=$s3->getObjectUrl($config['s3']['bucket'],$objects['Key']);
)时,它会崩溃
您可能想要的是来自putObject()的响应,您当前没有存储该响应。比如:
//retrieve image url
$objects = $s3->getIterator('ListObjects', [
'Bucket' => $config['s3']['bucket']
]);
试试看{
$result=$s3->putObject([
之后,您可以使用
$result['ObjectURL']
访问URL。返回源的完整文档。非常感谢。我已经开始工作了。如果有人遇到同样的问题,请按照上面的步骤从您的存储桶中的图像中提取URL。
try {
$result = $s3->putObject([
<snip>