从 amazon s3 存储桶对象获取图像 url
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 数据库列照片中?
现在我的网络应用程序给我这个错误:
不能在第 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() is on Amazon's site.
的 return 的完整文档
//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 的对象用作数组//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() is on Amazon's site.