[Vuejs]-Uploading Multipart Files with Uppy/Laravel/Vue

0πŸ‘

βœ…

I found the PHP equivalent of the getSignedUrl method.

$command = $this->client->getCommand('UploadPart', [
    'Bucket'        => $this->bucket,
    'Key'           => $key,
    'PartNumber'    => $partNumber,
    'UploadId'      => $uploadId,
    'Body'          => '',
]);

Here’s my solution:

/**
 * Create a pre-signed URL for parts to be uploaded to.
 * @param Request $request
 * @param string $uploadId
 * @param int $partNumber
 * @return JsonResponse
 */
public function signPartUpload(Request $request, string $uploadId, int $partNumber)
{
    $key = $request->has('key') ? $request->get('key') : null;

    // Check key
    if (! is_string($key)) {
        return response()->json(['error' => 's3: the object key must be passed as a query parameter. For example: "?key=abc.jpg"'], 400);
    }

    // Check part number
    if (! intval($partNumber)) {
        return response()->json(['error' => 's3: the part number must be a number between 1 and 10000.'], 400);
    }

    // Create the upload part command and get the pre-signed URL
    try {
        $command = $this->client->getCommand('UploadPart', [
            'Bucket'        => $this->bucket,
            'Key'           => $key,
            'PartNumber'    => $partNumber,
            'UploadId'      => $uploadId,
            'Body'          => '',
        ]);

        $presignedUrl = $this->client->createPresignedRequest($command, '+20 minutes');
    } catch (Exception $e) {
        return response()->json(['error' => $e->getMessage()], 400);
    }

    // Convert the pre-signed URL to a string
    $presignedUrlString = (string) $presignedUrl->getUri();

    return response()->json(['url' => $presignedUrlString]);
}

Leave a comment