如何使用 php 和 Amazon S3 sdk 下载文件?

发布于 2024-12-04 16:07:38 字数 595 浏览 0 评论 0原文

我正在尝试使我的脚本能够通过 php 在 Amazon S3 存储桶中显示 test.jpg。 这是我到目前为止所得到的:

require_once('library/AWS/sdk.class.php');

$s3 = new AmazonS3($key, $secret);

$objInfo = $s3->get_object_headers('my_bucket', 'test.jpg');
$obj = $s3->get_object('my_bucket', 'test.jpg', array('headers' => array('content-disposition' => $objInfo->header['_info']['content_type'])));

echo $obj->body;

这只是转储页面上的文件数据。我认为我还需要发送内容处置标头,我认为这是在 get_object() 方法中完成的,但事实并非如此。

注意:我使用的是此处提供的 SDK:http://aws.amazon.com/sdkforphp/

I'm trying to make it so that my script will show test.jpg in an Amazon S3 bucket through php.
Here's what I have so far:

require_once('library/AWS/sdk.class.php');

$s3 = new AmazonS3($key, $secret);

$objInfo = $s3->get_object_headers('my_bucket', 'test.jpg');
$obj = $s3->get_object('my_bucket', 'test.jpg', array('headers' => array('content-disposition' => $objInfo->header['_info']['content_type'])));

echo $obj->body;

This just dumps out the file data on the page. I think I need to also send the content-disposition header, which I thought was being done in the get_object() method, but it isn't.

Note: I'm using the SDK available here: http://aws.amazon.com/sdkforphp/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

↙温凉少女 2024-12-11 16:07:38

这两种方法都对我有用。第一种方式看起来更简洁。

    $command = $s3->getCommand('GetObject', array(
       'Bucket' => 'bucket_name',
       'Key'    => 'object_name_in_s3'  
       'ResponseContentDisposition' => 'attachment; filename="'.$my_file_name.'"'
    ));

    $signedUrl = $command->createPresignedUrl('+15 minutes');
    echo $signedUrl;
    header('Location: '.$signedUrl);
    die();

或者是更冗长但仍然实用的方式。

    $object = $s3->getObject(array(
    'Bucket' => 'bucket_name',
    'Key'    => 'object_name_in_s3'   
    ));

    header('Content-Description: File Transfer');
    //this assumes content type is set when uploading the file.
    header('Content-Type: ' . $object->ContentType);
    header('Content-Disposition: attachment; filename=' . $my_file_name);
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');

    //send file to browser for download. 
    echo $object->body;

Both of these methods work for me. The first way seems more concise.

    $command = $s3->getCommand('GetObject', array(
       'Bucket' => 'bucket_name',
       'Key'    => 'object_name_in_s3'  
       'ResponseContentDisposition' => 'attachment; filename="'.$my_file_name.'"'
    ));

    $signedUrl = $command->createPresignedUrl('+15 minutes');
    echo $signedUrl;
    header('Location: '.$signedUrl);
    die();

Or a more wordy but still functional way.

    $object = $s3->getObject(array(
    'Bucket' => 'bucket_name',
    'Key'    => 'object_name_in_s3'   
    ));

    header('Content-Description: File Transfer');
    //this assumes content type is set when uploading the file.
    header('Content-Type: ' . $object->ContentType);
    header('Content-Disposition: attachment; filename=' . $my_file_name);
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');

    //send file to browser for download. 
    echo $object->body;
明媚如初 2024-12-11 16:07:38

在回显 $object 主体之前,通过回显内容类型标头来使其工作。

$objInfo = $s3->get_object_headers('my_bucket', 'test.jpg');
$obj = $s3->get_object('my_bucket', 'test.jpg');

header('Content-type: ' . $objInfo->header['_info']['content_type']);
echo $obj->body;

Got it to work by echo'ing out the content-type header before echo'ing the $object body.

$objInfo = $s3->get_object_headers('my_bucket', 'test.jpg');
$obj = $s3->get_object('my_bucket', 'test.jpg');

header('Content-type: ' . $objInfo->header['_info']['content_type']);
echo $obj->body;
宣告ˉ结束 2024-12-11 16:07:38

对于 PHP sdk3 更改 Maximus 答案的最后一行

    $object = $s3->getObject(array(
       'Bucket' => 'bucket_name',
       'Key'    => 'object_name_in_s3'   
    ));

    header('Content-Description: File Transfer');
    //this assumes content type is set when uploading the file.
    header('Content-Type: ' . $object->ContentType);
    header('Content-Disposition: attachment; filename=' . $my_file_name);
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');

    //send file to browser for download. 
    echo $object["Body"];

