Storage
SDK examples
Edge Storage is fully compatible with AWS S3 SDKs. Use the official AWS SDK for your language with our endpoint.
On this page6 sections
The examples below use the official AWS SDKs for JavaScript and TypeScript, Python, Go and Ruby. Pick your language in each code block.
Install the SDK#
Terminal
npm install @aws-sdk/client-s3Terminal
pip install boto3Terminal
go get github.com/aws/aws-sdk-go-v2
go get github.com/aws/aws-sdk-go-v2/service/s3
go get github.com/aws/aws-sdk-go-v2/credentialsTerminal
gem install aws-sdk-s3Set up the client#
JavaScript
import {
S3Client,
PutObjectCommand,
GetObjectCommand,
ListObjectsV2Command
} from '@aws-sdk/client-s3'
const client = new S3Client({
endpoint: 'https://storage.edge.network',
region: 'us-east-1',
forcePathStyle: true,
credentials: {
accessKeyId: process.env.EDGE_ACCESS_KEY,
secretAccessKey: process.env.EDGE_SECRET_KEY
}
})Python
import boto3
import os
client = boto3.client(
's3',
endpoint_url='https://storage.edge.network',
aws_access_key_id=os.environ['EDGE_ACCESS_KEY'],
aws_secret_access_key=os.environ['EDGE_SECRET_KEY'],
region_name='us-east-1'
)Go
package main
import (
"context"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func main() {
cfg, _ := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("us-east-1"),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
os.Getenv("EDGE_ACCESS_KEY"),
os.Getenv("EDGE_SECRET_KEY"),
"",
)),
)
client := s3.NewFromConfig(cfg, func(o *s3.Options) {
o.BaseEndpoint = aws.String("https://storage.edge.network")
o.UsePathStyle = true
})
}Ruby
require 'aws-sdk-s3'
client = Aws::S3::Client.new(
endpoint: 'https://storage.edge.network',
region: 'us-east-1',
force_path_style: true,
access_key_id: ENV['EDGE_ACCESS_KEY'],
secret_access_key: ENV['EDGE_SECRET_KEY']
)Upload an object#
JavaScript
await client.send(new PutObjectCommand({
Bucket: 'my-bucket',
Key: 'images/photo.jpg',
Body: fileBuffer,
ContentType: 'image/jpeg'
}))Python
# Upload a file
client.upload_file('local-file.txt', 'my-bucket', 'remote-file.txt')
# Or upload bytes directly
client.put_object(
Bucket='my-bucket',
Key='hello.txt',
Body=b'Hello, Edge Storage!'
)Go
_, err := client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String("my-bucket"),
Key: aws.String("hello.txt"),
Body: strings.NewReader("Hello, Edge Storage!"),
})Ruby
client.put_object(
bucket: 'my-bucket',
key: 'hello.txt',
body: 'Hello, Edge Storage!'
)Download an object#
JavaScript
const response = await client.send(new GetObjectCommand({
Bucket: 'my-bucket',
Key: 'images/photo.jpg'
}))
const body = await response.Body.transformToByteArray()Python
# Download to file
client.download_file('my-bucket', 'remote-file.txt', 'local-file.txt')
# Or get bytes
response = client.get_object(Bucket='my-bucket', Key='hello.txt')
data = response['Body'].read()Ruby
response = client.get_object(bucket: 'my-bucket', key: 'hello.txt')
puts response.body.readList objects#
JavaScript
const response = await client.send(new ListObjectsV2Command({
Bucket: 'my-bucket',
Prefix: 'images/'
}))
for (const obj of response.Contents || []) {
console.log(obj.Key, obj.Size)
}Python
response = client.list_objects_v2(
Bucket='my-bucket',
Prefix='images/'
)
for obj in response.get('Contents', []):
print(obj['Key'], obj['Size'])Common configuration#
Whichever SDK you use, you’ll need these settings:
| Setting | Value | Notes |
|---|---|---|
endpoint |
https://storage.edge.network |
Required |
region |
us-east-1 |
Use any valid region string |
forcePathStyle |
true |
Required for Edge Storage |
Something unclear or out of date? Tell us