2015-05-06 64 views
1
//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数据库列照片中?从亚马逊s3桶对象获取图像url

现在我的web应用程序,让我这个错误: 不能使用类型为AWS \ S3 \ Iterator的对象\ ListObjectsIterator如阵列上线127

回答

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> 

之后,您可以访问URL为$result['ObjectURL']。从putObject() is on Amazon's site返回的完整文档。

+0

非常感谢。我得到了它的工作。如果任何人遇到同样的问题,请按照上述步骤从您的存储桶中的图像中拉出一个url。 – Gianni