I have a .json file in my Amazon s3 bucket when i try to access the file using http call in my Angular2 app i am getting an error
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://s3.us-east-2.amazonaws.com/....../list.json. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
I made the file in my bucket to be public and gave the access as read, write and edit.
Here is my Angular Code:
getValue(){
return this._http.get('https://s3.us-east-2.amazonaws.com/........./list.json').toPromise().then(res => <Contact[]> res.json().data)
.then(data => {return data;});
}
My cross origin XML in AWS
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
Can anyone help me resolving this issue Thanks in advance
Amazon S3 website endpoints do not support HTTPS or access points. If you want to use HTTPS, you can use Amazon CloudFront to serve a static website hosted on Amazon S3.
Amazon S3 allows both HTTP and HTTPS requests. By default, requests are made through the AWS Management Console, AWS Command Line Interface (AWS CLI), or HTTPS. To comply with the s3-bucket-ssl-requests-only rule, confirm that your bucket policies explicitly deny access to HTTP requests.
In short, if you set x-amz-acl: public-read on a file then you can access it as https://s3.amazonaws.com/bucket-name/path-to-file . No need for enabling website hosting, unless you want the pretty hostname and support for index and error documents.
I was looking for such feature too but it seems that AWS S3 do not have HTTP2 support yet. BTW, google cloud storage have this feature though.
After downloading the project or after creating a new project simply run the ng serve Angular command and it will run your Angular application on the default port i.e. 4200. Below is the sample output of the custom search filter project. Now next task is how to deploy our Angular application to Amazon AWS by using the S3 bucket.
You can make use of the following policy and add it under the manage bucket policy section. But make sure you change the bucket name to yours. You can now copy the angular build on your local system Angular App dist/techdirectarchive path and upload it in S3.
Amazon S3 supports both virtual-hosted–style and path-style URLs to access a bucket. Because buckets can be accessed using path-style and virtual-hosted–style URLs, we recommend that you create buckets with DNS-compliant bucket names. For more information, see Bucket restrictions and limitations .
In Amazon S3, path-style URLs use the following format. https://s3. Region .amazonaws.com/ bucket-name / key name. For example, if you create a bucket named mybucket in the US West (Oregon) Region, and you want to access the puppy.jpg object in that bucket, you can use the following path-style URL: https: //s3.us-west-2.amazonaws.
Probably nothing to do with your Angular code. Take a look at AWS S3 docs on CORS. You need to set up a CORS configuration in your bucket. The docs provide this as an example:
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>http://www.example1.com</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
<CORSRule>
<AllowedOrigin>http://www.example2.com</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
</CORSRule>
</CORSConfiguration>
The docs also say:
To configure your bucket to allow cross-origin requests, you create a CORS configuration, an XML document with rules that identify the origins that you will allow to access your bucket, the operations (HTTP methods) will support for each origin, and other operation-specific information.
This allows you to decide which origins can access your bucket (for example, if you want all origins to be able to access it, you can use the wildcard *
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With