Skip to content

Instantly share code, notes, and snippets.

@zmts
Last active August 27, 2020 08:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmts/b83e33011feff3c7ffd57357a6f54758 to your computer and use it in GitHub Desktop.
Save zmts/b83e33011feff3c7ffd57357a6f54758 to your computer and use it in GitHub Desktop.
Configure AWS SDK with Digital Ocean endpoint; S3

Configure AWS SDK with Digital Ocean endpoint

const AWS = require('aws-sdk')
const spacesEndpoint = new AWS.Endpoint('nyc3.digitaloceanspaces.com');

const s3 = new AWS.S3({
    endpoint: spacesEndpoint,
    accessKeyId: 'ACCESS_KEY',
    secretAccessKey: 'SECRET_KEY'
});
uploadImage (buffer, fileName) {
    if (!Buffer.isBuffer(buffer)) {
      throw new Error('buffer param is not a Buffer type')
    }

    return new Promise((resolve, reject) => {
      const params = {
        Bucket: this.bucket,
        Key: fileName,
        Body: buffer,
        ContentType: 'image/jpeg',
        ACL: 'public-read' // makes uploaded objects public
      }

      this.client.upload(params, (error, data) => {
        if (error) {
          logger.error(`${this.constructor.name}: unable to upload objects`, error)
          return reject(error)
        }
        resolve(data.Location)
      })
    })
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment