AWS Multipart上载SDK无法正常工作

时间:2013-08-30 08:11:14

标签: php amazon-web-services amazon-s3 amazon-ec2 multipart

我需要使用PHP 5.3帮助AWS多部分上传 我在用  1. WAMP服务器。  2. PHP 5.3  3.操作系统:Windows 8 我正在尝试将Big文件上传到My Amazon S3存储桶。

并尝试过很多方面。我遵循了代码程序 http://docs.aws.amazon.com/AmazonS3/latest/dev/LLuploadFilePHP.html

我编写了PHP代码,但我不知道我做了什么错误。

我的代码:

<?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);

请帮帮我。

3 个答案:

答案 0 :(得分:1)

我知道您在这里使用的是较旧的SDK 1,因此我的回答并不直接适用于您发布的内容。也就是说,SDK 1.x不再被更新,SDK 2.x是所有新工作应该使用的(根据AWS SDK for PHP团队)。

如果您确实更新项目以使用SDK 2,请查看S3Client::upload()。它应该极大地简化你在这里尝试做的事情。

答案 1 :(得分:0)

我已更新了我的SDK并更改了我的代码,如bellow,

////////////////// AWS Code Begin //////////////////// /////////////////////////// 步骤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 //////////////// ///////////// //使用配置文件

创建服务构建器
$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";
}

答案 2 :(得分:0)

我将我的SDK更新为版本2,并且工作正常。