[英]Getting image url from amazon s3 bucket object
//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数据库列照片中?
现在,我的Web应用程序给了我这个错误:无法在第127行中将类型为Aws \\ S3 \\ Iterator \\ ListObjectsIterator的对象用作数组
//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']
访问URL。 从putObject()返回的完整文档在Amazon网站上 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.