Amazon AWSSDKforPHP too slow

Amazon AWSSDKforPHP too slow

Hi,

I am using Amazon AWSSDKforPHP to connect my web application to S3. But there is a problem with the process or service requests that make it too slow.

For example, I have this code:

// Iterate an array of user images
foreach($images as $image){
    // Return the Bucket URL for this image
    $urls[] = $s3->get_object_url($bucket, 'users/'.trim($image).'.jpg', '5 minutes');
}

Assuming $ images is an array of user images, this returns an array called $ urls, which has (in his words) the URL of these images with credentials for 5 minutes. This request takes at least 6 seconds with 35 images, and this is normal. But ... when the images do not exist in the bucket, I want to set the default image for the user, something like "images / noimage.png". Here is the code:

// Iterate an array of user images
foreach($images as $image){

    // Check if the object exists in the Bucket
    if($s3->if_object_exists($bucket, 'users/'.trim($image).'.jpg')){
        // Return the Bucket URL for this image
        $urls[] = $s3->get_object_url($bucket, 'users/'.trim($image).'.jpg', '5 minutes');
    } else { 

        // Return the default image
        $urls[] = 'http://www.example.com/images/noimage.png';
    }

}

, SLOOOOOW. "$ s3- > if_object_exists()" Script 40 35 !

Script, cURL:

// Iterate an array of user images
foreach($images as $image){

    // Setup cURL
    $ch = curl_init($s3->get_object_url($bucket, 'users/'.trim($image).'.jpg', '1 minutes') );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    // Get Just the HTTP response code
    $res = curl_getinfo($ch,CURLINFO_HTTP_CODE);

    if($res == 200){ //the image exists
        $urls[] = $s3->get_object_url($bucket, 'users/'.trim($image).'.jpg', '5 minutes');
    }else{ // The response is 403
        $urls[] = 'http://www.example.com/images/noimage.png';
    }
}

Script 16 18 . , : (.

, .

.

+5
3

, . / , ?

, API, , 35 , . , . .

, , . , , .

+1

, S3, - s3fs . s3fs ( , EC2).

PHP (DirectoryIterator ..).

, , , S3 API, - , API .

+1

, if_object_exists() , AWS.

"thatidiotguy" :

S3 API, , / script? , PHP .

He is right.

Instead of calling, if_object_exists()you can instead call get_object_list()once - at the beginning of the script, and then compare your user’s URL with the list using the PHP function in_array().

You should see acceleration of about a million percent. However, do not quote me .;)

0
source

All Articles