duanbi7247 2018-10-23 22:00
浏览 80
已采纳

从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>

  • 写回答

1条回答 默认 最新

  • douzhuozhu9544 2018-10-24 00:19
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号