s3-sign.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. require 'vendor/autoload.php';
  3. header('Access-Control-Allow-Origin: *');
  4. header("Access-Control-Allow-Headers: GET");
  5. // CONFIG: Change these variables to a valid region and bucket.
  6. $awsRegion = 'eu-west-2';
  7. $bucket = 'uppy-test';
  8. // Directory to place uploaded files in.
  9. $directory = 'uppy-php-example';
  10. // Create the S3 client.
  11. $s3 = new Aws\S3\S3Client([
  12. 'version' => 'latest',
  13. 'region' => $awsRegion,
  14. ]);
  15. // Retrieve data about the file to be uploaded from the request body.
  16. $body = json_decode(file_get_contents('php://input'));
  17. $filename = $body->filename;
  18. $contentType = $body->contentType;
  19. // Prepare a PutObject command.
  20. $command = $s3->getCommand('putObject', [
  21. 'Bucket' => $bucket,
  22. 'Key' => "{$directory}/{$filename}",
  23. 'ContentType' => $contentType,
  24. 'Body' => '',
  25. ]);
  26. $request = $s3->createPresignedRequest($command, '+5 minutes');
  27. header('content-type: application/json');
  28. echo json_encode([
  29. 'method' => $request->getMethod(),
  30. 'url' => (string) $request->getUri(),
  31. 'fields' => [],
  32. // Also set the content-type header on the request, to make sure that it is the same as the one we used to generate the signature.
  33. // Else, the browser picks a content-type as it sees fit.
  34. 'headers' => [
  35. 'content-type' => $contentType,
  36. ],
  37. ]);