For PHP sdk3 change the last line of Maximus answer

    $object = $s3->getObject(array(
       'Bucket' => 'bucket_name',
       'Key'    => 'object_name_in_s3'   
    ));

    header('Content-Description: File Transfer');
    //this assumes content type is set when uploading the file.
    header('Content-Type: ' . $object->ContentType);
    header('Content-Disposition: attachment; filename=' . $my_file_name);
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');

    //send file to browser for download. 
    echo $object["Body"];
玩物 2024-12-11 16:07:38

如果您仍在寻找 2019 年以后的相关答案,使用适用于 PHP 3.x 的 AWS SDK,特别是使用 Composer 的“2006-03-01”,以下内容对我有用


...

/**
 * Download a file
 * 
 * @param   string  $object_key
 * @param   string  $file_name
 * @return  void
 */
function download($object_key, $file_name = '') {
  if ( empty($file_name) ) {
    $file_name = basename($file_path);
  }
  $cmd = $s3->getCommand('GetObject', [
    'Bucket'                        => '<aws bucket name>',
    'Key'                           => $object_key,
    'ResponseContentDisposition'    => "attachment; filename=\"{$file_name}\"",
  ]);
  $signed_url = $s3->createPresignedRequest($cmd, '+15 minutes') // \GuzzleHttp\Psr7\Request
                ->getUri() // \GuzzleHttp\Psr7\Uri
                ->__toString();
  header("Location: {$signed_url}");
}
download('<object key here>', '<file name for download>');

注意:这是为那些想要的人提供的解决方案以避免使用来自 AWS 的直接下载链接通过其服务器代理下载可能出现的问题。

If you're still looking for a relevant answer in 2019+, With AWS SDK for PHP 3.x and specifically '2006-03-01' with composer, the following worked for me


...

/**
 * Download a file
 * 
 * @param   string  $object_key
 * @param   string  $file_name
 * @return  void
 */
function download($object_key, $file_name = '') {
  if ( empty($file_name) ) {
    $file_name = basename($file_path);
  }
  $cmd = $s3->getCommand('GetObject', [
    'Bucket'                        => '<aws bucket name>',
    'Key'                           => $object_key,
    'ResponseContentDisposition'    => "attachment; filename=\"{$file_name}\"",
  ]);
  $signed_url = $s3->createPresignedRequest($cmd, '+15 minutes') // \GuzzleHttp\Psr7\Request
                ->getUri() // \GuzzleHttp\Psr7\Uri
                ->__toString();
  header("Location: {$signed_url}");
}
download('<object key here>', '<file name for download>');

NOTE: This is a solution for those who would want to avoid the issues that may arise from proxying the download through their servers by using a direct download link from AWS.

暖树树初阳… 2024-12-11 16:07:38

我将 Content-Disposition 标头添加到 getAuthenticatedUrl();

 // Example
 $timeOut = 3600; // in seconds
 $videoName = "whateveryoulike";
 $headers = array("response-content-disposition"=>"attachment");
 $downloadURL = $s3->getAuthenticatedUrl( FBM_S3_BUCKET, $videoName, FBM_S3_LIFETIME + $timeOut, true, true, $headers );

I added the Content-Disposition header to the getAuthenticatedUrl();

 // Example
 $timeOut = 3600; // in seconds
 $videoName = "whateveryoulike";
 $headers = array("response-content-disposition"=>"attachment");
 $downloadURL = $s3->getAuthenticatedUrl( FBM_S3_BUCKET, $videoName, FBM_S3_LIFETIME + $timeOut, true, true, $headers );
固执像三岁 2024-12-11 16:07:38

此脚本下载 S3 服务(例如 Amazon S3 或 DigitalOcean 空间)上所有目录中的所有文件。

  1. 配置您的凭据(请参阅类常量和类下的代码)
  2. 运行 composer require aws/aws-sdk-php
  3. 假设您将此脚本保存到 index.php,然后运行 ​​php index.php在控制台中运行 php 并让它撕裂!

请注意,我只是编写代码来完成工作,这样我就可以关闭我的 DO 帐户。它可以满足我的需要,但我可以进行一些改进以使其更具可扩展性。享受!


<?php

require 'vendor/autoload.php';
use Aws\S3\S3Client;

class DOSpaces {

    // Find them at https://cloud.digitalocean.com/account/api/tokens
    const CREDENTIALS_API_KEY           = '';
    const CREDENTIALS_API_KEY_SECRET    = '';

    const CREDENTIALS_ENDPOINT          = 'https://nyc3.digitaloceanspaces.com';
    const CREDENTIALS_REGION            = 'us-east-1';
    const CREDENTIALS_BUCKET            = 'my-bucket-name';

