How do I create a download link for an Amazon S3 bucket?

I am learning Amazon S3 using the S3 PHP Class. I loaded all my files into my S3 bucket, now I want to create links for every available file in my bucket.

Will the following function work for me?

public static function getAuthenticatedURL($bucket, $uri, $lifetime, $hostBucket = false, $https = false)
{

}

    $s3 = new S3('access-key', 'secret-key');
    $s3->getAuthenticatedURL($bucket, $uri, $lifetime, $hostBucket = false, $https = false);

Or another type function get_object_url, but get_object_url()not in my S3 class.

I am using the Undesigned Amazon S3 PHP class .

+6
source share
4 answers

If you want the public to go to the bucket, it's as simple as

Http: // [YourBucketName] .s3.amazonaws.com / [YourFileName]

So far you are setting permissions correctly.

, URL (, , ). SDK Amazon: http://aws.amazon.com/sdkforphp/, , .

$s3->getObjectUrl($bucket, $filename, '5 minutes');

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

+5

URL- S3 :

http(s)://<bucket>.s3.amazonaws.com/<object>
http(s)://s3.amazonaws.com/<bucket>/<object>
+11

, , , ( ). : URL-, S3, "file_get_contents" .., , ( ). pthreads, php.

: S3 Amazon, S3.php .

: (. , URL-)

http(s)://<bucket>.s3.amazonaws.com/<object>
http(s)://s3.amazonaws.com/<bucket>/<object>

HTTPS ( ):

https://amazon.com/file/you/wanted.xxx?ID:XXXXX?SIG:YYYYY  

(1) Create https: // url and use the multi curl tool to get them all at the same time (recommended).

The simplest example:

$url = /path/to_the/file_name/file.ext  

//note check amazon to confirm the path which will contain only "_" and no spaces.

$s3 = new S3($awsAccessKeyID, $awsSecretKey);
$curls[] = $s3->get_object_url($bucketName, $uri, '1 hour');
var_dump($results = multiCurlRequest($curls));    

Additional Information:

http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.S3.S3Client.html#_getObjectUrl http://undesigned.org.za/2007/10/22/amazon -s3-php-class / documentation

FYI:

function multiCurlRequest($curlList = array(),$user = '', $pass = '',$timeout = self::MULTI_REQ_TIMEOUT_SECS, $retTxfr = 1) {

    if (empty($curlList) || count($curlList) == 0) return false;

    $master = curl_multi_init();
    $node_count = count($curlList);

    for ($i = 0; $i < $node_count; $i++) {
        $ch[$i] = curl_init($curlList[$i]);
        curl_setopt($ch[$i], CURLOPT_TIMEOUT, $timeout); // -- timeout after X seconds
        curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, $retTxfr);
        curl_setopt($ch[$i], CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch[$i], CURLOPT_USERPWD, "{$user}:{$pass}");
        curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);
        curl_multi_add_handle($master, $ch[$i]);
    }

    // -- get all requests at once, finish when done or timeout met --
    do {  curl_multi_exec($master, $running);  }
    while ($running > 0);

    $results = array();

    // -- get results from requests --
    for ($i = 0; $i < $node_count; $i++) {
        $results[$i] = curl_multi_getcontent($ch[$i]);
        if ((int) curl_getinfo($ch[$i], CURLINFO_HTTP_CODE) > 399 || empty($results[$i])) {
            $this->set_request(  [ ['label' => '404', 'href' => $results[$i], '404' => '1' ] ] );
            unset($results[$i]);
        }
        curl_multi_remove_handle($master, $ch[$i]);
        curl_close($ch[$i]);
    }

    curl_multi_close($master);
    if (empty($results)) return false;
    //$results = array_values($results); // -- removed as we want the original positions
    return $results;
}
0
source

If you use aws-sdk-php v3and your file is private.

$cmd = $s3Client->getCommand('GetObject', [
    'Bucket' => 'my-bucket',
    'Key' => 'testKey'
]);

$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');

$presignedUrl = (string) $request->getUri();
0
source

All Articles