Use DigitalOcean Spaces with AWS S3 SDKs

Last verified 22 Jun 2026

Spaces Object Storage is an S3-compatible service for storing and serving large amounts of data. The built-in Spaces CDN minimizes page load times, improves performance, and reduces bandwidth and infrastructure costs.

The Spaces API is compatible with the AWS S3 API, so you can use many existing S3 tools and libraries with Spaces. One common use case is managing Spaces buckets programmatically with AWS S3 software development kits (SDKs).

AWS provides S3 SDKs for a variety of languages, and many of them are compatible with the Spaces API. For more information about Spaces compatibility with the S3 API, see the Spaces API documentation.

In S3-compatible tools, a key is the name of an object in a bucket.

For complete SDK reference documentation, see:

Install the SDK

Install the AWS SDK for your language with its package manager.

npm install @aws-sdk/client-s3
go get -u github.com/aws/aws-sdk-go
php composer.phar require aws/aws-sdk-php
pip install boto3
gem install aws-sdk-s3
dotnet add package AWSSDK.S3

Create Access Keys

To use the Spaces API, create an access key and secret key for your bucket.

Copy and securely store your spaces key and its secret to use in your environment so that they’re available to your code.

Configure a Client

To use Spaces with tools or libraries designed for the S3 API, configure the endpoint setting to point to your bucket region.

Use the format $<your-region>.digitaloceanspaces.com, where $<your-region> is the DigitalOcean datacenter region, such as nyc3, where the bucket is located.

Using presigned URLs does not allow transferred files to be cached when using the Spaces CDN. Attempting to do so may result in double the bandwidth charge without the CDN’s performance benefit.

Warning

Because of AWS-specific behavior in all SDKs except Python 3, bucket creation requires an AWS region such as us-east-1 in the client configuration. When a custom region is specified during bucket creation, these SDKs send a different payload, which causes an error.

Setting us-east-1 doesn’t reduce performance, regardless of the bucket location. The SDK uses the region for validation only. It sends the request to the configured custom endpoint instead.

Note
To create a bucket successfully with this SDK, set region to us-east-1, which is an AWS region name. The DigitalOcean datacenter region is determined by the endpoint value.
import { S3 } from "@aws-sdk/client-s3";

const s3Client = new S3({
    forcePathStyle: false,
    endpoint: "https://<your-region>.digitaloceanspaces.com",
    region: "us-east-1",
    credentials: {
      accessKeyId: process.env.SPACES_KEY,
      secretAccessKey: process.env.SPACES_SECRET
    }
});

export { s3Client };

Set forcePathStyle to false to use subdomain-style, or virtual-hosted-style requests.

Note
To create a bucket successfully with this SDK, set region to us-east-1, which is an AWS region name. The DigitalOcean datacenter region is determined by the endpoint value.

The following example includes additional imports after importing os required by later Go examples below.

package main

import (
    "os"
    "fmt"
    "io"
    "strings"
    "time"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/credentials"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
)

