Uploading Files to DigitalOcean Spaces

This code example demonstrates how to upload a file to DigitalOcean Spaces using the AWS SDK for PHP. It uses the S3Client class to interact with Spaces.

To upload files to DigitalOcean Spaces, you can use the AWS SDK for PHP which provides an easy-to-use API for interacting with Spaces. Here is an example of how to upload a file to a Space using the AWS SDK for PHP:

 <?php
require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;

// Set up the AWS SDK for PHP client
$client = new S3Client([
    'version' => 'latest',
    'region'  => '<your-space-region>',
    'endpoint' => 'https://<your-space-name>.<your-space-region>.digitaloceanspaces.com',
    'credentials' => [
        'key'    => '<your-access-key>',
        'secret' => '<your-secret-key>',
    ],
]);

// Set up the file to upload
$file_path = '/path/to/your/file.ext';
$file_name = 'file.ext';

// Upload the file to the Space
try {
    $result = $client->putObject([
        'Bucket' => '<your-space-name>',
        'Key'    => $file_name,
        'Body'   => fopen($file_path, 'r'),
    ]);
    echo "File uploaded successfully!\n";
} catch (AwsException $e) {
    echo "Error uploading file: " . $e->getMessage() . "\n";
}
?>

In this code, we set up the AWS SDK for PHP client with our DigitalOcean Space credentials and endpoint. We then set up the file to upload and use the putObject() method to upload the file to the Space.

Note that you need to replace <your-space-region>, <your-space-name>, <your-access-key>, and <your-secret-key> with your own values, and you may need to modify the code depending on your specific needs.