How to use Amazon SDK with Digital Ocean spaces
June 08, 2021 ยท 5 min read
JS

Let's start out with the code we'll be using

    const spacesEndpoint = new AWS.Endpoint('sfo2.digitaloceanspaces.com');
    const s3 = new AWS.S3({
        endpoint: spacesEndpoint,
        accessKeyId: 'YOUR ACCESS KEY HERE',
        secretAccessKey: 'YOUR SECRET HERE'
     });
    var params = {
        Bucket: 'YOUR DO SPACE NAME HERE',
        Key: path + filename,
        Body: rawBuffer,
        ACL: "public-read"
     };
    s3.putObject(params, function(err, data) {
        if (err) console.log(err, err.stack);
        else{
            let fileUrl = "https://yourbucketname.sfo2.digitaloceanspaces.com/"+path+filename
            //This is the url to your uploaded file
        };
    });

The explaination

const spacesEndpoint will be the region you choose for your Digital Ocean space, my region is San Fransico so you will see I have sfo2.digitaloceanspaces.com.

accessKeyId and secretAccessKey are both obtained by logging into your Digital Ocean account and clicking spaces then near the top right you will see manage keys. After click manage keys, Generate a new Spaces Access key.

I've included a screenshot of what those keys look like here. The top is accessKeyId and the bottom is secretAccessKey.

The Params

Bucket will be the name of your Digital Ocean Space.

Key will be the path where your file is saved at, and the file name you can see how this might look in the code block above.

Body will be the raw data that makes up your image.

ACL is where you can set whether or not the image can be viewed by anyone or if its private by doing 'public-read' or 'private'.