Access Storage with AWS S3 SDKs

Introduction

CloudBit Object Storage is an S3-compatible object storage service that lets you store and serve large amounts of data.
The CloudBit Object Storage API is inter-operable with the AWS S3 API, meaning you can use existing S3 tools and libraries with Spaces. A common use case is managing CloudBit Object Storage programmatically with AWS’ S3 SDKs.

Install the SDK

Install the AWS SDK using the package manager for your language of choice.
Java Script
Go
PHP
Python
Ruby
1
npm install aws-sdk
Copied!
1
go get -u github.com/aws/aws-sdk-go
Copied!
1
php composer.phar require aws/aws-sdk-php
Copied!
1
pip install boto3
Copied!
1
gem install aws-sdk-s3
Copied!

Obtain Access & Secret Keys

You are able to retrieve the access & secret keys in our customer portal:
The examples below rely on environment variables to access these keys. Export ACCESS_KEY and SECRET_KEY to your environment (e.g. export ACCESS_KEY=DSJE2334JAS) to make them available to your code.

SDKs

After you set up and configure an SDK, you can follow the examples below to see how to perform common CloudBit Object Storage operations in JavaScript, Go, PHP, Python and Ruby.
Java Script
Go
PHP
Python
Ruby
1
const AWS = require('aws-sdk');
2
const fs = require('fs'); // Needed for example below
3
4
const spacesEndpoint = new AWS.Endpoint('<S3-ENDPOINT>');
5
const s3 = new AWS.S3({
6
endpoint: spacesEndpoint,
7
accessKeyId: process.env.ACCESS_KEY,
8
secretAccessKey: process.env.SECRET_KEY
9
});
Copied!
1
package main
2
3
import (
4
"os"
5
// Additional imports needed for examples below
6
"fmt"
7
"io"
8
"strings"
9
"time"
10
11
"github.com/aws/aws-sdk-go/aws"
12
"github.com/aws/aws-sdk-go/aws/credentials"
13
"github.com/aws/aws-sdk-go/aws/session"
14
"github.com/aws/aws-sdk-go/service/s3"
15
)
16
17
func main() {
18
key := os.Getenv("ACCESS_KEY")
19
secret := os.Getenv("SECRET_KEY")
20
21
s3Config := &aws.Config{
22
Credentials: credentials.NewStaticCredentials(key, secret, ""),
23
Endpoint: aws.String("https://<ENDPOINT>"),
24
Region: aws.String("us-east-1"),
25
}
26
27
newSession := session.New(s3Config)
28
s3Client := s3.New(newSession)
29
30
// ...
Copied!
This SDK requires the region to be us-east-1, an AWS region name, to successfully create a new Bucket. The CloudBit Object Storage datacenter region is based on the <ENDPOINT> value.
1
<?php
2
3
// Included aws/aws-sdk-php via Composer's autoloader
4
require 'vendor/autoload.php';
5
use Aws\S3\S3Client;
6
7
$client = new Aws\S3\S3Client([
8
'version' => 'latest',
9
'region' => 'us-east-1',
10
'endpoint' => 'https://<ENDPOINT>',
11
'credentials' => [
12
'key' => getenv('ACCESS_KEY'),
13
'secret' => getenv('SECRET_KEY'),
14
],
15
]);
Copied!
1
import os
2
import boto3
3
4
session = boto3.session.Session()
5
client = session.client('s3',
6
region_name='nyc3',
7
endpoint_url='https://<ENDPOINT>',
8
aws_access_key_id=os.getenv('ACCESS_KEY'),
9
aws_secret_access_key=os.getenv('SECRET_KEY'))
Copied!
1
require 'aws-sdk-s3'
2
3
client = Aws::S3::Client.new(
4
access_key_id: ENV['ACCESS_KEY'],
5
secret_access_key: ENV['SECRET_KEY'],
6
endpoint: 'https://<ENDPOINT>',
7
region: 'us-east-1'
8
)
Copied!
Please replace the <ENDPOINT> place holder with the correct endpoint: Location BIT1: os.bit1.cloudbit.ch
Last modified 1yr ago