Spaces/PHP: Multi part upload

October 22, 2019 155 views
DigitalOcean Spaces

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.

Be the first one to answer this question.