    private $client = null;

    public function __construct(array $args = []) {

        $config = array_merge([
            'version' => 'latest',
            'region'  => static::CREDENTIALS_REGION,
            'endpoint' => static::CREDENTIALS_ENDPOINT,
            'credentials' => [
                'key'    => static::CREDENTIALS_API_KEY,
                'secret' => static::CREDENTIALS_API_KEY_SECRET,
            ],
        ], $args);

        $this->client = new S3Client($config);
    }

    public function download($destinationRoot) {
        $objects = $this->client->listObjectsV2([
            'Bucket' => static::CREDENTIALS_BUCKET,
        ]);

        foreach ($objects['Contents'] as $obj){

            echo "DOWNLOADING " . $obj['Key'] . "\n";
            $result = $this->client->getObject([
                'Bucket' => 'dragon-cloud-assets',
                'Key' => $obj['Key'],
            ]);

            $this->handleObject($destinationRoot . $obj['Key'], $result['Body']);

        }
    }

    private function handleObject($name, $data) {
        $this->ensureDirExists($name);

        if (substr($name, -1, 1) !== '/') {
            echo "CREATING " . $name . "\n";
            file_put_contents($name, $data);
        }
    }

    private function ensureDirExists($name) {
        $dir = $name;
        if (substr($name, -1, 1) !== '/') {
            $parts = explode('/', $name);
            array_pop($parts);
            $dir = implode('/', $parts);
        }

        @mkdir($dir, 0777, true);
    }

}

$doSpaces = new DOSpaces([
    'endpoint' => 'https://nyc2.digitaloceanspaces.com',
    'credentials' => [
        'key'    => '12345',
        'secret' => '54321',
    ],
]);
$doSpaces->download('/home/myusername/Downloads/directoryhere/');

This script downloads all files in all directories on an S3 service, such as Amazon S3 or DigitalOcean spaces.

  1. Configure your credentials (See the class constants and the code under the class)
  2. Run composer require aws/aws-sdk-php
  3. Assuming you saved this script to index.php, then run php index.php in a console and let it rip!

Please note that I just wrote code to get the job done so I can close down my DO account. It does what I need it to, but there are several improvements I could have made to make it more extendable. Enjoy!


<?php

require 'vendor/autoload.php';
use Aws\S3\S3Client;

class DOSpaces {

    // Find them at https://cloud.digitalocean.com/account/api/tokens
    const CREDENTIALS_API_KEY           = '';
    const CREDENTIALS_API_KEY_SECRET    = '';

    const CREDENTIALS_ENDPOINT          = 'https://nyc3.digitaloceanspaces.com';
    const CREDENTIALS_REGION            = 'us-east-1';
    const CREDENTIALS_BUCKET            = 'my-bucket-name';

    private $client = null;

    public function __construct(array $args = []) {

        $config = array_merge([
            'version' => 'latest',
            'region'  => static::CREDENTIALS_REGION,
            'endpoint' => static::CREDENTIALS_ENDPOINT,
            'credentials' => [
                'key'    => static::CREDENTIALS_API_KEY,
                'secret' => static::CREDENTIALS_API_KEY_SECRET,
            ],
        ], $args);

        $this->client = new S3Client($config);
    }

    public function download($destinationRoot) {
        $objects = $this->client->listObjectsV2([
            'Bucket' => static::CREDENTIALS_BUCKET,
        ]);

        foreach ($objects['Contents'] as $obj){

            echo "DOWNLOADING " . $obj['Key'] . "\n";
            $result = $this->client->getObject([
                'Bucket' => 'dragon-cloud-assets',
                'Key' => $obj['Key'],
            ]);

            $this->handleObject($destinationRoot . $obj['Key'], $result['Body']);

        }
    }

    private function handleObject($name, $data) {
        $this->ensureDirExists($name);

        if (substr($name, -1, 1) !== '/') {
            echo "CREATING " . $name . "\n";
            file_put_contents($name, $data);
        }
    }

    private function ensureDirExists($name) {
        $dir = $name;
        if (substr($name, -1, 1) !== '/') {
            $parts = explode('/', $name);
            array_pop($parts);
            $dir = implode('/', $parts);
        }

        @mkdir($dir, 0777, true);
    }

}

$doSpaces = new DOSpaces([
    'endpoint' => 'https://nyc2.digitaloceanspaces.com',
    'credentials' => [
        'key'    => '12345',
        'secret' => '54321',
    ],
]);
$doSpaces->download('/home/myusername/Downloads/directoryhere/');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文