【问题标题】:How to download an Object from AWS S3 Bucket with PHP?如何使用 PHP 从 AWS S3 存储桶下载对象?
【发布时间】:2015-09-12 15:38:00
【问题描述】:

我知道如何像这样将对象上传到 Aws S3 Bucket:

try {
    $oClientAws->putObject(array(
        'Bucket' => 'bucket_test',
        'Key'    => 'fileName.jpg',
        'Body'   => fopen('path/to/file/fileName.jpg', 'r'),
        'ACL'    => 'public-read',
    ));            
} 
catch (Aws\Exception\S3Exception $e) {}

但我不知道如何下载可以使用 $oClientAws->getObject(parms...) 的对象并更改标头的内容类型,但这只是在浏览器上显示我的文件,但不要下载文件。

tks!

【问题讨论】:

    标签: php amazon-web-services amazon-s3 amazon bucket


    【解决方案1】:

    可以使用 s3client API 的 getObject 方法从 s3 存储桶下载文件

    /**
     * Gets file stored in Amazon s3 bucket.
     * 
     * @param string $awsAccesskey , access key used to access the Amazon bucket.
     * @param string $awsSecretKey , secret access key used to access the Amazon bucket.
     * @param string $bucketName , bucket name from which the file is to be accessed.
     * @param string $file_to_fetch , name of the file to be fetched, if the file is with in a folder it should also include the folder name.
     * @param string $path_to_save , path where the file received should be saved.
     * @return boolean true if the file is successfully received and saved else returns false.
     */
    function get_from_s3_bucket( $awsAccesskey, $awsSecretKey, $bucketName, $file_to_fetch, $path_to_save ) {
    
        try {
            $bucket  = $bucketName;
            require_once('S3.php');
            $s3      = new S3( $awsAccesskey, $awsSecretKey );
            $object  = $s3->getObject( $bucket, $file_to_fetch, $path_to_save );
            if ( $object->code == 200 ) {
                return true;
            } else {
                return false;
            }
        } catch ( Exception $e ) {
            return false;
        }
    }
    

    请参阅以下链接以获取更多指导:

    http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html

    【讨论】:

      【解决方案2】:

      使用 S3 独立类(我发现它与 AWS 开发工具包没有太大区别)getObject 有一个 saveTo 参数,您可以在其中传递文件名以将文件保存到...查看方法:

      /**
      * Get an object
      *
      * @param string $bucket Bucket name
      * @param string $uri Object URI
      * @param mixed $saveTo Filename or resource to write to
      * @return mixed
      */
      public static function getObject($bucket, $uri, $saveTo = false)
      {
          $rest = new S3Request('GET', $bucket, $uri, self::$endpoint);
          if ($saveTo !== false)
          {
              if (is_resource($saveTo))
                  $rest->fp =& $saveTo;
              else
                  if (($rest->fp = @fopen($saveTo, 'wb')) !== false)
                      $rest->file = realpath($saveTo);
                  else
                      $rest->response->error = array('code' => 0, 'message' => 'Unable to open save file for writing: '.$saveTo);
          }
          if ($rest->response->error === false) $rest->getResponse();
      
          if ($rest->response->error === false && $rest->response->code !== 200)
              $rest->response->error = array('code' => $rest->response->code, 'message' => 'Unexpected HTTP status');
          if ($rest->response->error !== false)
          {
              self::__triggerError(sprintf("S3::getObject({$bucket}, {$uri}): [%s] %s",
              $rest->response->error['code'], $rest->response->error['message']), __FILE__, __LINE__);
              return false;
          }
          return $rest->response;
      }
      

      获取课程的链接如下:https://aws.amazon.com/code/1448

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-07
        • 1970-01-01
        • 1970-01-01
        • 2019-09-06
        • 1970-01-01
        • 2021-07-18
        • 1970-01-01
        • 2011-12-15
        相关资源
        最近更新 更多