When developing web-based applications, we often run into the problem of front-end JavaScript being unable to read resources stored on Alibaba Cloud OSS (Object Storage Service). This article documents a typical CORS (Cross-Origin Resource Sharing) troubleshooting process and its solution in detail.
Problem Description
Scenario:
We wrote a Node.js script to scrape data and upload it to Alibaba Cloud OSS. Then, on the front-end pages of the Hexo blog, we used JavaScript (fetch or XMLHttpRequest) to request that JSON data file in order to render charts or lists.
Symptoms:
- Direct access works: pasting the file’s OSS URL into the browser address bar downloads or previews the file fine, with an HTTP 200 status.
- Code requests fail: when the page’s code tries to read the file, the browser console reports an error, usually “Network Error” or “Failed to fetch”.
- In DevTools: in the Network panel, the request status may show as (failed) or CORS error, and Response Data can’t be viewed.
Analysis
This is a classic Cross-Origin Resource Sharing (CORS) problem.
What is cross-origin?
The browser’s Same-Origin Policy is a security mechanism that restricts how a document or script loaded from one origin (protocol + domain + port) interacts with resources from another origin.
When your blog (for example https://wenjun.me) tries to request a resource on Alibaba Cloud OSS (for example https://bucket-name.oss-cn-shanghai.aliyuncs.com/data.json), the browser makes a cross-origin request because the domains differ.
Why does direct access work?
When you type the URL directly into the address bar, that’s a “navigation” action and is not subject to the Same-Origin Policy. But when you use fetch or axios in JS code, the browser strictly checks the response headers the server returns.
How to verify
We can use the curl command to simulate the request and inspect the response headers:
|
If the Access-Control-Allow-Origin field is missing from the response headers, the browser blocks the response data for that request and throws a cross-origin error.
Solution
To fix this, we need to configure the server side (Alibaba Cloud OSS) to allow cross-origin access.
Step 1: Sign in to the Alibaba Cloud OSS console
- Sign in to the Alibaba Cloud OSS Management Console.
- In the left navigation bar, find and click your Bucket name (for example
file201503).
Step 2: Configure cross-origin rules
Go to Data Security -> Cross-Origin Settings (CORS).
Click Create Rule.
Fill it in as follows:
Allowed Origins:
- It’s best to enter your site’s domain, for example
https://wenjun.me. - For testing you can use
*(allows all domains), but in production it’s best to restrict the domain.
- It’s best to enter your site’s domain, for example
Allowed Methods:
- Check
GET(if you only read files). - If you need uploads, you may also need
PUT,POST, etc.
- Check
Allowed Headers:
- Use
*(allow all request headers).
- Use
Expose Headers:
- Enter
ETag. - This lets front-end JS access the
ETagresponse header, which is sometimes needed for cache validation.
- Enter
Click OK to save.
The configuration usually takes effect within seconds. Once done, refresh your page and the data should load normally.
Advanced Optimization: Setting the Right File Metadata
Besides fixing CORS, to give files a better experience in the browser, it’s best to set the correct file headers on upload:
- Content-Type: set to
application/json(for JSON files). This tells the browser it’s a data file, not plain text. - Content-Disposition: set to
inline.inline: the browser tries to preview the content directly in the window.attachment: the browser forces a download dialog.
Example code in Node.js (the ali-oss SDK):
|
With the above configuration, you can cleanly solve the cross-origin problem of the front end reading data from Alibaba Cloud OSS and improve the user experience.
评论
0 条评论