How to force the download of an AWS Bucket S3 object with PHP?

Asked

Viewed 903 times

8

I know how to upload an object to the AWS S3 Bucket this way:

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) {}

But I don’t know how to force the download of an object I can use $oClientAws->GetObject (parametros ...) and change the type of header content, but this just shows my file in the browser, but does not actually download the file.

There’s some right way to do it?

  • With no chance of being duplicated, mine refers to the S3 of aws and not just to force the download of a file that is quite simple. But beauty.

  • Exactly, the content-type works but the file comes directly from Bucket and not from an ftp, and to force the download of an ftp for example I must give a fread at the end of the header changes or is there actually a path in the directory to the file, but in Bucket is directly in aws, I found a method in the API called saveTo(), which saves in a directory the file and this will help, I hope I have been clear, using the Bucket from aws it is easier to understand the situation of not having the physical file in an ftp. Hugs.

  • In the client, with saveTo() it was possible only on the server because it should be informed the folder that it should be saved, but this helps me because I can then read and force the download and remove that file that was saved on the server.

  • 3

    I voted to reopen because it is the S3 system. It is not duplicity.

2 answers

2

Unfortunately there is no parameter to force the download. What you can do is:

  • Create your own url, for example, download.php, capture the Amazon object and use source code similar to the one below

    $result = $s3->getObject(array(
    'Bucket' => $bucket,
    'Key'    => $keyname
    ));
    header("Content-Type: {$result['ContentType']}");
    echo $result['Body'];
    

1

The author received a reply from Soen, imported to here by chance help other users.

Use the S3 class standalone (which I found is not much different from the AWS SDK) with the getObject:

/**
* 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;
}

Source: https://aws.amazon.com/code/1448

Browser other questions tagged

You are not signed in. Login or sign up in order to post.