//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');
}
如何让php取出一个url,以便将ex:http://www.amazonaws/bucket/image.jpg存储到我的sql数据库列照片中?
现在我的网络应用程序给了我这个错误: 不能在第127行使用Aws \ S3 \ Iterator \ ListObjectsIterator类型的对象作为数组
答案 0 :(得分:2)
//retrieve image url
$objects = $s3->getIterator('ListObjects', [
'Bucket' => $config['s3']['bucket']
]);
这不是你认为它正在做的事情。正如方法名称所示,这将使您获得迭代器,而不是内存数组。这个迭代器将覆盖存储桶中的每个项目,而不仅仅是您上传的文件。由于它是一个迭代器,当你尝试使用数组访问方法($photoLink = $s3->getObjectUrl($config['s3']['bucket'], $objects['Key']);
)时,它会爆炸。
你可能想要的是来自putObject()的响应,你当前没有存储它。类似的东西:
try {
$result = $s3->putObject([
<snip>
之后,您可以使用$result['ObjectURL']
访问该网址。从putObject() is on Amazon's site返回的完整文档。