Question

i want to upload image from react js to digitalocean spaces. but it gives cors error.

hello.

from react js i want to upload image directly to the digitalocean spaces without any server.

the frontend run on localhost 5173 port. and i already configured the cors for this port.

but its still gives me this issue.

here is my code

image alt text

cors configuration: image alt text


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
October 15, 2024

Hi there,

The CORS preflight requests are often sent using the OPTIONS method, especially when performing a POST or PUT request from a browser. Make sure your CORS settings allow the OPTIONS method. You can add OPTIONS to your allowed methods in the DigitalOcean Spaces CORS configuration:

Allowed Methods: GET, PUT, DELETE, HEAD, POST, OPTIONS

Also check that you’re setting the correct headers in your request, especially Content-Type. Sometimes, a missing or incorrect Content-Type can trigger CORS issues.

In your case, you already have the content type set in the params:

ContentType: file.type

Besides that, make sure that the file type is correctly detected. If needed, explicitly set it to multipart/form-data in case of issues:

ContentType: "multipart/form-data",

You can also use your browser’s developer tools to see what the CORS preflight request (OPTIONS) and response look like. Check if the headers on the response allow the origin, methods, and other necessary headers.

Make sure that your CORS settings on DigitalOcean Spaces allow the following headers:

  • Access-Control-Allow-Origin: * or the specific origins (http://localhost:5173)
  • Access-Control-Allow-Methods: Ensure GET, PUT, POST, and OPTIONS are allowed
  • Access-Control-Allow-Headers: If you’re sending custom headers, such as Authorization or Content-Type, make sure they are allowed here as well.

Once you’ve made these changes, try again, and let me know if the issue persists!

- Bobby

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.