Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Amazon s3 using http in angular2

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

like image 485
skid Avatar asked Feb 03 '17 22:02

skid


People also ask

Does AWS S3 support HTTP?

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.

Does S3 use HTTP or HTTPS?

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.

Can I access S3 bucket from browser?

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.

Does S3 use http 2?

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.

How to deploy angular application to Amazon AWS using S3 bucket?

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.

How to copy angular build from local system to 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.

How do I access a bucket in Amazon 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 .

What is a path-style url in Amazon S3?

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.


1 Answers

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 *).

like image 180
Josh Beam Avatar answered Sep 24 '22 23:09

Josh Beam