By gschnieders
I tried to follow a couple of tutorials, but so far I got nowhere. I set-up an additional space, where overnight I transfer the images to that space. So when I want to delete a file on that space, I need to be able to connect to it and give via PHP a commando that I delete that file or multiple files. Can someone pinpoint me to a (simple) example how I can connect to the space and make some modifications (get a list of all the files, delete a file, …).
Kind regards,
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!
Hello, @gschnieders
I believe that a similar question was asked before in our community. You can check the question here:
Hope that this helps! Regards, Alex
To connect to your DigitalOcean Spaces and interact with it via PHP, you can use the AWS SDK for PHP, as DigitalOcean Spaces is compatible with the S3 API. Below is a step-by-step guide to set up the connection and perform basic operations like listing files, deleting files, and uploading files.
composer require aws/aws-sdk-php
Create DigitalOcean API Keys
Create a PHP file, e.g., connect_space.php, and use the following code:
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
// DigitalOcean Spaces configuration
$space_name = 'your-space-name';
$region = 'your-region'; // e.g., "nyc3" for New York
$endpoint = "https://$region.digitaloceanspaces.com"; // Base URL for Spaces
// Your API keys
$access_key = 'your-access-key';
$secret_key = 'your-secret-key';
try {
// Initialize the S3 client
$s3 = new S3Client([
'version' => 'latest',
'region' => $region,
'endpoint' => $endpoint,
'credentials' => [
'key' => $access_key,
'secret' => $secret_key,
],
]);
echo "Connected to DigitalOcean Space successfully.";
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
Replace:
your-space-name with the name of your Space.your-region (e.g., nyc3, ams3).your-access-key and your-secret-key with the API credentials you generated.try {
$result = $s3->listObjects([
'Bucket' => $space_name,
]);
echo "Files in Space:\n";
foreach ($result['Contents'] as $object) {
echo $object['Key'] . "\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
$file_to_delete = 'path/to/your/file.jpg';
try {
$s3->deleteObject([
'Bucket' => $space_name,
'Key' => $file_to_delete,
]);
echo "File '$file_to_delete' deleted successfully.";
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.