Report this

What is the reason for this report?

Connecting via PHP to your DigitalOcean space

Posted on February 17, 2021

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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello, @gschnieders

I believe that a similar question was asked before in our community. You can check the question here:

https://www.digitalocean.com/community/questions/whats-the-best-way-to-use-digital-ocean-spaces-upload-api-with-a-php-project

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.


1. Prerequisites

  1. Install the AWS SDK for PHP Use Composer to install the SDK:
composer require aws/aws-sdk-php
  1. Create DigitalOcean API Keys

    • Go to your DigitalOcean Control Panel.
    • Navigate to API > Spaces access keys and create an access key.
    • Note the Access Key and Secret Key.

2. Connecting to Your Space

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.

3. Performing Basic Operations

A. List All Files

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();
}

B. Delete a File

$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();
}

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.