Skip to content

Instantly share code, notes, and snippets.

@eliashussary
Last active October 16, 2024 02:42
Show Gist options
  • Save eliashussary/f9ed666140c765d4db6451c58b2acc58 to your computer and use it in GitHub Desktop.
Save eliashussary/f9ed666140c765d4db6451c58b2acc58 to your computer and use it in GitHub Desktop.
How to use Digital Ocean Spaces API with NodeJS using the AWS-SDK
import AWS from 'aws-sdk'
import Buffer from 'buffer'
AWS.config.setPromisesDependency(require('bluebird'))
;(async () => {
const spaces = new AWS.S3({
// {region}.digitaloceanspaces.com ex: nyc3.digitaloceanspaces.com
endpoint: new AWS.Endpoint(process.env.DO_ENDPOINT),
// generate here:
// https://cloud.digitalocean.com/settings/api/
accessKeyId: process.env.DO_ACCESS_KEY,
secretAccessKey: process.env.DO_SECRET_KEY,
})
const fileKey = 'my-file-name'
const fileBuffer = Buffer.from('This is a test') // typically and image or file buffer.
await spaces.putObject({
// if you haven't already created one, you can do so here: https://cloud.digitalocean.com/spaces
// alternative programatically by calling the following before putObject:
// spaces.createBucket({bucket:'my-bucket-name'})
Bucket: process.env.DO_BUCKET_NAME,
Key: fileKey,
Body: fileBuffer,
ACL: 'public-read'
}).promise()
})
@evertonpavan
Copy link

Thank you, @eliashussary!

@Muhammad-Ahmed-Lakhani
Copy link

thanks <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment