【问题标题】:AWS Multipart upload SDK not workingAWS 分段上传 SDK 不起作用
【发布时间】:2013-08-30 08:11:14
【问题描述】:

我需要有关使用 PHP 5.3 进行 AWS 分段上传的帮助 我在用 1. WAMP 服务器。 2. PHP 5.3 3. 操作系统:Windows 8 我正在尝试将大文件上传到我的 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);

请帮帮我。

【问题讨论】:

  • 你有什么错误吗?

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我已经更新了我的 SDK 并更改了我的代码,如下所示,

    ////////////////AWS 代码开始 ///////////////// /////////////////////////// 第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'];
    

    ///////////////////////// 第 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";
    }
    

    【讨论】:

      【解决方案3】:

      我将我的 SDK 更新到第 2 版,它工作正常。

      【讨论】:

        猜你喜欢
        • 2020-04-18
        • 2016-06-06
        • 1970-01-01
        • 2021-06-13
        • 2021-06-13
        • 2018-04-25
        • 2018-11-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多