Question

How to fetch images from Spaces with a typescript fetch request?

I’m trying to fetch information on the images that I’m storing in my Space and keep getting a 403 error.

There’s no explicit example of how to do that with a simple fetch request so need a bit of help to make this work.

Here’s what I’m trying at the moment:

fetch('https://${space-name}.sgp1.digitaloceanspaces.com', {
    method: 'GET',
    headers: {
        'Host': '${space-name}.sgp1.digitaloceanspaces.com',
        'authorization': 'AWS ${ACCESS_KEY}:${SECRET}',
        'acl': "x-amz-acl:public-read",
    }
}).then(res => {
    console.log(res)
})

Hope you can help


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
March 2, 2024

Hey,

The issue you’re encountering with the 403 error likely stems from incorrect or insufficient authentication details or headers in your fetch request. DigitalOcean Spaces is compatible with the S3 API, so you need to sign your requests properly. However, using AWS signature directly in a fetch request isn’t straightforward because it requires generating a signature based on your secret key, the request method, the endpoint, and the headers.

For security reasons, you shouldn’t expose your ACCESS_KEY and SECRET in client-side code. It’s better to handle this on the server side or use a pre-signed URL for public access.

A pre-signed URL provides a way to give temporary access to your files without exposing your credentials. You should generate these URLs server-side. Here’s a basic example in Node.js using the AWS SDK, which you can adapt to your server-side TypeScript code:

import AWS from 'aws-sdk';

// Configure the AWS SDK with your DigitalOcean Spaces details
const spacesEndpoint = new AWS.Endpoint('sgp1.digitaloceanspaces.com');
const s3 = new AWS.S3({
    endpoint: spacesEndpoint,
    accessKeyId: 'YOUR_ACCESS_KEY',
    secretAccessKey: 'YOUR_SECRET_KEY',
});

// Generate a pre-signed URL for a file in your Space
const url = s3.getSignedUrl('getObject', {
    Bucket: 'your-space-name',
    Key: 'your-file-name',
    Expires: 60, // URL expiration time in seconds
});

console.log(url);

You can then use this URL with a simple fetch request without the need for additional headers:

fetch(presignedUrl)
    .then(res => res.json()) // or res.blob() if you're fetching an image
    .then(data => {
        console.log(data);
    });

If you absolutely need to fetch directly without a pre-signed URL (not recommended for client-side due to security concerns), you’ll need to construct a proper authentication header. This is quite complex as it involves creating a signature using your secret key. AWS provides detailed documentation on this process, but implementing it is beyond the scope of a simple fetch request.

For security and simplicity, I recommend generating pre-signed URLs on your server and using those URLs in your client-side fetch requests. This approach keeps your credentials secure and simplifies the client-side code to a straightforward fetch request.

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel