-
-
Save codeibrahima/bc6bf68125ffce8f45a234ad741a2685 to your computer and use it in GitHub Desktop.
How to use Digital Ocean Spaces API with NodeJS using the AWS-SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment