从s3 bucket ajax php下载

从s3 bucket ajax php下载

问题描述:

Hey everyone so I'm moving my site over to AWS and s3 using elastic beanstock. I have run into a bit of a problem because my PHP files are stored on a different URL than the s3 bucket is.

For example, my website URL is similar to this http://mysite.eu-west-1.elasticbeanstalk.com/test2.php, and my s3 bucket URL is similar to this https://s3.us-east-2.amazonaws.com/my-media-services/stockUploads/a288f1366c721c3cdcc8d417f866758d/test_184.mp4 as you can see they are different and the HTML download attribute is not supported via cross-origin URLS.

As such is it possible to download the files via ajax so that the user wouldn't have to go to another page and thereby to have a similar experience to the html5 download attribute? If so how would I do this? I'm using AWS PHP SDK

this no longer works because it's on s3 and is cross-origin

<a href="https://s3.us-east-2.amazonaws.com/my-media-services/stockUploads/a288f1366c721c3cdcc8d417f866758d/test_184.mp4" download>download</a>

You should enable cors on s3 bucket:

  1. Select bucket
  2. Select permissions tab
  3. Select CORS
  4. Enable CORS configuration:

    <CORSConfiguration> <CORSRule> <AllowedOrigin>http://www.example1.com</AllowedOrigin> <AllowedMethod>GET</AllowedMethod> <AllowedHeader>*</AllowedHeader> <MaxAgeSeconds>3000</MaxAgeSeconds> </CORSRule> </CORSConfiguration>

    enter image description here

For download the file you need add a metadata to object (mp4):

enter image description here

Here is working:

<a id="download" href="https://s3.amazonaws.com/dev-repositorio-imagenes/colocolo/YODA_HD.mp4" download>Download</a>

Read More

From the server-code it's very important upload the file as:

content-type:"application/octet-stream"

How to set content-type PHP SDK

Hope to help you!