func main() {
    key := os.Getenv("SPACES_KEY")
    secret := os.Getenv("SPACES_SECRET")

    s3Config := &aws.Config{
        Credentials: credentials.NewStaticCredentials(key, secret, ""),
        Endpoint:    aws.String("https://<your-region>.digitaloceanspaces.com"),
        Region:      aws.String("us-east-1"),
        S3ForcePathStyle: aws.Bool(false),
    }

    newSession := session.New(s3Config)
    s3Client := s3.New(newSession)

    ...

Set S3ForcePathStyle to false to use subdomain-style, or virtual-hosted-style requests.

Note
To create a bucket successfully with this SDK, set region to us-east-1, which is an AWS region name. The DigitalOcean datacenter region is determined by the endpoint value.

This example loads the AWS SDK for PHP with Composer’s autoloader.

<?php

require 'vendor/autoload.php';
use Aws\S3\S3Client;

$client = new Aws\S3\S3Client([
        'version' => 'latest',
        'region'  => 'us-east-1',
        'endpoint' => 'https://<your-region>.digitaloceanspaces.com',
        'use_path_style_endpoint' => false,
        'credentials' => [
                'key'    => getenv('SPACES_KEY'),
                'secret' => getenv('SPACES_SECRET'),
            ],
]);

Set use_path_style_endpoint to false to use subdomain-style, or virtual-hosted-style requests.

import os
import boto3

session = boto3.session.Session()
client = session.client('s3',
                        region_name='<your-region>',
                        endpoint_url='https://<your-region>.digitaloceanspaces.com',
                        aws_access_key_id=os.getenv('SPACES_KEY'),
                        aws_secret_access_key=os.getenv('SPACES_SECRET'))
Note
To create a bucket successfully with this SDK, set region to us-east-1, which is an AWS region name. The DigitalOcean datacenter region is determined by the endpoint value.
require 'aws-sdk-s3'

client = Aws::S3::Client.new(
  access_key_id: ENV['SPACES_KEY'],
  secret_access_key: ENV['SPACES_SECRET'],
  endpoint: 'https://<your-region>.digitaloceanspaces.com',
  force_path_style: false,
  region: 'us-east-1'
)

Set force_path_style to false to use subdomain-style, or virtual-hosted-style requests.

using System;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;

string accessKey = Environment.GetEnvironmentVariable("SPACES_KEY");
string secretKey = Environment.GetEnvironmentVariable("SPACES_SECRET");

AmazonS3Config config = new AmazonS3Config();
config.ServiceURL = "https://<your-region>.digitaloceanspaces.com";

AmazonS3Client s3Client = new AmazonS3Client(
        accessKey,
        secretKey,
        config
        );

Calling S3 API Commands

After you configure the client, you can call supported S3 commands against Spaces.

The following examples create a bucket, and then list all buckets in the account.

import { S3Client, CreateBucketCommand, ListBucketsCommand } from '@aws-sdk/client-s3';

async function main() {
  const client = new S3Client({
    endpoint: 'https://<your-region>.digitaloceanspaces.com',
    region: 'us-east-1',
    forcePathStyle: false,
    credentials: {
      accessKeyId: process.env.SPACES_KEY,
      secretAccessKey: process.env.SPACES_SECRET
    }
  });

  await client.send(new CreateBucketCommand({ Bucket: '<your-new-space>' }));
  const { Buckets } = await client.send(new ListBucketsCommand({}));
  console.log(Buckets?.map(b => b.Name));
}
main().catch(console.error);
package main

import (
    "fmt"
    "log"
    "os"

    "github.com/minio/minio-go"
)

func main() {
    accessKey := os.Getenv("SPACES_KEY")
    secKey := os.Getenv("SPACES_SECRET")
    endpoint := "<your-region>.digitaloceanspaces.com"
    spaceName := "<your-new-space>"
    ssl := true

    client, err := minio.New(endpoint, accessKey, secKey, ssl)
    if err != nil { log.Fatal(err) }

    if err := client.MakeBucket(spaceName, "us-east-1"); err != nil {
        log.Fatal(err)
    }

    spaces, err := client.ListBuckets()
    if err != nil { log.Fatal(err) }
    for _, space := range spaces {
        fmt.Println(space.Name)
    }
}
import boto3, os
from botocore.client import Config

session = boto3.session.Session()
client = session.client(
    's3',
    region_name='<your-region>',
    endpoint_url='https://<your-region>.digitaloceanspaces.com',
    aws_access_key_id=os.getenv('SPACES_KEY'),
    aws_secret_access_key=os.getenv('SPACES_SECRET'),
    config=Config(s3={'addressing_style': 'virtual'})
)

client.create_bucket(Bucket='<your-new-space>')
print([b['Name'] for b in client.list_buckets()['Buckets']])
require 'aws-sdk-s3'

client = Aws::S3::Client.new(
  access_key_id: ENV['SPACES_KEY'],
  secret_access_key: ENV['SPACES_SECRET'],
  endpoint: 'https://<your-region>.digitaloceanspaces.com',
  force_path_style: false,
  region: 'us-east-1'
)

client.create_bucket(bucket: '<your-new-space>')
client.list_buckets.buckets.each { |b| puts b.name }
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;

$client = new S3Client([
  'version' => 'latest',
  'region' => 'us-east-1',
  'endpoint' => 'https://<your-region>.digitaloceanspaces.com',
  'use_path_style_endpoint' => false,
  'credentials' => [
    'key'    => getenv('SPACES_KEY'),
    'secret' => getenv('SPACES_SECRET'),
  ],
]);

$client->createBucket(['Bucket' => '<your-new-space>']);
$result = $client->listBuckets();
foreach ($result['Buckets'] as $b) { echo $b['Name'], PHP_EOL; }

We can't find any results for your search.

Try using different keywords or simplifying your search terms.