簡體   English   中英

從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數據庫列照片中?

現在,我的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM