Question

How to use DigitalOcean Spaces with the AWS S3 SDKs?

DigitalOcean Spaces was designed to be inter-operable with the AWS S3 API in order allow users to continue using the tools they are already working with. In most cases, using Spaces with an existing S3 library requires configuring the endpoint value to be ${REGION}.digitaloceanspaces.com Though often how to change that setting is not well documented as examples tend to use the default AWS values. Third-party libraries tend to be better with this as they will support alternative, self-hosted object storage implementations like Mino or Ceph.

In the answers, let’s share some basic examples of working with Spaces using the AWS SDKs in various languages.

Subscribe
Share

Submit an 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.

c# asp.net core 2.1 - list objects

(I use this to verify my automated backup to spaces from an internal company asp.net core web application)

Add Nuget package: https://www.nuget.org/packages/AWSSDK.S3/

dotnet add package AWSSDK.S3 

Add references:

using System;
using System.Collections.Generic;
using System.Linq;
using Amazon.S3;

Declare constants for authentication:

private const string S3_SECRET_KEY = "your-secret-key-value";
private const string S3_ACCESS_KEY = "your-access-key-value";
private const string S3_HOST_ENDPOINT = "https://nyc3.digitaloceanspaces.com";
private const string S3_BUCKET_NAME = "your-bucket-name-here";

Sample method to fetch all filenames stored in Spaces bucket:

 public static List<string> GetFileListFromSpacesBackupStorage()
        {
            AmazonS3Config ClientConfig = new AmazonS3Config();
            ClientConfig.ServiceURL = S3_HOST_ENDPOINT;
            IAmazonS3 s3Client = new AmazonS3Client(S3_ACCESS_KEY, S3_SECRET_KEY, ClientConfig);
            var ObjectList = s3Client.ListObjectsAsync(S3_BUCKET_NAME).Result;
            var FileList = ObjectList.S3Objects.Select(c => c.Key).ToList();
            return FileList;
        }

c# asp.net core 2.1 - list objects

(I use this to verify my automated backup to spaces from an internal company asp.net core web application)

Add Nuget package: https://www.nuget.org/packages/AWSSDK.S3/

dotnet add package AWSSDK.S3 

Add references:

using System;
using System.Collections.Generic;
using System.Linq;
using Amazon.S3;

Declare constants for authentication:

private const string S3_SECRET_KEY = "your-secret-key-value";
private const string S3_ACCESS_KEY = "your-access-key-value";
private const string S3_HOST_ENDPOINT = "https://nyc3.digitaloceanspaces.com";
private const string S3_BUCKET_NAME = "your-bucket-name-here";

Sample method to fetch all filenames stored in Spaces bucket:

 public static List<string> GetFileListFromSpacesBackupStorage()
        {
            AmazonS3Config ClientConfig = new AmazonS3Config();
            ClientConfig.ServiceURL = S3_HOST_ENDPOINT;
            IAmazonS3 s3Client = new AmazonS3Client(S3_ACCESS_KEY, S3_SECRET_KEY, ClientConfig);
            var ObjectList = s3Client.ListObjectsAsync(S3_BUCKET_NAME).Result;
            var FileList = ObjectList.S3Objects.Select(c => c.Key).ToList();
            return FileList;
        }

Working C# example using Amazon AWSSDK (V2.x).

IAmazonS3 amazonS3Client = 
	AWSClientFactory.CreateAmazonS3Client("your-spaces-key", "your-spaces-key-secrete",
	new AmazonS3Config
	{
	   ServiceURL = "https://nyc3.digitaloceanspaces.com"
	}
);
var myBuckets = amazonS3Client.ListBuckets();

Working C# example using Amazon AWSSDK (V2.x).

IAmazonS3 amazonS3Client = 
	AWSClientFactory.CreateAmazonS3Client("your-spaces-key", "your-spaces-key-secrete",
	new AmazonS3Config
	{
	   ServiceURL = "https://nyc3.digitaloceanspaces.com"
	}
);
var myBuckets = amazonS3Client.ListBuckets();

Working C# example using Amazon AWSSDK (V2.x).

IAmazonS3 amazonS3Client = 
	AWSClientFactory.CreateAmazonS3Client("your-spaces-key", "your-spaces-key-secrete",
	new AmazonS3Config
	{
	   ServiceURL = "https://nyc3.digitaloceanspaces.com"
	}
);
var myBuckets = amazonS3Client.ListBuckets();

Kotlin (& Java) - [SDK]

You only need to setup “withEndpointConfiguration” correctly and then you can use others API normally, example here or in Amazon official document

var s3Client = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(AwsClientBuilder.EndpointConfiguration("https://sgp1.digitaloceanspaces.com", "sgp1"))
.withCredentials(AWSStaticCredentialsProvider(BasicAWSCredentials("key", "secret")))
                .build()

s3Client.listObjects("bucketName")

PHP - (AWS docs)

<?php

// Included aws/aws-sdk-php via Composer's autoloader
// Installed with: composer.phar require aws/aws-sdk-php
require 'vendor/autoload.php';
use Aws\S3\S3Client;

// Configure a client using Spaces
$client = new Aws\S3\S3Client([
        'version' => 'latest',
        'region'  => 'nyc3',
        'endpoint' => 'https://nyc3.digitaloceanspaces.com',
        'credentials' => [
                'key'    => 'ACCESS_KEY',
                'secret' => 'SECRET_KEY',
            ],
]);

// Create a new Space
$client->createBucket([
    'Bucket' => 'my-new-space-with-a-unique-name',
]);

// Listing all Spaces in the region
$spaces = $client->listBuckets();
foreach ($spaces['Buckets'] as $space){
    echo $space['Name']."\n";
}


// Upload a file to the Space
$insert = $client->putObject([
     'Bucket' => 'my-new-space-with-a-unique-name',
     'Key'    => 'file.ext',
     'Body'   => 'The contents of the file'
]);

PHP - (AWS docs)

<?php

// Included aws/aws-sdk-php via Composer's autoloader
// Installed with: composer.phar require aws/aws-sdk-php
require 'vendor/autoload.php';
use Aws\S3\S3Client;

// Configure a client using Spaces
$client = new Aws\S3\S3Client([
        'version' => 'latest',
        'region'  => 'nyc3',
        'endpoint' => 'https://nyc3.digitaloceanspaces.com',
        'credentials' => [
                'key'    => 'ACCESS_KEY',
                'secret' => 'SECRET_KEY',
            ],
]);

// Create a new Space
$client->createBucket([
    'Bucket' => 'my-new-space-with-a-unique-name',
]);

// Listing all Spaces in the region
$spaces = $client->listBuckets();
foreach ($spaces['Buckets'] as $space){
    echo $space['Name']."\n";
}


// Upload a file to the Space
$insert = $client->putObject([
     'Bucket' => 'my-new-space-with-a-unique-name',
     'Key'    => 'file.ext',
     'Body'   => 'The contents of the file'
]);

PHP - (AWS docs)

<?php

// Included aws/aws-sdk-php via Composer's autoloader
// Installed with: composer.phar require aws/aws-sdk-php
require 'vendor/autoload.php';
use Aws\S3\S3Client;

// Configure a client using Spaces
$client = new Aws\S3\S3Client([
        'version' => 'latest',
        'region'  => 'nyc3',
        'endpoint' => 'https://nyc3.digitaloceanspaces.com',
        'credentials' => [
                'key'    => 'ACCESS_KEY',
                'secret' => 'SECRET_KEY',
            ],
]);

// Create a new Space
$client->createBucket([
    'Bucket' => 'my-new-space-with-a-unique-name',
]);

// Listing all Spaces in the region
$spaces = $client->listBuckets();
foreach ($spaces['Buckets'] as $space){
    echo $space['Name']."\n";
}


// Upload a file to the Space
$insert = $client->putObject([
     'Bucket' => 'my-new-space-with-a-unique-name',
     'Key'    => 'file.ext',
     'Body'   => 'The contents of the file'
]);

PHP - (AWS docs)

<?php

// Included aws/aws-sdk-php via Composer's autoloader
// Installed with: composer.phar require aws/aws-sdk-php
require 'vendor/autoload.php';
use Aws\S3\S3Client;

// Configure a client using Spaces
$client = new Aws\S3\S3Client([
        'version' => 'latest',
        'region'  => 'nyc3',
        'endpoint' => 'https://nyc3.digitaloceanspaces.com',
        'credentials' => [
                'key'    => 'ACCESS_KEY',
                'secret' => 'SECRET_KEY',
            ],
]);

// Create a new Space
$client->createBucket([
    'Bucket' => 'my-new-space-with-a-unique-name',
]);

// Listing all Spaces in the region
$spaces = $client->listBuckets();
foreach ($spaces['Buckets'] as $space){
    echo $space['Name']."\n";
}


// Upload a file to the Space
$insert = $client->putObject([
     'Bucket' => 'my-new-space-with-a-unique-name',
     'Key'    => 'file.ext',
     'Body'   => 'The contents of the file'
]);