Report this

What is the reason for this report?

Spaces/PHP: Multi part upload

Posted on October 22, 2019

Hello,

I’m trying to use the AWS S3 PHP package to upload a file in multiple parts to DigitalOcean’s Spaces. I can successfully call createMultipartUpload and uploadPart to upload the file parts but whenever I call completeMultipartUpload I get:

AWS HTTP error: Client error: `POST https://---.ams3.digitaloceanspaces.com/staging-test/big.rar?uploadId=2~CBqqg-oDxOIPGe_EflgaODFdWkFmBuq` resulted in a `400 Bad Request` response: <?xml version="1.0" encoding="UTF-8"?><Error><Code>MalformedXML</Code>

I’m using the following code to upload the file / parts:

<?php

use Aws\S3\S3Client;

// Instantiate the client.
$s3 = new S3Client(array(
    "version"     => "latest",
    "region"      => 'ams3',
    "endpoint"    => 'https://ams3.digitaloceanspaces.com',
    "credentials" => array(
        "key"    => $settings->key,
        "secret" => $settings->secret,
    ),
));

$file = fopen($filename, 'r');

$key = "big.rar"
$bucket = "apptest"

// 1. Create a new multipart upload and get the upload ID.
$response = $s3->createMultipartUpload(array(
    'Bucket' => $bucket,
    'Key'    => $key
);
$uploadId = $result['UploadId'];

// 2. Upload the data in parts.
$parts = array();
$partNumber = 1;
while (!feof($file)) {
    $result = $s3->uploadPart(array(
        'Bucket'     => $bucket,
        'Key'        => $key,
        'UploadId'   => $uploadId,
        'PartNumber' => $partNumber,
        'Body'       => fread($file, 5242880),
    ));
    $parts[] = array(
        'PartNumber' => $partNumber++,
        'ETag'       => $result['ETag'],
    );
}

// 3. Complete multipart upload.
$result = $s3->completeMultipartUpload(array(
    'Bucket'   => $bucket,
    'Key'      => $key,
    'UploadId' => $uploadId,
    'Parts'    => $parts,
));
$url = $result['Location'];

fclose($file);

Shouldn’t it work? Thank you.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

Looking at the request message the endpoint seems to have a weird format - POST https://---.ams3.digitaloceanspaces.com

I’ll recommend checking if the endpoint is correctly configured in each part of your script ams3.digitaloceanspaces.com

Hope that this helps!

Here’s the solution: https://stackoverflow.com/a/31413193/1099019

Array structure is different in v3 of the sdk.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.