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
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!
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.
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
andSECRET
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:
You can then use this URL with a simple fetch request without the need for additional headers:
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