Question

I need Help on AWS multi-part Upload using PHP 5.3 I am using 1. WAMP Server. 2. PHP 5.3 3. OS: Windows 8 I am trying to upload a Big file to My Amazon S3 bucket.

And tried in many ways. I followed the code procedure from http://docs.aws.amazon.com/AmazonS3/latest/dev/LLuploadFilePHP.html

I wrote PHP code but I don't know what mistake I did.

My Code:

<?php
        require_once './sdk/sdk.class.php';

    use \AmazonS3;

    $ufile = $_FILES['ufile'];
    $filepath = $ufile['tmp_name'];
    $bucket = '**My_bkt**';
    var_dump($ufile);
    print $keyname = \date("Y") . "/" . \date("F") . "/" . \date("d") . "/" . $ufile['name'] . "<BR />"; 


    $api_key = "***my_api_key***";
    $secret_key = "***my_secret_key***";


    // Define a megabyte.
    define('MB', 1048576);

    // Instantiate the class
    //$s3 = new AmazonS3();
    $s3 = new AmazonS3(array(
        'key' => $api_key,
        'secret' => $secret_key,
            ));

    // 1. Initiate a new multipart upload.
    /* @var $response type */
    $response = $s3->initiate_multipart_upload($bucket, $keyname);

    // Get the Upload ID.
    $upload_id = (string) $response->body->UploadId;

    // 2. Upload parts.
    // Get part list for a given input file and given part size.
    // Returns an associative array.
    $parts = $s3->get_multipart_counts(filesize($filepath), 3 * MB);

    foreach ($parts as $i => $part) {
        // Upload part and save response in an array.
        $responses[] = $s3->upload_part($bucket, $keyname, $upload_id, array(
            'fileUpload' => $filepath,
            'partNumber' => ($i + 1),
            'seekTo' => (integer) $part['seekTo'],
            'length' => (integer) $part['length'],
                )
        );
    }

    // 3. Complete multipart upload. We need all part numbers and ETag values.
    $parts = $s3->list_parts($bucket, $keyname, $upload_id);

    $response = $s3->complete_multipart_upload($bucket, $keyname, $upload_id, $parts);

    var_dump($response);

Please help me.

Was it helpful?

Solution 2

I have Updated My SDK and Changed My Code like bellow,

////////////////// AWS Code Begin //////////////////// /////////////////////////// Step 1 /////////////////////////////

$ufile = $_FILES['Filedata'];
$filename = $ufile['tmp_name'];
$filesize = $ufile['size'];

/*     * ************ Calculating Number of Parts ******************* */
$number_of_parts = 0;
$r = $filesize % PART; // Remainder
$q = floor($filesize / PART); // Quotient
if ($r != 0) {
    $number_of_parts = $q + 1;
} else {
    $number_of_parts = $q;
}
$bucket = 'isource123';
$keyname = date("Y") . "/" . date("F") . "/" . date("d") . "/" . $ufile['name'];

///////////////////////////// Step 2 ///////////////////////////// // Create a service builder using a configuration file

$aws = Aws::factory('./aws/Aws/Common/Resources/aws-config.php');

// Get the client from the builder by namespace
    $client = $aws->get('S3');
$uploader = \Aws\S3\Model\MultipartUpload\UploadBuilder::newInstance()
        ->setClient($client)
        ->setSource($filename)
        ->setBucket($bucket)
        ->setKey($keyname)
        ->setOption('Metadata', array('Foo' => 'Bar'))
        ->setOption('CacheControl', 'max-age=3600')
        ->setConcurrency($number_of_parts)
        ->build();

try {
    $uploader->upload();
    echo "Upload complete.\n";
} catch (MultipartUploadException $e) {
    $uploader->abort();
    echo "Upload failed.\n";
}

OTHER TIPS

I know you're using the older SDK 1 here, so my answer doesn't apply directly to what you've posted. That said, SDK 1.x is no longer being updated and SDK 2.x is what all new work should be using (as per the AWS SDK for PHP team).

If you do update your project to use SDK 2, then take a look at S3Client::upload(). It should greatly simplify what you're trying to do here.

I updated My SDK to version 2 and it's working fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top